如何在PHP中将嵌套数组转换为Value?

时间:2015-07-26 07:51:53

标签: php arrays

我的阵列

libprotobuf-dev

我的PHP代码

$('.carousel-outer-wrapper').on('mousewheel', function(event) {
        var carousel = $('.carousel-wrapper');
        TweenLite.to(carousel, 1, {top: "+=50px"});
    });

我的错误是:

  

注意:数组转换为....

嵌套数组中的第一个值。

如何将嵌套数组转换为值?

我需要这样展示,

a:3:{s:6:"Choice";a:2:{i:0;s:5:"First";i:1;s:6:"Second";}s:5:"fcode";s:26:"form_rajas_exmsw2rpc81anlj";s:9:"useremail";s:26:"rajasekarang.cud@gmail.com";} array ( 'Choice' => array ( 0 => 'First', 1 => 'Second', ), 'fcode' => 'form_rajas_exmsw2rpc81anlj', 'useremail' => 'rajasekarang.cud@gmail.com', )

2 个答案:

答案 0 :(得分:1)

如果$value是一个数组,则需要一个嵌套循环。

foreach ($decode as $key => $value) {
    if (!is_array($value)) {
        $value = array($valule);
    }
    foreach ($value as $subvalue) {
        echo "<td width='100'>$subvalue</td>";
    }
}

如果你可以有多个级别的嵌套,你应该编写一个处理每个级别的递归函数。

如果您希望子数组显示为以逗号分隔的列表,则可以使用implode

foreach ($decode as $key => $value) {
    if (is_array($value)) {
        $value = implode(', ', $value);
    }
    echo "<td width='100'>$subvalue</td>";
}

答案 1 :(得分:0)

你有嵌套的数组,填充了不同大小的元素,所以你总会得到一个错误。所以在你的foreach循环中,你可以检查值是否是数组,例如

if (count($value)>1){
//code here like for instance echo $value[0]...
}//not recomended becuse an array can have only one value, but you can if you know you will put at least 2

if(is_array($value)..

我将执行以下操作:

foreach($thing as $thingy =>$that){
   //check if array or not
   if(is_array($that)){
       foreach($that as $t)
             //do whatever
   }else
      //do whatever
}