1

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!

2
  • So the structure for your multidimensional array is fixed this way? Or resources could be at deeper levels? Commented Jun 25, 2019 at 6:59
  • resources is fixed, correct! Commented Jun 25, 2019 at 7:03

5 Answers 5

2
<?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
Sign up to request clarification or add additional context in comments.

1 Comment

That was pretty easy :-D
1

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

Comments

1
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.

Comments

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

1 Comment

Please edit your answer to add some explanation of how this solves the problem.
0

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);

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.