Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How insert Multiple Array item into One Array in PHP

i want to add multiple Array item into One Array Object in PHP

i have array like below :


    [
        {
            "name": "AAA"
        },
        {
            "family": "BBB"
        },
        {
            "job": "CCC"
        }
    ]

And i need to Convert like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "name": "AAA",
    "family": "BBB",
    "job": "CCC"
}

Array Data maybe changed , but , i write this code for explain my problem :

$RetArray=array();
    $Array_Test=array(array('name'=>'AAA'),array('family'=>'BBB'),array('job'=>'CCC'));
    foreach ($Array_Test as $json_item){
       foreach ($json_item as $key=>$value){
       array_push($RetArray,array($key => $value));
     }
    }
    echo json_encode($RetArray);

But this code returns the same as the first array!
I want to return every item into one array.

>Solution :

Try this:

$RetArray=[];
$SrcArray=[
    ["name"=>"AAA"],
    ["family"=>"BBB"],
    ["job"=>"CCC"],
];
foreach($SrcArray as $item){
    $RetArray=array_merge($RetArray,$item);
}
echo json_encode($RetArray);

Here is what it got: https://3v4l.org/kZJ2T

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading