0

I am using PHP 5.5.12.

I have the following multidimensional array:

[
    {
        "id": 1,
        "type":"elephant", 
        "title":"Title of elephant"
    }, 
    {
        "id": 2,
        "type":"tiger", 
        "title":"Title of tiger"
    },
    {
        "id": 3,
        "type":"lion", 
        "title":"Title of lion",
        "children":[{
            "id": 4,
            "type":"cow", 
            "title":"Title of cow"
        },
        {
            "type":"elephant", 
            "title":"Title of elephant"
        },
        {
            "type":"buffalo", 
            "title":"Title of buffalo"
        }]
    }
]

I am iterating this array using foreach loop.

The array key type must be in elephant, tiger and lion. If not, then the result should return false.

How can I achieve this?

3
  • 2
    Any source code you wrote so far that we can use as a model?
    – Masiorama
    May 12, 2015 at 10:06
  • Have you tried anything?
    – B001ᛦ
    May 12, 2015 at 10:07
  • So, you want to verify that whether every single key (in every nested level) that named type have a valid value (of those you specified)?
    – someOne
    May 12, 2015 at 10:25

4 Answers 4

1

So you want to check if your $myArray contains a value or not:

// first get all types as an array
$type = array_column($myArray, "type");

// specify allowed types values
$allowed_types = ["lion", "elephant", "tiger"];

$count = count($type);
$illegal = false;

// for loop is better
for($i = 0; $i < $count; $i++)
{
    // if current type value is not an element of allowed types
    // array, then both set the $illegal flag as true and break the 
    // loop
    if(!in_array($type[$i], $allowed_types)
        $illegal = true;
        break;
}
0

Since you're using PHP5.5.12, you can make use of array_column.

$arr = json_decode($json, true);
//Walk through each element, only paying attention to type
array_walk( array_column($arr, 'type'), function($element, $k) use(&$arr) {
    $arr[$k]['valid_type'] = in_array($element, array('lion', 'tiger', 'elephant'));
});

From here, each element in the array ($arr) will have a new key valid_type with a boolean value - 1 if the type is valid, 0 if it isn't.

https://eval.in/350322

-1

Is this something that you are looking for?

foreach($your_array as $item) {
    if (!array_key_exists('type', $item)) {
    return FALSE;
    }
}
1
  • The array key type must be in elephant, tiger and lion. If not, then the result should return false. - That doesn't check the value of the type.
    – ʰᵈˑ
    May 12, 2015 at 10:26
-1
function keyExists($arr, $key) {
    $flag = true;

    foreach($arr as $v) {
        if(!isset($v[$key])) {
            $flag = false;
            break;
        }
    }

    return $flag;
}

Hope this helps :)

2
  • The array key type must be in elephant, tiger and lion. If not, then the result should return false. - Your code doesn't check the value.
    – ʰᵈˑ
    May 12, 2015 at 10:26
  • Im sorry i did not understand the question correctly then. You could just add the test condition in the if condition and it will work as expected. May 12, 2015 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.