0

I have two Json arrays like below:

$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}]';

As you can see array 'a' starts with few keys which is not in array 'b'. I want to skip the arrays which has 'name' keys.

I did the following code, but didnt work:

  $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);

The result array c should be:

[{"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}]

Please help me

1 Answer 1

1

This is a simple debug problem, $array is already the array.

$index = 0;
foreach($aJson as $key => $array)
{
    if(isset($array['name'])) 
        continue;        

    $c[$index] = array_merge($bJson[$index], $array);
    $index++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just a comment, but in your desired result you have this included: {"type":"textarea","label":"Notes","Element_Values":"331","Element_Name":"textarea-710091","Count_Images":0,"req":0}. "req":0 is in there ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.