Solución:
Simplemente decodifique su cadena json y luego use array push
$tempArray = json_decode($jsonstring, true);
array_push($tempArray, $your_data);
Para tu caso
$str="{
"players":[
{
"name":"Moldova",
"image":"/Images/Moldova.jpg",
"roll_over_image":"tank.jpg"
},
{
"name":"Georgia",
"image":"/Images/georgia.gif",
"roll_over_image":"tank.jpg"
} ]}";
$arr = json_decode($str, true);
$arrne['name'] = "dsds";
array_push( $arr['players'], $arrne );
print_r($arr);
Simplemente verifique el valor de print_r ($ arr); Espero que esto sea lo que quieres. 🙂
Agregar otro jugador
$tempArray = json_decode($inp, true);
array_push($tempArray['players'], array('name' => $data['username'], 'image' => $data['userimage'], 'roll_over_image' => 'tank.jpg'));
Actualizar partidos
matriz de primera coincidencia
$tempArray['games'][0]['matches'];
segunda matriz de coincidencias
$tempArray['games'][1]['matches'];
ahora son simples matrices bidimensionales con claves player1id
, player2id
y winner
– debería ser fácil actualizarlos. Después de eso, puede codificar el $tempArray
de vuelta a json.
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js" ></script>
</head>
<body>
<?php
//first copy your json data data.json
$str = file_get_contents('data.json');//get contents of your json file and store it in a string,bro small suggestion never keep any JSON data in ur html file its not safe.always keep json data in external file.
$arr = json_decode($str, true);//decode it
$arrne['players'] = "sadaadad";
$arrne['image'] = "sadaadad";
$arrne['roll_over_image'] = "sadaadad";
array_push( $arr['employees'], $arrne);//push contents to ur decoded array i.e $arr
$str = json_encode($arr);
//now send evrything to ur data.json file using folowing code
if (json_decode($str) != null)
{
$file = fopen('data.json','w');
fwrite($file, $str);
fclose($file);
}
else
{
// invalid JSON, handle the error
}
?>
</body>
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)