Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将一个文件的数组值放入另一个文件的另一个数组中_Php_Arrays_Json - Fatal编程技术网

Php 将一个文件的数组值放入另一个文件的另一个数组中

Php 将一个文件的数组值放入另一个文件的另一个数组中,php,arrays,json,Php,Arrays,Json,我知道有很多类似的问题,但我不知道怎么做。 我在test.php中有一个数组 [ { "_id": "89", "name": "rfg", "author": "toto", "description": "", "status": "draft", "skills": [], "creationDate": "2008-07-17T07:49:05.000Z", "modificationDate": "2010-09-17T0

我知道有很多类似的问题,但我不知道怎么做。 我在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"
}, ...
我想把所有这些值放在final.php中的数组“modules”中

[
{
    "_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"
        }
    ]
}, ...
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 );
我想要的结果是,如果final.php和test.php中的Id相同,那么我就输入值。 例如,如果final.php中的值“_id”与test.php中的值“_id”相同,那么“name”、“author”、“description”等将出现在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",
            "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"
        }
    ]
}, ...
final.PHP中的我的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"
        }
    ]
}, ...
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 );
有人能帮我吗

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

如果id相同,请更好地解释一下发生了什么。我编辑了我的问题,希望它很清楚我不知道为什么,但它不起作用。我忘了告诉大家我从2API中将它们恢复到数组,我不能将它们放在文件中。这两个数组是json对象。你能展示你的API调用吗?有错误吗?我没有错误,我用cURL调用它们,当我打印“新json”时,我只是说“这个页面不工作,请在你的问题中添加完整的代码。你可以删除api键等等。你是否正确地重命名了vars$test1和$test2?”?