Add New Data into PHP JSON String

Add new data into PHP JSON string

you need to json_decode($data) first, then add the new key/value, and json_encode() it.

How to insert a new key value to the existing JSON- PHP

Here is an example how you can do it:

Method 1 decoding it to an associative array:

$object = json_decode('{
"systemService":"1234",
"DATA_PLAN":"SMEF",
"amount":"5000"
}', true);

$object['message'] = "Your message";

echo json_encode($object); //this will give you the json string with your new property

Method 2 decoding it to an object:

$object = json_decode('{
"systemService":"1234",
"DATA_PLAN":"SMEF",
"amount":"5000"
}');

$object->message = "Your message";

echo json_encode($object); //this will give you the json string with your new property

Append data to top of the JSON file using PHP form

At beggining you have [1] then you insert [2] and after $array_data[] = $extra; goes to [1][2] and the you reverse array [2][1]. At this moment when you insert a new value you have [2][1][3] and the after reversing [3][1][2] the solution would be reversing before insert extra:

test.php:

    <?php
if(file_exists('wallpaper.json'))
{

$current_data = file_get_contents('wallpaper.json');
$array_data = json_decode($current_data, true);
$extra = array(
'name' => $_REQUEST['name'],
'author' => $_REQUEST["author"],
'category'=> $_REQUEST["category"],
'url' => $_REQUEST["url"]

);
echo $extra['name'];
$array_data = array_reverse($array_data);
$array_data[] = $extra;
$array_data = array_reverse($array_data);
$final_data = json_encode($array_data);
if(file_put_contents('wallpaper.json', $final_data))
{
$message = "<label class='text-success'>File Appended Success fully</p>";
}
}
?>

to run I used to pass parameters:

http://localhost/test.php?name=4&author=4&category=4&url=4

insert json data using php

You have a JSON in this format:

[
{"username": "abc", "email":"abc@gmail.com"},
{"username": "xyz", "email":"xyz@gmail.com"}
]

that, decoded, produce this array:

[0][ 'username'=>'abc', 'email'=>'abc@gmail.com' ]
[1][ 'username'=>'xyz', 'email'=>'xyz@gmail.com' ]

Your code:

$json[$user] = array("username" => $username, "email" => $email);

$user appears as not defined.

If you want simply add new user to JSON, modify above line in this way:

$json[] = array("username" => $username, "email" => $email);

Otherwise, if you want update an existent user or add it if it doesn't exist, you have first to search for the user existence:

$user = array_search( $username, array_column( $json, 'username' ) );

if( $user !== False ) $json[$user] = array("username" => $username, "email" => $email);
else $json[] = array("username" => $username, "email" => $email);

Edit:

On PHP < 5.5, array_column() is not available.

If you have PHP >= 5.3 you can use this alternative. On php < 5.3, you can use this syntax to create your own array_column:

if( ! function_exists( 'array_column' ) )
{
function array_column( $array, $column_name )
{
function get_column( &$element, $key, $column_name )
{
$element = $element[$column_name];
}
array_walk( $array, 'get_column', $column_name );
return $array;
}
}

  • Read more about array_column()
  • Read more about array_search()

Append data to JSON array using PHP

$accountData is an object, as it should be. Array access is not valid:

array_push($accountData->loginHistory, $newLoginHistory);
// or simply
$accountData->loginHistory[] = $newLoginHistory;

Append data to a .JSON file with PHP

$data[] = $_POST['data'];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);

How to add element to JSON object using PHP?

Simply, decode it using json_decode()

And append array to resulting array.

Again encode it using json_encode()

Complete code:

<?php
$arr = '[
{
"id":1,
"name":"Charlie"
},
{
"id":2,
"name":"Brown"
},
{
"id":3,
"name":"Subitem",
"children":[
{
"id":4,
"name":"Alfa"
},
{
"id":5,
"name":"Bravo"
}
]
},
{
"id":8,
"name":"James"
}
]';
$arr = json_decode($arr, TRUE);
$arr[] = ['id' => '9999', 'name' => 'Name'];
$json = json_encode($arr);

echo '<pre>';
print_r($json);
echo '</pre>';


Related Topics



Leave a reply



Submit