我有两个Json数组,如下所示:
$a='[{"type":"text","req":0,"name":"user"},{"type":"text","req":0,"name":"org"},'
. '{"type":"textarea","label":"Notes","req":0},'
. '{"type":"text","label":"text1","req":0},'
. '{"type":"textarea","label":"Notes","req":0},'
. '{"type":"text","label":"text2","req":0},'
. '{"type":"textarea","label":"Notes","req":1}]';
$b='[{"type":"textarea","label":"Notes","Element_Values":"331","Element_Name":"textarea-710091","Count_Images":0},'
. '{"type":"text","label":"text1","Element_Values":"1","Element_Name":"text-987351","Count_Images":0},'
. '{"type":"textarea","label":"Notes","Element_Values":"332","Element_Name":"textarea-254458","Count_Images":0},'
. '{"type":"text","label":"text2","Element_Values":"2","Element_Name":"text-3410","Count_Images":0},'
. '{"type":"textarea","label":"Notes","Element_Values":"333","Element_Name":"textarea-554051","Count_Images":0}]';
如您所见,数组“ a”以几个键开头,而这些键不在数组“ b”中。我想跳过具有“名称”键的阵列。
我做了以下代码,但是没有用:
$c = [];
$aJson=json_decode($a, true);
$bJson=json_decode($b, true);
foreach($aJson as $key => $array)
{
foreach($array as $an)
{
if(array_key_exists('name', $an))
{
//continue;
}
}
$c[$key] = array_merge($bJson[$key],$array);
}
echo json_encode($c);
结果数组c应该是:
[{"type":"textarea","label":"Notes","Element_Values":"331","Element_Name":"textarea-710091","Count_Images":0,"req":0},{"type":"text","label":"text1","Element_Values":"1","Element_Name":"text-987351","Count_Images":0,"req":0},{"type":"textarea","label":"Notes","Element_Values":"332","Element_Name":"textarea-254458","Count_Images":0,"req":0},{"type":"text","label":"text2","Element_Values":"2","Element_Name":"text-3410","Count_Images":0,"req":0},{"type":"textarea","label":"Notes","Element_Values":"333","Element_Name":"textarea-554051","Count_Images":0,"req":1}]
请帮助我
答案 0 :(得分:1)
这是一个简单的调试问题,$array
已经是数组。
$index = 0;
foreach($aJson as $key => $array)
{
if(isset($array['name']))
continue;
$c[$index] = array_merge($bJson[$index], $array);
$index++;
}