Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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_Replace_Indexing - Fatal编程技术网

如何在php中替换数组中索引处的值

如何在php中替换数组中索引处的值,php,arrays,json,replace,indexing,Php,Arrays,Json,Replace,Indexing,我试图做的是在用户名匹配的索引处替换JSON数组的值。我该怎么做呢?下面是我的PHP试用代码和JSON。使用array_替换是最好的方法还是应该采取不同的方法 <?php $user_with_game = file_get_contents("oneuser.json");//$_POST["userGames"]; //$user_with_game = stripslashes ($JSON); $user_decoded_JSON = json_decode($user_wit

我试图做的是在用户名匹配的索引处替换JSON数组的值。我该怎么做呢?下面是我的PHP试用代码和JSON。使用array_替换是最好的方法还是应该采取不同的方法

<?php 
 $user_with_game = file_get_contents("oneuser.json");//$_POST["userGames"];
//$user_with_game = stripslashes ($JSON);
$user_decoded_JSON = json_decode($user_with_game, true);

$JSON = file_get_contents("users.json");
$decoded_JSON = json_decode($JSON, true);

//print_r ($user_decoded_JSON);

foreach ($decoded_JSON['Users'] as $key => $value)
{
    if ( $value['username'] == $user_decoded_JSON['username'])
    {
        $decoded_JSON['Users'] = array_replace($decoded_JSON['Users'],      $user_decoded_JSON);
    }
}

//print_r ($decoded_JSON);
$encoded_JSON = json_encode( $decoded_JSON);
echo $encoded_JSON;
 ?>



**users.json**
{
"Users":[
  {
     "password":"glass",
     "gamelist":[
        {
           "platform":"xbox-360",
           "game":"bioshock infinite"
        },
        {
           "platform":"xbox-360",
           "game":"tomb raider"
        }
     ],
     "username":"dorinayres",
     "reviewerlist":[

     ]
  },
  {
     "password":"happy",
     "gamelist":[
        {
           "platform":"xbox-360",
           "game":"far cry 3"
        },
        {
           "platform":"xbox-360",
           "game":"terraria"
        }
     ],
     "username":"ian",
     "reviewerlist":[

     ]
  }
  ]
}


**oneuser.json**
 {
"password":"glass",
 "gamelist":[
 {
     "platform":"xbox-360",
     "game":"bioshock infinite"
  },
  {
     "platform":"xbox-360",
     "game":"tomb raider"
  }
],
"username":"dorinayres",
"reviewerlist":[

]
}

**users.json**
{
“用户”:[
{
“密码”:“玻璃”,
“游戏列表”:[
{
“平台”:“xbox-360”,
“游戏”:“生物冲击无限”
},
{
“平台”:“xbox-360”,
“游戏”:“古墓丽影”
}
],
“用户名”:“dorinayres”,
“审查名单”:[
]
},
{
“密码”:“快乐”,
“游戏列表”:[
{
“平台”:“xbox-360”,
“游戏”:“远距3”
},
{
“平台”:“xbox-360”,
“游戏”:“Terria”
}
],
“用户名”:“ian”,
“审查名单”:[
]
}
]
}
**oneuser.json**
{
“密码”:“玻璃”,
“游戏列表”:[
{
“平台”:“xbox-360”,
“游戏”:“生物冲击无限”
},
{
“平台”:“xbox-360”,
“游戏”:“古墓丽影”
}
],
“用户名”:“dorinayres”,
“审查名单”:[
]
}

foreach($decoded_JSON['Users'] as &$user){
    if($user['username'] == $user_decoded_JSON['username']){
        //do something directly on $user here and it will effect the actual array
    }
}
$decoded_JSON_tmp=$decoded_JSON;
foreach ($decoded_JSON['Users'] as $key => $value)
{
    if ( $value['username'] == $user_decoded_JSON['username'])
    {
        $decoded_JSON_tmp['Users'] = array_replace($decoded_JSON['Users'],      $user_decoded_JSON);
    }
}
$decoded_JSON=$decoded_JSON_tmp;