Home » Php » put array value of one file php into an other array of an other file

put array value of one file php into an other array of an other file

Posted by: admin February 25, 2020 Leave a comment

Questions:

I know there are a lot of similar question but I didn’t find how to do that.
I have one array in test.php

[
{
    "_id": "89",
    "name": "rfg",
    "author": "toto",
    "description": "",
    "status": "draft",
    "skills": [],
    "creationDate": "2008-07-17T07:49:05.000Z",
    "modificationDate": "2010-09-17T07:49:05.179Z"
}, ...

And I want to put all these value in an array “modules” wich are in final.php

[
{
    "_id": "89446259",
    "name": "something",
    "description": "coco",
    "programTemplate": "895",
    "author": "tutu",
    "tutors": [
        "titi"
    ],
    "startDate": "2018-10-05T13:49:00.000Z",
    "endDate": "2019-10-05T13:49:00.000Z",
    "skills": [],
    "modules": [
        {
            "_id": "89",
            "type": "course"
        },
        {
            "_id": "102",
            "type": "course"
        },
        {
            "type": "something",
            "name": "choose"
        }
    ]
}, ...

The result as I want is if Id is the same in final.php and in test.php so I put the value.
For example, if the value ‘_id’ in final.php is the same of the value ‘_id’ of test.php so ‘name’, ‘author’, ‘description’ ect will come in final.php.

Result I want :

[
{
    "_id": "89446259",
    "name": "something",
    "description": "coco",
    "programTemplate": "895",
    "author": "tutu",
    "tutors": [
        "titi"
    ],
    "startDate": "2018-10-05T13:49:00.000Z",
    "endDate": "2019-10-05T13:49:00.000Z",
    "skills": [],
    "modules": [
        {
            "_id": "89",
            "type": "course",
            "name": "rfg",
            "author": "toto",
            "description": "",
            "status": "draft",
            "skills": [],
            "creationDate": "2008-07-17T07:49:05.000Z",
            "modificationDate": "2010-09-17T07:49:05.179Z"
        },
        {
            "_id": "102",
            "type": "course"
        },
        {
            "type": "something",
            "name": "choose"
        }
    ]
}, ...

My PHP code in final.php

require_once('courses.php');

// initialisation
$results = curl_init( );

// configuration
//session
curl_setopt( $results, CURLOPT_URL, "https://ex.myapi.com/call_api" );
curl_setopt( $results, CURLOPT_RETURNTRANSFER, true );

// récupération du fichier
$result = curl_exec( $results );
$toto=json_decode($result);

//$objArray = json_decode($rows);

$newarray = array();
foreach ($toto as &$t){

    $modules=$t->modules;
    foreach ($modules as $m){
        $coco='787';    


        $m->testId= $val;
        if ($m->testId == $m->_id){
            $m->names = $name;
        $m->description = $desc;
        $m->author = $author;
        $m->status = $status;
        $m->creaDate = $creaDate;
        $m->modifDate = $modifDate; 


        // $m->toto = $coco;
        $newarray[] = $m;
    }

}
print_r($toto);
echo json_encode($toto);

curl_close( $results );

Someone can help me?

How to&Answers:

test.php:

$test1 = $decoded_json_object; // YOUR **DECODED** JSON OBJECT FROM test.php

final.php:

require_once( 'test.php' );

$test2 = $decoded_json_object_2 // YOUR **DECODED** JSON OBJECT FROM final.php

foreach( $test1 as &$course )  {
  foreach( $test2 as &$program )  {
    foreach( $program->modules as &$module ) {
      if( $module->_id == $course->_id ) {
        $module = (object) array_merge( (array) $module, (array) $course);
      }
    }
  }
}

$test2 = json_encode( $test2 );