简体   繁体   English

我如何遍历此数组-PHP

[英]How can I loop through this array - PHP

This is the var_dump($options) 这是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 根据要求Print_r()

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
        )

)

use print_r($options) in staid of var_dump it's easier to read.. var_dump使用print_r($options)易于阅读。

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 这是一个有效的例子

Try this? 尝试这个?

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

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 : 其print_r输出将是:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM