Write a PHP program to retrieve all of the values for a given key
<?php function pluck($items, $key) { return array_map( function($item) use ($key) { return is_object($item) ? $item->$key : $item[$key]; }, $items); } print_r(pluck([ ['product_id' => 'p100', 'name' => 'Computer'], ['product_id' => 'p200', 'name' => 'Laptop'], ], 'name')); ?>
Sample Output:
Array ( [0] => Computer [1] => Laptop )
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
Array ( [0] => Computer [1] => Laptop )need an explanation for this answer? contact us directly to get an explanation for this answer