generic function for extracting values from an array with one particular key in PHP

Posted by Sabya on Stack Overflow See other posts from Stack Overflow or by Sabya
Published on 2010-06-15T10:46:37Z Indexed on 2010/06/15 10:52 UTC
Read the original article Hit count: 262

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
)

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays