我有这样的json数据:
{
"no" : "",
"hs_code" : "01.05",
"uraian" : "Unggas hidup, yaitu ayam dari spesies Gallus",
"description" : "Live poultry, thatis to say, fowls of the ",
"bm" : ""
},
{
"no" : "",
"hs_code" : "",
"uraian" : "domestic us, bebek, an gs a, kalkun dan a yam guinea",
"description" : "species Gallus domesticus, ducks, geese,",
"bm" : ""
},
{
"no" : "",
"hs_code" : "",
"uraian" : "",
"description" : "turkeys and guinea fowls",
"bm" : ""
},
{
"no" : "",
"hs_code" : 0002"",
"uraian" : "ssss",
"description" : "ssss",
"bm" : ""
},
如何在 php 中使用这样的结果数据循环:
{
"no" : "",
"hs_code" : "01.05",
"uraian" : "Unggas hidup, yaitu ayam dari spesies Gallus domestic us, bebek, an gs a, kalkun dan a yam guinea",
"description" : "Live poultry, thatis to say, fowls of the species Gallus domesticus, ducks, geese turkeys and guinea fowls",
"bm" : ""
},
{
"no" : "",
"hs_code" : 0002"",
"uraian" : "ssss",
"description" : "ssss",
"bm" : ""
},
因此,如果“hs_code”索引没有值,则“uraian”和“description”索引将合并到“uraian”和“description”索引中,其 hs_code 索引具有值,请帮忙...我试试这个循环
$data = APPPATH."modules/masterCrud/seeder/hscode_2017_tes.json";
$sss = json_decode(file_get_contents($data));
$result = [];
$x = 0;
for($i = 0; $i < count($sss); $i++)
{
$x--;
$no = $sss[$i]->no;
$hs_code = $sss[$i]->hs_code;
if($sss[$i]->hs_code == '')
{
$sss[$i]->uraian .= $sss[$i]->uraian;
$sss[$i]->description .= $sss[$i]->description;
}
$uraian = $sss[$i]->uraian;
$description = $sss[$i]->description;
$bm = $sss[$i]->bm;
array_push($result,compact('no','hs_code','uraian','description','bm'));
}
print_r($result);
这没有用...
代码:
$result = [];
$x= 0;
for($i = 0; $i < count($sss); $i++) {
if($sss[$i]->hs_code) {
$x= $i;
$result[$i] = $sss[$i];
} else {
$result[$x]->uraian = $result[$x]->uraian . ' ' . $sss[$i]->uraian;
$result[$x]->description = $result[$x]->description . ' ' . $sss[$i]->description;
}
}
array_multisort($result, SORT_ASC);
print_r($result);
说明:
技巧:在每个hs_code
有价值的地方,将该位置保存为$x
by $i
。
因此,对于 empty hs_code
、 updateuraian
和description
.
$result will has difference in keys so user **array_multisort**
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句