Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array into simple 1 level array in PHP [duplicate]

Tags:

arrays

php

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)
like image 552
Koffeehaus Avatar asked Aug 01 '26 14:08

Koffeehaus


2 Answers

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');
like image 196
John Conde Avatar answered Aug 03 '26 04:08

John Conde


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
)
like image 20
Prasanth Bendra Avatar answered Aug 03 '26 03:08

Prasanth Bendra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!