0

This is the var_dump($options)

array (size=4)
  0 => string '2' (length=1)
  'parent_2_children' => 
    array (size=3)
      0 => string '22' (length=2)
      1 => string '24' (length=2)
      2 => string '23' (length=2)
  1 => string '3' (length=1)
  'parent_3_children' => 
    array (size=3)
      0 => string '26' (length=2)
      1 => string '25' (length=2)
      2 => string '27' (length=2)

What I have tried up to now is

if(!is_null($options))
        {
            foreach($options as $option)
            {
                 if(!array_key_exists('parent_'.$option.'_children',$options))
                 {
                    //if position null output an error
                 }
            }   


        }

Print_r() as requested

Array
(
    [0] => 2
    [parent_2_children] => Array
        (
            [0] => 22
            [1] => 24
            [2] => 23
        )

    [1] => 3
    [parent_3_children] => Array
        (
            [0] => 26
            [1] => 25
            [2] => 27
        )

)
3
  • 1
    What do you want to do while you are looping through it? Commented Oct 24, 2012 at 7:06
  • if position null output an error Commented Oct 24, 2012 at 7:08
  • Your existing code should be fine for that. Commented Oct 24, 2012 at 7:12

3 Answers 3

1

use print_r($options) in staid of var_dump it's easier to read..

check if you got a numeric key, then check if the new key exists. Throw an error.

if(!is_null($options)) {
    foreach($options as $key => $option) {
        if(is_int($key) && !array_key_exists('parent_'.$option.'_children',$options)) {
           echo 'parent_'.$option.'_children does not exist';
        }
    }   
}

Here is a working example

Sign up to request clarification or add additional context in comments.

3 Comments

what do you want it to do? I can throw an exception, or echo some text... I'll update. But note that it will only show something when the key doesn't exist
i'm already echo 'parent_'.$option.'_children'; this. does not work
the array you provided is a valid array. If you got an invalid key, it will output the message. try pushing a non existing value like $options[0] = '9999999'; before running the loop
0

Your code is correct. An additional check on the nature of the key will reduce the execution, since you have to do the processing only for numeric keys.

    if(!is_null($options))
    {
        foreach($options as $key => $option)
        {
            if (is_numeric($key)) {
                 if(!array_key_exists('parent_'.$option.'_children',$options))
                  {
                       print 'parent_'.$option.'_children does not exist';
                   }
             }
        }   


    }

To test the code, try the following array :

 $options = array(0 => 2, 'parent_2_children' => array ( 0 => 22, 1 => 24, 2 => 23 ), 1 => 3, 'parent_3_children' => array ( 0 => 26, 1 => 25, 2 => 27 ), 2=>4 ); 

whose print_r output will be :

 Array
(
[0] => 2
[parent_2_children] => Array
    (
        [0] => 22
        [1] => 24
        [2] => 23
    )

[1] => 3
[parent_3_children] => Array
    (
        [0] => 26
        [1] => 25
        [2] => 27
    )

[2] => 4

)

and, it will output :

  parent_4_children does not exist

1 Comment

Right now, it will not output anything, since both array keys of the form parent_'.$option.'_children exists in the array
0

Try this?

if(!is_null($options)){
    foreach($options as $option){
        if(!array_key_exists('parent_'.$option.'_children',$options)){
            throw new Exception("Something went wrong!");
        }
    }   
}

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.