PHP foreach with Nested Array?

后端 未结 4 1027
广开言路
广开言路 2020-11-27 02:53

I have a nested array in which I want to display a subset of results. For example, on the array below I want to loop through all the values in nested array[1].

Ar         


        
相关标签:
4条回答
  • 2020-11-27 03:07

    Both syntaxes are correct. But the result would be Array. You probably want to do something like this:

    foreach ($tmpArray[1] as $value) {
      echo $value[0];
      foreach($value[1] as $val){
        echo $val;
      }
    }
    

    This will print out the string "two" ($value[0]) and the integers 4, 5 and 6 from the array ($value[1]).

    0 讨论(0)
  • 2020-11-27 03:10
    foreach ($tmpArray as $innerArray) {
        //  Check type
        if (is_array($innerArray)){
            //  Scan through inner loop
            foreach ($innerArray as $value) {
                echo $value;
            }
        }else{
            // one, two, three
            echo $innerArray;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:21

    As I understand , all of previous answers , does not make an Array output, In my case : I have a model with parent-children structure (simplified code here):

    public function parent(){
    
        return $this->belongsTo('App\Models\Accounting\accounting_coding', 'parent_id');
    }
    
    
    public function children()
    {
    
        return $this->hasMany('App\Models\Accounting\accounting_coding', 'parent_id');
    }
    

    and if you want to have all of children IDs as an Array , This approach is fine and working for me :

    public function allChildren()
    {
        $allChildren = [];
        if ($this->has_branch) {
    
            foreach ($this->children as $child) {
    
                $subChildren = $child->allChildren();
    
                if (count($subChildren) == 1) {
                    $allChildren  [] = $subChildren[0];
                } else if (count($subChildren) > 1) {
                    $allChildren += $subChildren;
                }
            }
        }
        $allChildren  [] = $this->id;//adds self Id to children Id list
    
        return $allChildren; 
    }
    

    the allChildren() returns , all of childrens as a simple Array .

    0 讨论(0)
  • 2020-11-27 03:30

    If you know the number of levels in nested arrays you can simply do nested loops. Like so:

    //  Scan through outer loop
    foreach ($tmpArray as $innerArray) {
        //  Check type
        if (is_array($innerArray)){
            //  Scan through inner loop
            foreach ($innerArray as $value) {
                echo $value;
            }
        }else{
            // one, two, three
            echo $innerArray;
        }
    }
    

    if you do not know the depth of array you need to use recursion. See example below:

    //  Multi-dementional Source Array
    $tmpArray = array(
        array("one", array(1, 2, 3)),
        array("two", array(4, 5, 6)),
        array("three", array(
                7,
                8,
                array("four", 9, 10)
        ))
    );
    
    //  Output array
    displayArrayRecursively($tmpArray);
    
    /**
     * Recursive function to display members of array with indentation
     *
     * @param array $arr Array to process
     * @param string $indent indentation string
     */
    function displayArrayRecursively($arr, $indent='') {
        if ($arr) {
            foreach ($arr as $value) {
                if (is_array($value)) {
                    //
                    displayArrayRecursively($value, $indent . '--');
                } else {
                    //  Output
                    echo "$indent $value \n";
                }
            }
        }
    }
    

    The code below with display only nested array with values for your specific case (3rd level only)

    $tmpArray = array(
        array("one", array(1, 2, 3)),
        array("two", array(4, 5, 6)),
        array("three", array(7, 8, 9))
    );
    
    //  Scan through outer loop
    foreach ($tmpArray as $inner) {
    
        //  Check type
        if (is_array($inner)) {
            //  Scan through inner loop
            foreach ($inner[1] as $value) {
               echo "$value \n";
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题