1

Suppose an array like the following:

Array
(
    [a] => Array
        (
            [id] => 3
            [x] => binginb
        )

    [b] => Array
        (
            [id] => 5
            [x] => dibdab
        )

)

Now I would like to access this array based on its contents, so id and x in this case.

It would be nice here to be able to do something like

 $i = array_search(5,array_column($a,'id'));
 $stuff = dostuff($a[$i]['x']);

but that doesn't work since $a[1] is an undefined offset.

Is it possible to do anything like this without mutating the original array?

1
  • This might make sense for an id, but what would you expect back when there are multiple results? Commented Apr 24, 2018 at 19:25

4 Answers 4

4

array_search() returns the index of the result of array_column(). You could use array_keys() to find the key for this index:

$a = [
   'a' => ['id' => 3, 'x' => 'binginb'],
   'b' => ['id' => 5, 'x' => 'dibdab'],
   ];
$i = array_search(5, array_column($a,'id'));
$key = array_keys($a)[$i]; // get the key at the $i index.
$stuff = dostuff($a[$key]['x']); // Here, $a[$key]['x'] = 'dibdab'

Note that array_search() could return FALSE if the value is not found. You could test the result (using a strict comparison) before to use it.

$i = array_search(5, array_column($a,'id'));
if ($i !== false) {
    $key = array_keys($a)[$i]; // get the key at the $i index.
    $stuff = dostuff($a[$key]['x']); // Here, $a[$key]['x'] = 'dibdab'
}
Sign up to request clarification or add additional context in comments.

1 Comment

And to extend the technique slightly, if you want to get all matches for something, ('id' == 5), you could do $k = array_keys... instead of array_search then print_r($a[array_keys($a)[$k[1]]]['x']); will be "'x' for the second element matching "ID = 5" in $a"
2

Use array_column to re-index the array by id:

$new = array_column($a, 'x', 'id');

Then to search for 5, just:

echo $new[5];

If you may have more than just x:

$new = array_column($a, null, 'id');

Then:

echo $new[5]['x'];

If you only do this once:

echo array_column($a, 'x', 'id')[5];

Or:

echo array_column($a, null, 'id')[5]['x'];

Comments

1

If you don't wish to be limited by PHP built-in functions then try this out:

<?php
function dostuff( $var1 )
{
    print_r($var1);
}

$a = [
   'a' => ['id' => 3, 'x' => 'binginb'],
   'b' => ['id' => 5, 'x' => 'dibdab'],
];

array_map( // 2: pass each element from the result from array_filter to dostuff()
    'dostuff',
    array_filter( // 1: filter $a based on the criteria defined in the anonymous function
        $a,
        function($item){
            return $item['id'] === 5;
        }
    )
);

Output:

Array
(
    [id] => 5
    [x] => dibdab
)

Comments

0

i think it will help you,

<?php
$array = array(
    'a' => array(
        'id' => 3,
        'x' => 'binginb',
    ),
    'b' => array(
        'id' => 5,
        'x' => 'dibdab',
    )
);

$i = array_column($array,'x','id');

// Output of $i

Array ( [3] => binginb [5] => dibdab )

$stuff = dostuff($i[5]); // $i[5] return dibdab

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.