Multi-Dimensional Array if array elements are missing in foreach

Pure Rhymer Organization

I have this kind of multi-dimensional array

Array
(
    [return] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [911111111111] => 1
                        )

                )

        )

    [error_row] => Array
        (
            [0] => 911111111111
        )

)

The problem is I got an error

Message: Invalid argument supplied for foreach()

So I tried to put an (array) in foreach()

so far what I tried is

foreach((array)$check_cloud_id_exist as $key)
{
   foreach((array)$key as $subkey){
      foreach((array)$subkey as $childkey){
         foreach((array)$childkey as $childitem => $childs){
             //code here
         }
      }
   }
}

It gives me a loop array with one element containing the boolean value as an int which is not present in [error_row]

Sumon Sarker

Here is a Recursive Function Example to get All Key,Value from associative array in PHP.

$YourArray = [
  'return'=>[
    [
        [
            '911111111111'=>1
        ]
    ]
  ],
  'error_row'=>[
    '911111111111'
  ]
];

#print_r($YourArray);

function RecursiveFunc($array){
  if (is_array($array)) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            echo "Key   : ".$key.PHP_EOL; #Get The Key
            RecursiveFunc($value);
        }else{
            echo "Value : ".$value.PHP_EOL; #Get The Value
        }
    }
  }else{
    echo "Value : ".$array.PHP_EOL; #Get The Value
  }
}

RecursiveFunc($YourArray);

Note: Above Function will give you all key,value from $YourArray

Here is your solution

$end_of_return = '';

if (is_array($array['return'])) {
  $last_array_index   = count($array['return'])-1;
  $another_last_index = count($array['return'][$last_array_index])-1;
  $end_of_return = $array['return'][$last_array_index][$another_last_index];
}

$end_of_error_row = end($array['error_row']);

var_dump($end_of_return,$end_of_error_row);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related