1

I have an array as below and I want to get all the values in front of 'id', in a seperated array. Like: arry('12', '10', '11', '9')

 array
      3 => 
        array
          'occurance' => int 4
          'id' => string '12' (length=2)
      1 => 
        array
          'occurance' => int 3
          'id' => string '10' (length=2)
      2 => 
        array
          'occurance' => int 2
          'id' => string '11' (length=2)
      0 => 
        array
          'occurance' => int 1
          'id' => string '9' (length=1)

5 Answers 5

7

You can use array_map

$values = array(
        3 => array('occurance' => 4,'id' => '12'),
        1 => array('occurance' => 3,'id' => '10'),
        2 => array('occurance' => 2,'id' => '11'),
        0 => array('occurance' => 1,'id' => '9'));

$values = array_map(function($var){ return $var['id']; }, $values);
var_dump($values);

Output

array
  3 => string '12' (length=2)
  1 => string '10' (length=2)
  2 => string '11' (length=2)
  0 => string '9' (length=1)
Sign up to request clarification or add additional context in comments.

Comments

2

Use a foreach loop and put id in another array:

$newArray = array()
foreach($array as $val){
    $newArray[] = $val['id'];
}

Comments

1

Try this:

foreach($YourArray as $ar)
{
    $FinalArray[] = $ar['id'];
}

print_r($FinalArray);

Comments

1

Since PHP 5.5 you can use array_column.

$idArray = array_column($multiArray, 'id');

Comments

0
foreach($array as $key=>$value)
{
if(array_key_exists('id',$value))
$arr2[]=$value['id'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.