1

I have an multidimensional array in php as follows.

MyArray
(
    [0] => Array
        (
            [id] => c
            [val] => 290
        )

   [2] => Array
        (
            [id] => a
            [val] => 160
        )

   [3] => Array
        (
           [id] => v
           [val] => 150
        )
)

out of this array i want to extract array(c,a,v) I am not able to extract this particular array out of the above multidimensional array. How can i achieve it?

1
  • 1
    Their keys differ. What is the relationship? Is it always the first element of the sub-array, no matter its key? Please always be as specific as possible with your requirements. Otherwise, answerers are left to guess at your real need. Commented Dec 2, 2014 at 15:22

3 Answers 3

2

In PHP >= 5.5 you can use array_column()

$letters = array_column($originalArray, 'id');
Sign up to request clarification or add additional context in comments.

Comments

2

Suggest you to use array_map().

$your_arr = array(
    array(
        "id" => "c" ,"val" => 290 ),
    array(
        "id" => "a","val" => 160,
    ),
    array(
        "user" => "v","val" => 150,
    )
);

$arr = array_map(function($v){
    return $v['id']; 
}, $your_arr);

print '<pre>';
print_r($arr);
print '</pre>';

Output:

Array
(
    [0] => c
    [1] => a
    [2] => v
)

Comments

1

Using array_map might help.

array_map(function($elem) { return array_values($elem)[0]; }, $array);

3 Comments

The value 'v' does not come from a key ['id']
Hi sorry i just updated my array name. Can you tell me which variable i should replace it with in your code? and in variable the array is returned into?
$array stays for the name of array (the latter variable.)

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.