如何在PHP中显示循环结果

时间:2016-06-03 10:23:51

标签: php arrays zend-framework2

我有一个数组:

Array
(
    [0] => Array
        (
            [phappointment_id] => 69
            [phc_id] => 49
            [valuator] => 91140961
            [valuated] => 87101133
        )

    [1] => Array
        (
            [phappointment_id] => 70
            [phc_id] => 49
            [valuator] => 98160588
            [valuated] => 87101133
        )

    [2] => Array
        (
            [phappointment_id] => 71
            [phc_id] => 49
            [valuator] => 91140961
            [valuated] => 89140962
        )

    [3] => Array
        (
            [phappointment_id] => 72
            [phc_id] => 49
            [valuator] => 98160588
            [valuated] => 94160284
        )
)

我将这个数组循环到这段代码中:

for ($i = 0; $i < $length; $i++) {
    $dataValuated=$perf->loadValuated($post['valuate'][$i]['valuated']);
    $dataValuator=$perf->loadValuator($post['valuate'][$i]['valuator']);
}
$arrValuated=array();
$arrValuator=array();
foreach($dataValuator as $val){
   array_push($arrValuator, $val);
}
foreach($dataValuated as $val){
   array_push($arrValuated, $val);
}

但是当我得到结果时,我只找到1个结果,它必须是4个结果。

echo json_encode(数组(&#39;评估者&#39; =&gt; $ arrValuator,&#39;评估&#39; =&gt; $ arrValuated));

结果:

{"valuator":[{"valuator_name":"asad"}],"valuated":[{"valuated_name":"desan"}]}

如何解决?感谢

2 个答案:

答案 0 :(得分:1)

试试这个,您可以直接从$arrValuated

创建数组$arrValuator$post['valuate']
<?php 
$arrValuated=array();
$arrValuator=array();

foreach($post['valuate'] as $valuate )
{
    $arrValuated[] = $perf->loadValuated($valuate['valuated']);
    $arrValuator[] = $perf->loadValuator($valuate['valuator']);
}
?>

答案 1 :(得分:0)

可能是因为for循环中$ dataValuated和$ dataValuator后面缺少[]:

for ($i = 0; $i < $length; $i++) {
    $dataValuated[]=$perf->loadValuated($post['valuate'][$i]['valuated']);
    $dataValuator[]=$perf->loadValuator($post['valuate'][$i]['valuator']);
}

如果没有[],通过将新值替换为新值,将始终是一个结果。