Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将数组中的数据拆分为json中保存的行_Php_Arrays_Json - Fatal编程技术网

如何在php中将数组中的数据拆分为json中保存的行

如何在php中将数组中的数据拆分为json中保存的行,php,arrays,json,Php,Arrays,Json,我有这样一个数组: array{"obj_target1":"Test1","obj_count1":"3","remarks1":"Done", "obj_target2":"Test2","obj_count2":"3","remarks2":"Running", "obj_target3":"Test3","obj_count3":"3","remarks3":"Pending"} 我想在php?中将其显示为3行,希望我没有误解您的问题 <?php $json = '{"obj_t

我有这样一个数组:

array{"obj_target1":"Test1","obj_count1":"3","remarks1":"Done",
"obj_target2":"Test2","obj_count2":"3","remarks2":"Running",
"obj_target3":"Test3","obj_count3":"3","remarks3":"Pending"}

我想在
php

中将其显示为3行,希望我没有误解您的问题

<?php
$json = '{"obj_target1":"Test1","obj_count1":"3","remarks1":"Done", "obj_target2":"Test2","obj_count2":"3","remarks2":"Running", "obj_target3":"Test3","obj_count3":"3","remarks3":"Pending"}';
$array = json_decode($json,true);
$output = array();
for($i=1;$i<count($array)/3+1;$i++) {
    $tmp = array();
    $tmp['obj_target'] = (isset($array['obj_target'.$i]) ? $array['obj_target'.$i] : '');
    $tmp['obj_count'] = (isset($array['obj_count'.$i]) ? $array['obj_count'.$i] : '');
    $tmp['remarks'] = (isset($array['remarks'.$i]) ? $array['remarks'.$i] : '');
    $output[] = $tmp;
}

// print_r($output);
// // OR
// echo json_encode($output);

?>
    Obj_Target Obj_Count Remarks <? foreach($output as $x){ echo $x['obj_target'] . ' ' . $x['obj_count'] . ' ' . $x['remarks'];} ?>
<hr />
<table>
    <thead>
        <th>obj_target</th>
        <th>obj_count</th>
        <th>remarks</th>
    </thead>
    <tbody>
        <? foreach($output as $x): ?>
        <tr>
            <td><? echo $x['obj_target']; ?></td>
            <td><? echo $x['obj_count']; ?></td>
            <td><? echo $x['remarks']; ?></td>
        </tr>
        <? endforeach; ?>
    </tbody>
</table>

Obj_目标Obj_计数备注

目标 obj_计数 评论
希望你想要这样的东西。
在表格中显示

$json = '{"obj_target1":"Test1","obj_count1":"3","remarks1":"Done", "obj_target2":"Test2","obj_count2":"3","remarks2":"Running", "obj_target3":"Test3","obj_count3":"3","remarks3":"Pending"}';
$array = json_decode($json,true);
$output = array_chunk($array, 3, true);
echo "Obj_Target, Obj_Count, Remarks<br/>";
foreach ($output AS $dataArr) {
    echo implode(', ', $dataArr)."<br/>";
}
$json='{“obj_target1”:“Test1”、“obj_count1”:“3”、“remark1”:“Done”、“obj_target2”:“Test2”、“obj_count2”:“3”、“remark2”:“Running”、“obj_target3”:“Test3”、“obj_count3”:“3”、“remark3”:“Pending”};
$array=json_decode($json,true);
$output=array\u chunk($array,3,true);
回声“;
回应“Obj_TargetObj_COUNT备注”;
foreach($dataArr输出){
$dataval=数组_值($dataArr);
回显“{$dataval[0]}{$dataval[1]}{$dataval[2]}”;
}
回声“;
显示为字符串

$json = '[ { "obj_target": "Test1", "obj_count": "3", "remarks": "Done" }, { "obj_target": "Test2", "obj_count": "3", "remarks": "Running" }, { "obj_target": "Test3", "obj_count": "3", "remarks": "Pending" } ]';
$array = json_decode($json,true);
echo "<table border='1'>";
echo "<tr><td>Obj_Target</td><td>Obj_Count</td><td>Remarks</td></tr>";
foreach ($array AS $dataArr) {
    echo "<tr><td>{$dataArr['obj_target']}</td><td>{$dataArr['obj_count']}</td><td>{$dataArr['remarks']}</td></tr>";
}
echo "</table>";
$json='{“obj_target1”:“Test1”、“obj_count1”:“3”、“remark1”:“Done”、“obj_target2”:“Test2”、“obj_count2”:“3”、“remark2”:“Running”、“obj_target3”:“Test3”、“obj_count3”:“3”、“remark3”:“Pending”};
$array=json_decode($json,true);
$output=array\u chunk($array,3,true);
回显“Obj_目标,Obj_计数,备注
”; foreach($dataArr输出){ 回波内爆(“,”,$dataArr)。“
”; }
但我认为,如果json结构如下所示,它会更好

[ { “obj_目标”:“Test1”, “obj_计数”:“3”, “备注”:“完成” }, { “obj_目标”:“Test2”, “obj_计数”:“3”, “备注”:“正在运行” }, { “obj_目标”:“Test3”, “obj_计数”:“3”, “备注”:“待定” } ]

然后,显示屏将显示:-

$json='[{“obj_目标”:“Test1”,“obj_计数”:“3”,“备注”:“Done”},{“obj_目标”:“Test2”,“obj_计数”:“3”,“备注”:“Running”},{“obj_目标”:“Test3”,“obj_计数”:“3”,“备注”:“Pending”};
$array=json_decode($json,true);
回声“;
回应“Obj_TargetObj_COUNT备注”;
foreach($dataArr形式的数组){
echo“{$dataArr['obj_target']}{$dataArr['obj_count']}{$dataArr['comments']}”;
}
回声“;

发布您的预期输出解码后,如何将其打印在3行中我认为OP需要三个列,包括object\u标题、ob\u计数和备注。数据应该像这样对齐…我的预期输出是:Obj_目标Obj_计数备注Test1 3完成Test2 3运行Test3 3 Pendinghower答案应该是我可以添加更多数据并显示相应地rows@FarooqSheikh像什么!目标目标目标目标计数备注测试1 3完成测试2 3运行测试3 3等待所有人的回复和努力祝你长寿,并祝所有人幸福,尤其是@santhy。。。。谢谢,这对我有用。
$json = '{"obj_target1":"Test1","obj_count1":"3","remarks1":"Done", "obj_target2":"Test2","obj_count2":"3","remarks2":"Running", "obj_target3":"Test3","obj_count3":"3","remarks3":"Pending"}';
$array = json_decode($json,true);
$output = array_chunk($array, 3, true);
echo "Obj_Target, Obj_Count, Remarks<br/>";
foreach ($output AS $dataArr) {
    echo implode(', ', $dataArr)."<br/>";
}
$json = '[ { "obj_target": "Test1", "obj_count": "3", "remarks": "Done" }, { "obj_target": "Test2", "obj_count": "3", "remarks": "Running" }, { "obj_target": "Test3", "obj_count": "3", "remarks": "Pending" } ]';
$array = json_decode($json,true);
echo "<table border='1'>";
echo "<tr><td>Obj_Target</td><td>Obj_Count</td><td>Remarks</td></tr>";
foreach ($array AS $dataArr) {
    echo "<tr><td>{$dataArr['obj_target']}</td><td>{$dataArr['obj_count']}</td><td>{$dataArr['remarks']}</td></tr>";
}
echo "</table>";