Q:

Write a PHP program to retrieve all of the values for a given key

0

Write a PHP program to retrieve all of the values for a given key

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?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
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now