1

how can i get all values from multidimensional associative array I dont want to use print_r want to control my array put all the value in normal array with unique values my array is look like this

array (size=10)
  0 => 
    array (size=3)
      0 => 
        array (size=1)
          'Campaign' => string 'DEMO' (length=4)
      1 => 
        array (size=1)
          'Campaign' => string 'Home_Sec' (length=8)
      2 => 
        array (size=1)
          'Campaign' => string '' (length=0)
  1 => 
    array (size=0)
      empty
  2 => 
    array (size=0)
      empty
  3 => 
    array (size=1)
      0 => 
        array (size=1)
          'Campaign' => string 'Back_Brace' (length=10)
  4 => 
    array (size=2)
      0 => 
        array (size=1)
          'Campaign' => string 'Home_Sec' (length=8)
      1 => 
        array (size=1)
          'Campaign' => string '' (length=0)
  5 => 
    array (size=1)
      0 => 
        array (size=1)
          'Campaign' => string 'home_Sec_2' (length=10)
  6 => 
    array (size=1)
      0 => 
        array (size=1)
          'Campaign' => string 'Burial_Ins' (length=10)
  7 => 
    array (size=0)
      empty
  8 => 
    array (size=0)
      empty
  9 => 
    array (size=0)
      empty

I dont want to use print_r want to control my array put all the value in normal array with unique values

3 Answers 3

2

array_walk is an option, but here's another option if you want to try something a bit more coded by yourself, solving this problem recursively

This will flatten any n-max level array into a single array that contains all the values of all the sub arrays (including the initial array itself)

<?php
$array = array(
1 => array(1, 2, 3, 4 => array(
1, 2, 3, 4
)),
4, 5);
function recurse_values($array) {
    if (is_array($array)) {
        $output_array = array();
        foreach ($array as $key=>$val) {
            $primitive_output = recurse_values($val);
            if (is_array($primitive_output)) {
                $output_array = array_merge($output_array, $primitive_output);
            }
            else {
                array_push($output_array, $primitive_output);
            }
        }
        return $output_array;
    }
    else {
        return $array;
    }
}
print_r(recurse_values($array));
?>

If you need unique values, at the end you can add a array_unique to do this.

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

1 Comment

thanks but the solution by @Machavity suites me more
1

You can use array_walk

$array = array(...); //your values here
function output($item, $key) {
     echo $key . ' =>' . $item;
}
array_walk($array, 'output');

2 Comments

its give me this value 0 =>Array1 =>Array2 =>Array3 =>Array4 =>Array5 =>Array6 =>Array7 =>Array8 =>Array9 =>Array
thanks for the clue your code is good but in my case need to use array_walk_recursive thanks any way
0

Are you asking how you can "flatten" this multi-dimensional array into a one dimension? Possible solutions to similar problems... How to Flatten a Multidimensional Array?

2 Comments

yes that will be one of the solution can tell me how
Edited with link to a similar problem. I personally like the second solution by "too much php"

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.