user8918437
user8918437

Reputation: 65

How to find in a multidimensional array a specific value using a specific key

I have an array which looks like this

$myArray = Array
(
    [Standardbox] => Array
        (
            [details] => Array
                (
                    [name] => Standardbox
                )

            [resources] => Array
                (
                    [0] => Array
                        (
                            [resourceId] => 1
                            [resourceName] => Knife
                            [amount] => 1
                            [unit] => 2
                        )

                    [1] => Array
                        (
                            [resourceId] => 2
                            [resourceName] => Fork
                            [amount] => 1
                            [unit] => 2
                        )

                )

        )

)

and I want to check if the value of 1 of the key resourceId (knife) is present in the array.

I have found some functions here at stackoverflow but nothing really works for my purposes:

This one looks very promising but I think it does not consider that the array is multidimensional:

function multi_key_in_array($needle, $haystack, $key) 
{
    foreach ($haystack as $h) 
    {
        if (array_key_exists($key, $h) && $h[$key]==$needle) 
        {
            return true;
        }
    }
    return false;
}

and then calling

if(multi_key_in_array(1, $myArray, "resourceId"))
{
     // It is present in the array
}

Any help is highly appreciated!

Upvotes: 1

Views: 70

Answers (5)

Nadir Latif
Nadir Latif

Reputation: 3763

As I understand, you need to check if one of the resourceId values is 1. You can do this by iterating over the array values. for example:

function IsResourcePresent($myArray) {
    $resources = $myArray['Standardbox']['resources'];
    for ($count = 0; $count < count($resources); $count++) {
        if ($resources[$count]['resourceId'] == '1') return true;
    }
    return false;
}

IsResourcePresent($myArray);

Upvotes: 0

Haris
Haris

Reputation: 368

function arr_find_recursive($key, $value, $array)
{
    foreach ($array as $arr_key => $arr_val) 
    {
        if (is_array($arr_val))
        {
            if (arr_find_recursive($key, $value, $arr_val))
            {
                return true;
            }
        }
        else
        {
            if ($arr_key === $key && $arr_val === $value) 
            {
                return true;
            }
        }
    }
    return false;
}

//Call function

if (arr_find_recursive("resourceId", 2, $myArray))
{
    echo "exists";
}
else
{
    echo "not found";
}

This function is recursive and can find key value pair in array of any depth.

Upvotes: 1

Riosant
Riosant

Reputation: 344

$result = in_array('1', array_column($myArray['Standardbox']['resources'], 'resourceId'));
if($result)
echo $result.' - found';
else 
echo 'Not found';

Upvotes: 0

suhas pandit
suhas pandit

Reputation: 273

<?php

function multi_key_in_array($needle, $haystack, $key) 
{
    foreach ($haystack as $h) 
    {
        if (array_key_exists($key, $h) && $h[$key]==$needle) 
        {
            return true;
        }
    }
    return false;
}


$myArray = Array
(
    'Standardbox' => Array
        (
            'details' => Array
                (
                    'name' => 'Standardbox'
                ),

            'resources' => Array
                (
                    0 => Array
                        (
                            'resourceId' => 1,
                            'resourceName' => 'Knife',
                            'amount' => 1,
                            'unit' => 2
                        ),

                    1 => Array
                        (
                            'resourceId' => 2,
                            'resourceName' => 'Fork',
                            'amount' => 1,
                            'unit' => 2
                        )

                )

        )

);


if(multi_key_in_array(1, $myArray['Standardbox']['resources'], "resourceId"))
{
     echo 'true';
} else {
    echo 'false';
}

> Blockquote

Upvotes: 2

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

you can use array_column with in_array

$find = 1;
$r = in_array($find,array_column($myArray['Standardbox']['resources'], 'resourceId'));
echo $r;

https://3v4l.org/Gck1l

Upvotes: 1

Related Questions