简体   繁体   English

如何从阵列中返回所有结果?

[英]How can i return all the results from my array?

Array 
( 
    [edit] => true 
    [id] => 1 
    [type] => Array 
    ( 
        [0] => LC 
    ) 
    [userid] => 1 
    [norooms] => 1 
    [park] => Central 
    [start] => 09:00 
    [end] => 11:00 
    [length] => 2 
    [student] => 79 
    [status] => Rejected  
)
<?php
$posted_data = array();
if (!empty($_POST['edit'])) {
   $posted_data = json_decode($_POST['editVal'], true);
}
print_r ($posted_data);

foreach ($posted_data as $key => $value) {
    echo '<p>'.$key.'</p>';
    echo '<p>'.$value.'</p>';
}
?>

The array at the top is the jason_decode returned. 顶部的数组是返回的jason_decode However with my foreach function it does not display the first index of the array within the array. 但是,对于我的foreach函数,它不会显示数组中数组的第一个索引。 ie. 即。 ( [0] => LC ) . ( [0] => LC )

Where am I going wrong? 我要去哪里错了?

You need to build a recursive function, something like: 您需要构建一个递归函数,例如:

function print_recursively(array $array)
{
    foreach ($array as $key => $value) 
    {
        if(is_array($value))
        {
            print_recursively($value);
        }
        else
        {
            echo '<p>'.$key.'</p>';
            echo '<p>'.$value.'</p>';
        }
    } 
}

Tune it according to your needs. 根据您的需要进行调整。

If you know there is array hierarchy to one level only 如果您知道仅将数组层次结构设置为一个级别

Keep printing the values and if the value is an array using is_array .. Iterate again. 继续打印值,如果值是使用is_array ..的数组。则再次进行迭代。

  foreach($dataArray as $key =>$value){ 
  if(is_array($value)){
     foreach($value as $array2Data){
           echo  $array2Data; //you can use keys as well
      }
  }
  else 
      echo $value;
  } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM