Get a specific pair of key values ​​from a multidimensional array

I have the following array:

array(2) {
  [0] => array(3) {
    ["submission_id"] => int(28)
    ["date"] => string(22) "2010-10-18 15:55:33+02"
    ["user_id"] => int(12)
  }
  [1] => array(3) {
    ["submission_id"] => int(37)
    ["date"] => string(22) "2010-11-21 16:02:07+01"
    ["user_id"] => int(23)
  }

I want to get only user_id key values ​​from this array. I could obviously get hung up on it, but I was wondering if there is a faster way.

+5
source share
2 answers

You can use array_map(maybe not faster, since it will make a function call for an array element):

function getUserId($a) {
    return $a['user_id'];
}

$user_ids = array_map('getUserId', $array);

In addition, a loop is the only way ( array_mapdoing a loop anyway).

+17
source

You can only access user_id values ​​such as this if you know the index of the array you want to access:

$arr = your array here..
echo $arr[0]['user_id'];
echo $arr[1]['user_id'];
-3
source

All Articles