【问题标题】:to print Multidimensional PHP array打印多维 PHP 数组
【发布时间】:2016-07-13 08:09:34
【问题描述】:
$arr_cont = array('type1' =>"Fruits",'f_div'=>
                    array(
                         
                         1 => "Apple",
                         2 => "Banana",
                         3 => "Mango",
                         4 => "Grapes",
                         )
                        ,
            'type2' => "colors",'c_div' =>
                    array (
                            1 => "Red",
                            2 => "Green",
                          )
                        ,
            'type3' => "Shapes",'s_div' =>
                    array(
                            1 => "Square",
                            2 => "Round",   
                            )
                        ,
            'type4' => "Flowers",'l_div' =>
                    array(
                            1 => "Rose",
                            2 => "Lily",
                        )
                    
            );

我有上面提到的数组,我想要下面的输出,请给我完整的 foreach 循环编码:

输出:

type1 : Fruits : f_div
1. Apple
2. Banana
3. Mango
4. Grapes
type2 : colors :c_div 
1. Red
2. Green
type3 : Shapes : s_div
1. Square
2. Round
type4 : Flowers : l_div
1. Rose
2. Lily

我已经尝试了下面提到的代码,但它给出了一些错误:

foreach($arr_cont as $val => $cont){
    print $val ." : " ;
    foreach($cont as $val1 => $id){
       print $id ." : ".$val1;
}

错误信息

Warning: Invalid argument supplied for foreach() in 

但它正在打印 $arr_cont 变量以及错误 错误在第二个 foreach 循环中

【问题讨论】:

  • 向我们展示你到目前为止已经开始了什么?
  • 不怎么样?我们不是免费的编码服务。
  • 两个 foreach .... 检查你的值是否为数组,如果为 true,则创建另一个循环,否则 echo val
  • 显示您尝试过的和无效的。是一个简单的数组迭代。如果您尝试了某项操作但不起作用,我们可以提供帮助,但如果您没有尝试任何操作,我们不会为您解决问题。

标签: php arrays


【解决方案1】:
<?php

$arr_cont = array(
    'type1' => "Fruits",
    'f_div' =>
    array(
        1 => "Apple",
        2 => "Banana",
        3 => "Mango",
        4 => "Grapes",
    )
    ,
    'type2' => "colors",
    'c_div' =>
    array(
        1 => "Red",
        2 => "Green",
    )
    ,
    'type3' => "Shapes",
    's_div' =>
    array(
        1 => "Square",
        2 => "Round",
    )
    ,
    'type4' => "Flowers",
    'l_div' =>
    array(
        1 => "Rose",
        2 => "Lily",
    )
);
$output = "";
foreach ($arr_cont as $val => $cont) {
    if (is_array($cont)) {
        $output .= "$val<br />";
        foreach ($cont as $contIndex => $contValue) {
            $output .= " $contIndex. $contValue<br />";
        }
    } else {
        $output .= "$val : $cont : ";
    }
}
echo $output;

输出:

type1 : Fruits : f_div
1. Apple
2. Banana
3. Mango
4. Grapes
type2 : colors : c_div
1. Red
2. Green
type3 : Shapes : s_div
1. Square
2. Round
type4 : Flowers : l_div
1. Rose
2. Lily

【讨论】:

    【解决方案2】:

    $output =null;

    foreach ($arr_cont as $val => $cont) {

    if (is_array($cont)) {
    
        $output .= $val.'<br/>';
        foreach ($cont as $contIndex => $contValue) {
            $output .=  $contIndex.' '. $contValue.'<br/>';
        }
    } else {
        $output .= $val .':'. $cont.':';
    }
    

    } 回声$输出;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多