S
S
seoplus20192019-04-15 19:35:08
PHP
seoplus2019, 2019-04-15 19:35:08

How to find an element in a multidimensional array using array_search?

there is an array

Array
(
    [0] => Array
        (
            [node] => Array
                (
                    [id] => 17874292930344481
                    [text] => 
                    [created_at] => 1555015118
                    [did_report_as_spam] => 
                    [owner] => Array
                        (
                            [id] => 6773204196
                            [is_verified] => 
                            [profile_pic_url] => 
                            [username] => gadalka_lida999
                        )

                    [viewer_has_liked] => 
                    [edge_liked_by] => Array
                        (
                            [count] => 0
                        )

                )

        )

    [1] => Array
        (
            [node] => Array
                (
                    [id] => 18036829474119034
                    [text] => Her account please
                    [created_at] => 1555019066
                    [did_report_as_spam] => 
                    [owner] => Array
                        (
                            [id] => 4245266739
                            [is_verified] => 
                            [profile_pic_url] => 
                            [username] => el94__
                        )

                    [viewer_has_liked] => 
                    [edge_liked_by] => Array
                        (
                            [count] => 0
                        )

                )

        )

how to check if this array contains username = gadalka_lida999

did this but does not work

$key = array_search("gadalka_lida999", array_column($array, 'username'));
echo $arr[$key]["username"];

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
abyrvalg, 2019-04-16
@abyrvalg

If it is not known where exactly the desired key-value pair can be located, then something like this:

function isItemExists(iterable $source, $expectedKey, $expectedValue): bool
    {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveArrayIterator($source, RecursiveArrayIterator::CHILD_ARRAYS_ONLY),
            RecursiveIteratorIterator::LEAVES_ONLY
        );

        foreach ($iterator as $itemKey => $itemValue) {
            if ($expectedKey === $itemKey && $expectedValue === $itemValue) {
                return true;
            }
        }

        return false;
    }

A
Alexey Dabalaev, 2019-04-15
@AleksDab

Alternatively, traverse the array recursively. On each of the "levels" use array_search(). If found, exit.

0
0xD34F, 2019-04-15
@0xD34F

$key = array_search($name, array_map(function($n) {
  return $n['node']['owner']['username'];
}, $arr));

if ($key !== false) {
  $el = $arr[$key];
  ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question