3

A simple thing to do, but I forgot how to convert

Array
(
    [0] => Array
        (
            [hashcode] => 952316176c1266c7ef1674e790375419
        )

    [1] => Array
        (
            [hashcode] => 5b821a14c98302ac40de3bdd77a37ceq
        )

)

into this:

Array (952316176c1266c7ef1674e790375419, 5b821a14c98302ac40de3bdd77a37ceq)
hello_there_andy
  • 2,039
  • 2
  • 21
  • 51
Pringles
  • 4,355
  • 3
  • 18
  • 19

4 Answers4

5

I know this is premature but since this is coming soon I figured I throw this out there. As of (the not yet released) PHP 5.5 you can use array_column():

 $hashcodes = array_column($array, 'hashcode');
John Conde
  • 217,595
  • 99
  • 455
  • 496
4

Try this :

$array  = array(array("test"=>"xcxccx"),array("test"=>"sdfsdfds"));

$result = call_user_func_array('array_merge', array_map("array_values",$array));

echo "<pre>";
print_r($result);

Output:

Array
(
    [0] => xcxccx
    [1] => sdfsdfds
)
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
2

A good ol' loop solves :)

<?php

$array = array(
    array( 'hashcode' => 'hash' ),  
    array( 'hashcode' => 'hash2' ), 
);

$flat = array();

foreach ( $array as $arr ) {
    $flat[] = $arr['hashcode'];
}

echo "<pre>";

print_r( $flat );

?>
kjetilh
  • 4,821
  • 2
  • 18
  • 24
  • I would say that this is a more elegant way to solve this, here is a similar example http://codepad.viper-7.com/ST6fSo i just wrote. – Ogelami Mar 27 '13 at 13:53
  • @Ogelami I'm still seeing a multidimensional array on the link posted. – Daryl Gill Mar 27 '13 at 13:54
  • @DarylGill thats odd, i don't i didn't use
     tags, and i used 2 print_r's can it be that?
    Here is the same code but with 
    's.
    http://codepad.viper-7.com/g9lD5e
    – Ogelami Mar 27 '13 at 13:56
0
$source = array(
    array(
        'hashcode' => '952316176c1266c7ef1674e790375419'
    ),
    array(
        'hashcode' => '5b821a14c98302ac40de3bdd77a37ceq'
    )
);

$result = array();
array_walk($source, function($element) use(&$result){
    $result[] = $element['hashcode'];
});

echo '<pre>';
var_dump($result);
mkjasinski
  • 3,115
  • 2
  • 22
  • 21