2

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]

4
  • You should use recursive function to get proper values. Commented Mar 16, 2017 at 5:36
  • @SumonSarker you mean array_walk_recursive? Commented Mar 16, 2017 at 5:40
  • Let me know, Do you want to make 'Tree List' from the array? Commented Mar 16, 2017 at 5:41
  • I dont exactly know what you mean but I want to get the end of [return] which is 1 and end of [error_row] which is 911111111111 Commented Mar 16, 2017 at 5:44

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

but how can I get the value of it?
Ok, Wait, I am updating the Answer @PureRhymerOrganization

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.