generic function for extracting values from an array with one particular key in PHP
- by Sabya
Is it possible in PHP to extract values from an array with a particular key path and return an array of those values? I'll explain with an example:
$user =
array (
array(
id => 1,
email =>'[email protected]',
project => array ('project_id' => 222, 'project_name' => 'design')
),
array(
id => 2,
email =>'[email protected]',
project => array ('project_id' => 333, 'project_name' => 'design')
)
);
/** I have to write a function something like: */
$projectIds = extractValuesWithKey($user, array('project', 'project_id'));
print_r($projectIds);
Output:
Array(
[0] => 222,
[1] => 333
)