php从多维关联数组中获取所有值

时间:2015-09-16 21:39:48

标签: php multidimensional-array associative-array

如何从多维关联数组中获取所有值 我不想使用print_r想要控制我的数组将所有值放在普通数组中的唯一值  我的数组看起来像这样

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

我不想使用print_r想要控制我的数组将所有值放在普通数组中的唯一值

3 个答案:

答案 0 :(得分:1)

您可以使用array_walk

    <div class="body-container">
    <ul class="testimonial-holder">
        <li class="testimonial">Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more</li>
        <li class="testimonial">Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more</li>
        <li class="testimonial">Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more Lorem ipsum and more</li>
    </ul>
</div>

答案 1 :(得分:1)

array_walk是一个选项,但是如果你想尝试更多自己编码的东西,这是另一种选择,解决这个问题递归

这会将任何 n-max级别数组展平为包含所有子数组的所有值的单个数组(包括初始数组本身)

<?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));
?>

如果您需要唯一值,最后可以添加array_unique来执行此操作。

答案 2 :(得分:0)

您是否在问如何将这个多维阵列“扁平化”为一维?类似问题的可能解决方案...... How to Flatten a Multidimensional Array?