I have a result from my MySQL DB that I'm json encoding in PHP, the result looks like
:
[
{
"id": "8488",
"name": "Tenby",
"area": "Area1"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1"
}
]
What I would like to do is to add a new key/value pair to that JSON so that it will be :
[
{
"id": "8488",
"name": "Tenby",
"area": "Area1",
"image": "1278.jpg"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1",
"image": "1279.jpg"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1",
"image": "1280.jpg"
}
]
So how can I do that in PHP?
<?php
$data[0]['id']="8488";
$data[0]['name']="Tenby";
$data[0]['area']="Area1";
$data[1]['id']="8489";
$data[1]['name']="Harbour";
$data[1]['area']="Area1";
$data[2]['id']="8490";
$data[2]['name']="Mobius";
$data[2]['area']="Area1";
echo json_encode($data)."<br/>";
/*Add Image element (or whatever) into the array according to your needs*/
$data[0]['image']="1278.jpg";
$data[1]['image']="1279.jpg";
$data[2]['image']="1280.jpg";
echo json_encode($data);
?>
PHP is not very good with JSON. Its best to convert from JSON to Array to do this - what #egig has also recommended.
Example code:
$temp = json_decode($json);
$temp[] = new data, whatever you want to add...;
$json = json_encode($temp);
Hope this helps.
hu?
the result of the db query will be an array or an object...
add the additional entry to that data, only encode after every necessary data manipulation is done
alternatives, but clumsy (horrible):
json_decode, add stuff, json_encode
build your additional data as a string, str_replace for example "area": "Area1" in your json string with "area": "Area1", "image": "1278.jpg"
but really:
output formatting like json_encode should only be done once you are sure that you have the whole output together and it is send out
Maybe things have changed since then, but I know this will work at this current stage:-
So here is the JSON string:-
$strJSON = '[
{
"id": "8488",
"name": "Tenby",
"area": "Area1"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1"
}
]';
If this is still a string, then I would convert it into an object like this:-
$json = json_decode( $strJSON );
Now that it is in object format, to add my "image" keys with their values, I would then add them like it is shown below:-
$json[0]->image = "1278.jpg";
$json[1]->image = "1279.jpg";
$json[2]->image = "1280.jpg";
So then if I add the code below after the above code:-
echo "<pre>";
print_r( $json );
echo "</pre>";
We should then see it outputting on the page like so:-
Array
(
[0] => stdClass Object
(
[id] => 8488
[name] => Tenby
[area] => Area1
[image] => 1278.jpg
)
[1] => stdClass Object
(
[id] => 8489
[name] => Harbour
[area] => Area1
[image] => 1279.jpg
)
[2] => stdClass Object
(
[id] => 8490
[name] => Mobius
[area] => Area1
[image] => 1280.jpg
)
)
//$index = some value in array;
for($i=0;$i<count($index);$i++){
$data[$i]->key = $index[$i];
}
print $data;
Related
This question already has answers here:
Get min and max value in PHP Array
(9 answers)
Closed 4 months ago.
I have the following array $data. n and 0 are identical, it's just the way the data is returned. I need to determine the max and min values for the id value, it looks like the id values are stored as strings, how is this achieved?
Array
(
[0] => stdClass Object
(
[n] => artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}
[0] => artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}
)
[1] => stdClass Object
(
[n] => artefact[10.4]{"id": "2", "name": "002", "type": "Artefact"}
[0] => artefact[10.4]{"id": "2", "name": "002", "type": "Artefact"}
)
[2] => stdClass Object
(
[n] => artefact[10.5]{"id": "3", "name": "003", "type": "Artefact"}
[0] => artefact[10.5]{"id": "3", "name": "003", "type": "Artefact"}
)
)
It seems that {"id":....} is a JSON value, and we need to parse the value and collect IDs. Before we need to clean artefact string with preg_replace function. In this case, we can determine max and min values:
<?php
$data = [
[
'0'=>'artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}',
'n'=>'artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}',
],
[
'0'=>'artefact[10.4]{"id": "1", "name": "001", "type": "Artefact"}',
'n'=>'artefact[10.4]{"id": "3", "name": "001", "type": "Artefact"}',
],
];
$ids = [];
foreach ($data as $datum) {
foreach ($datum as $item) {
$parsedItem = preg_replace("/artefact\[(.+?)\]/",'',$item);
$jsonDecoded = json_decode($parsedItem);
$ids[] = $jsonDecoded->id;
}
}
$maxId = $ids ? max($ids):0;
$minId = $ids ? min($ids):0;
echo "Max id: $maxId <br> Min id $minId ";
This is the data inside a variable named $data and i printed it using print_r().
Array
(
[user_id] => 6
[car_id] => 9
[pickup_name] => only me
[snooze] => 15
[pickup_loc] => pickup location
[drop_loc] => drop location
[note] => see you soon
[schedule] => [ {
"id": "1",
"car_name": "Mercedes-Benz C-Class",
"price": "200",
"status": "0",
"cr_dt": "2019-07-25 18:29:42",
"up_dt": "2019-07-26 11:20:36"
},
{
"id": "2",
"car_name": "Mercedes-Benz C-Class",
"price": "300",
"status": "0",
"cr_dt": "2019-07-25 18:29:42",
"up_dt": "2019-07-26 11:20:36"
} ]
[id] => 16
)
I want to access the data inside ['schedule'] in my foreach loop.
Thanks in advance.
You can use json_decode( $data['schedule]) directly. Otherwise you can use a variable like:
$jsarray = json_decode( $data['schedule])
Then you can access the data.
i'm trying to add value with key to exist array
this my array
{
"object": {
"USER_ID": "1",
"EMAIL": "abdabughazaleh#hotmail.com",
"FIRST_NAME": "abd",
"LAST_NAME": "abughazaleh",
"PICTURE": "images/users/xuhEzR6m4LvjCuKx1vAb.jpg"
}
}
with json result .
i need to add element like this :
{
"object": {
"USER_ID": "1",
"EMAIL": "abdabughazaleh#hotmail.com",
"FIRST_NAME": "abd",
"LAST_NAME": "abughazaleh",
"PICTURE": "images/users/xuhEzR6m4LvjCuKx1vAb.jpg",
"new_key": "new_value"
}
}
when i trying this :
array_push($ar['object'],array('new_key'=>'new_value'));
and this :
$ar['object']['new_key'] = 'new_value';
problem not solved for me :(
This is not an array, this is json:
{
"object": {
"USER_ID": "1",
"EMAIL": "abdabughazaleh#hotmail.com",
"FIRST_NAME": "abd",
"LAST_NAME": "abughazaleh",
"PICTURE": "images/users/xuhEzR6m4LvjCuKx1vAb.jpg"
}
}
First, convert json into associative array with json_decode:
$json = '{
"object": {
"USER_ID": "1",
"EMAIL": "abdabughazaleh#hotmail.com",
"FIRST_NAME": "abd",
"LAST_NAME": "abughazaleh",
"PICTURE": "images/users/xuhEzR6m4LvjCuKx1vAb.jpg"
}
}';
$json = json_decode($json, true); //true second parameter is actully for converting json into associative array
Than add a value to newly created array:
$json['object']['key'] = 'value';
Result of print_r($json):
Array
(
[object] => Array
(
[USER_ID] => 1
[EMAIL] => abdabughazaleh#hotmail.com
[FIRST_NAME] => abd
[LAST_NAME] => abughazaleh
[PICTURE] => images/users/xuhEzR6m4LvjCuKx1vAb.jpg
[key] => value
)
)
Than convert back to json:
$json = json_encode($json);
Reult of echo $json;:
{
"object":{
"USER_ID":"1",
"EMAIL":"abdabughazaleh#hotmail.com",
"FIRST_NAME":"abd",
"LAST_NAME":"abughazaleh",
"PICTURE":"images\/users\/xuhEzR6m4LvjCuKx1vAb.jpg",
"key":"value"
}
}
I solve it using this code ,
<?php $array->object->new_key ='new_value'; ?>
I have read lots of answers on SO, but I cannot figure out how to make them work with my problem.
This is what I have:
{
"name": "My Company LLC ->",
"children": [
{
"name": "District of the Stores",
"children": [
{
"name": "johnny1"
},
{
"name": "jonny2"
}
]
}, //I don't want my array to end here
{
"name": "store number 10",
"children": [
{
"name": "johnny3"
},
{
"name": "jonny4"
}
]
}
]
}
This is what I want.
{
"name": "My Company LLC ->",
"children": [
{
"name": "District of the Stores",
"children": [
{
"name": "johnny1"
},
{
"name": "jonny2"
},
{
"name": "store number 10",
"children": [
{
"name": "johnny3"
},
{
"name": "jonny4"
}
]
}
]
}
]
}
Here is what I tried to do it with:
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'))));
$name['children'][]=array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'jonny4')));
echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';
I know that I am inserting an array at the end of the children array which is causing my second array problem in the first example, but I don't know how to put the second children array back into the first one back in the original. This is part of a big list from a database, but I'm stuck here. I cannot see where the SO answers I've found help me to insert the second array.
It looks like you want this:
$name['children'][0]['children'][] = array(...);
This would reference the first child of the root element (index 0), then proceed like you did previously by adding to that elements children array.
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'), array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4'))))));
echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';
Here is the output
{
"name": "My Company LLC ->",
"children": [
{
"name": "District of the Stores",
"children": [
{
"name": "johnny1"
},
{
"name": "jonny2"
},
{
"name": "store number 10",
"children": [
{
"name": "johnny3"
},
{
"name": "johnny4"
}
]
}
]
}
]
}
If you want to keep the lines of code separate you can do the following:
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'))));
$name['children'][0]['children'][] = array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4')));
echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';
Also, it is probably a lot easier to debug this sort of thing by looking at the array instead of encoding into json. here is your original output as an array, which really shows you what's going on better.
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'))));
$name['children'][]=array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'jonny4')));
echo '<pre>';
print_r($name);
echo '</pre>';
And the output is:
Array
(
[name] => My Company LLC ->
[children] => Array
(
[0] => Array
(
[name] => District of the Stores
[children] => Array
(
[0] => Array
(
[name] => johnny1
)
[1] => Array
(
[name] => jonny2
)
)
)
[1] => Array
(
[name] => store number 10
[children] => Array
(
[0] => Array
(
[name] => johnny3
)
[1] => Array
(
[name] => jonny4
)
)
)
)
)
I want to tranform an php array into an json string to use with JavaScript InfoVis Toolkit formart.
the objective:
InfoVis Demo Tree
Json specification format:
InfoVis-loading and serving JSON data
I have this php array: $my_array :
Array
(
[item_1] => Array
(
[id] => item_1_ID
[name] => item_1_NAME
[data] => item_1_DATA
[children] => Array
(
[door] => Array
(
[id] => door_ID
[name] => door_NAME
[data] => door_DATA
[children] => Array
(
[mozart] => Array
(
[id] => mozart_ID
[name] => mozart_NAME
[data] => mozart_DATA
[children] => Array
(
[grass] => Array
(
[id] => grass_ID
[name] => grass_NAME
[data] => yes
)
[green] => Array
(
[id] => green_ID
[name] => green_NAME
[data] => no
)
[human] => Array
(
[id] => human_ID
[name] => human_NAME
[data] => human_DATA
[children] => Array
(
[blue] => Array
(
[id] => blue_ID
[name] => blue_NAME
[data] => blue_DATA
[children] => Array
(
[movie] => Array
(
[id] => movie_ID
[name] => movie_NAME
[data] => yes
)
)
)
)
)
)
)
)
)
[beat] => Array
(
[id] => beat_ID
[name] => beat_NAME
[data] => yes
)
[music] => Array
(
[id] => music_ID
[name] => music_NAME
[data] => no
)
)
)
)
now if I json_encode($my_array);
{
"item_1": {
"id": "item_1_ID",
"name": "item_1_NAME",
"data": "item_1_DATA",
"children": {
"door": {
"id": "door_ID",
"name": "door_NAME",
"data": "door_DATA",
"children": {
"mozart": {
"id": "mozart_ID",
"name": "mozart_NAME",
"data": "mozart_DATA",
"children": {
"grass": {
"id": "grass_ID",
"name": "grass_NAME",
"data": "yes"
},
"green": {
"id": "green_ID",
"name": "green_NAME",
"data": "no"
},
"human": {
"id": "human_ID",
"name": "human_NAME",
"data": "human_DATA",
"children": {
"blue": {
"id": "blue_ID",
"name": "blue_NAME",
"data": "blue_DATA",
"children": {
"movie": {
"id": "movie_ID",
"name": "movie_NAME",
"data": "yes"
}
}
}
}
}
}
}
}
},
"beat": {
"id": "beat_ID",
"name": "beat_NAME",
"data": "yes"
},
"music": {
"id": "music_ID",
"name": "music_NAME",
"data": "no"
}
}
}
}
But to InfoVis the current json output (json_encode($my_array)) has 3 problems:
is not using [ ]
the 'children' arrays have the key names
arrays items is with their key names -> example: "item_1": { ....
let me point the problem so maybe you can help with an function to transform this json string:
see this slice of json_encode($my_array) output:
{
"item_1": {
"id": "item_1_ID",
"name": "item_1_NAME",
"data": "item_1_DATA",
"children": {
"door": {
"id": "door_ID",
1. problem 1:
{
"item_1": {
we have to remove those keys like: "item_1":
2. problem 2:
"children": {
"door": {
"id": "door_ID",
the correct code for this should be:
"children": [
{
"id": "door_ID",......
"door": was removed... because it`s a key
"children": { => becomes" "children": [
An working example of 'children':
"children": [
{
"id": "grass_ID",
"name": "grass_NAME",
"data": "yes"
},
{
"id": "green_ID",
"name": "green_NAME",
"data": "no"
}
]
to clarify an complete example of WORKING Json InfoVis format:
json = {
id: "node02",
name: "0.2",
children: [{
id: "node13",
name: "1.3",
children: [{
id: "node24",
name: "2.4"
}, {
id: "node222",
name: "2.22"
}]
}, {
id: "node125",
name: "1.25",
children: [{
id: "node226",
name: "2.26"
}, {
id: "node237",
name: "2.37"
}, {
id: "node258",
name: "2.58"
}]
}, {
id: "node165",
name: "1.65",
children: [{
id: "node266",
name: "2.66"
}, {
id: "node283",
name: "2.83"
}, {
id: "node2104",
name: "2.104"
}, {
id: "node2109",
name: "2.109"
}, {
id: "node2125",
name: "2.125"
}]
}, {
id: "node1130",
name: "1.130",
children: [{
id: "node2131",
name: "2.131"
}, {
id: "node2138",
name: "2.138"
}]
}]
};
is it clear to understand?
Hope anyone can help me.. I'm working on this for days!
thank you.
Try this quickie conversion function
function fixit($yourArray) {
$myArray = array();
foreach ($yourArray as $itemKey => $itemObj) {
$item = array();
foreach ($itemObj as $key => $value) {
if (strtolower($key) == 'children') {
$item[$key] = fixit($value);
} else {
$item[$key] = $value;
}
}
$myArray[] = $item;
}
return $myArray;
}
$fixed = fixit($my_array);
$json = json_encode($fixed);
PHP doesn't differentiate between arrays (numeric keys) and associative arrays (string keys). They're all just Arrays. Javascript DOES differentiate. Since you're using string keys, they HAVE to be done as objects ({}) in JS.
You can't tell json_encode to ignore the keys in an array (e.g. your 'children' sub-arrays). That'd mean the produced JSON is NOT the same as the original PHP structure - you've now changed key names.
You'd have to process your array and convert all those children sub-array keys to numbers:
grass -> 0
green -> 1
etc...
so that json-encode could see that it's a numerically keyed PHP array, meaning it'll produce an actual javavscript array ([]), and not an object ({}).
The alternative is writing your own JSON-encoder to do this on-the-fly for you.
This is documented behaviour. An associative array will produce an object literal when JSON stringified with json_encode. Update your original array structure to represent the outcome you want, instead of mangling the produced JSON representation, or wrap your own solution around json_encode for each object.
Edit: attempting a cleanup operation
The code
$original = <your original data-array>; // assumed, I reversed your encoded JSON as test data
// Start by stripping out the associative keys for level 1
$clean = array_values($original);
// Then recursively descend array, and do the same for every children-property encountered
function &recursiveChildKeysCleaner(&$arr) {
// If $arr contains 'children'...
if (array_key_exists('children', $arr)) {
/// ...strip out associative keys
$arr['children'] = array_values($arr['children']);
// ...and descend each child
foreach ($arr['children'] as &$child) {
recursiveChildKeysCleaner($child);
}
}
return $arr;
}
foreach ($clean as &$item) {
recursiveChildKeysCleaner($item);
}
unset($item);
echo json_encode($clean);
Output
[{
"id": "item_1_ID",
"name": "item_1_NAME",
"data": "item_1_DATA",
"children": [{
"id": "door_ID",
"name": "door_NAME",
"data": "door_DATA",
"children": [{
"id": "mozart_ID",
"name": "mozart_NAME",
"data": "mozart_DATA",
"children": [{
"id": "grass_ID",
"name": "grass_NAME",
"data": "yes"
},
{
"id": "green_ID",
"name": "green_NAME",
"data": "no"
},
{
"id": "human_ID",
"name": "human_NAME",
"data": "human_DATA",
"children": [{
"id": "blue_ID",
"name": "blue_NAME",
"data": "blue_DATA",
"children": [{
"id": "movie_ID",
"name": "movie_NAME",
"data": "yes"
}]
}]
}]
}]
},
{
"id": "beat_ID",
"name": "beat_NAME",
"data": "yes"
},
{
"id": "music_ID",
"name": "music_NAME",
"data": "no"
}]
}]