简体   繁体   中英

Simple array manipulation

I have the following array structure;

Array(
[0] => Array(
        [product_name] => Silver
    )

[1] => Array(
        [product_name] => Gold
    )

[2] => Array(
        [product_name] => Ride
    )

[3] => Array(
        [product_name] => Bronze
    )
)

I need it to be like this, but I can't figure out how. I can't implement an additional function, it needs to be clean and neat.

Array(
    [0] => Silver
    [1] => Gold
    [2] => Ride
    [3] => Bronze
)

Thanks for any help offered. :)

foreach($array as &$value)
{
    $value = $value['product_name'];
}

I believe this works:

iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), false);

I'd use array_map personnaly !

$array = array_map(function($product){return $product['product_name'];}, $array);
foreach($firstarray as $var) {
   $secondarray[] = $var["product_name"];
}

If [product_name] is the key name, then this would work:

foreach($array as $i=>$a)
    $array[$i] = $a['product_name'];
$array = array(
    array("product_name" => "Silver"),
    array("product_name" => "Gold"),
    array("product_name" => "Ride"),
    array("product_name" => "Bronze")
);

You can do somenthing like this:

foreach($array as &$pos) $pos = $pos["product_name"];

So if you do a print_r($array) the result will be:

Array
(
    [0] => Silver
    [1] => Gold
    [2] => Ride
    [3] => Bronze
)

Hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM