Parse JSON from file using PHP from objects/arrays - php

I get the following output using print_r in php of my decoded JSON:
stdClass Object
(
[assignments] => Array
(
[0] => stdClass Object
(
[assignmentid] => 1
[grades] => Array
(
[0] => stdClass Object
(
[id] => 1
[userid] => 3
[attemptnumber] => 0
[timecreated] => 1484244192
[timemodified] => 1484244203
[grader] => 2
[grade] => 85.00000
)
)
)
)
[warnings] => Array
(
)
)
I want to get the value for [grade] => 85.00000 and store it in a variable. How would I go about doing this?

What about:
$var = $obj->assignments[0]->grades[0]->grade;

Use true as second parameter of json_decode() to decode it to array and then you have to loop over your result.
$data = json_decode($json, true);
foreach ($data['assignments'] as $row) {
foreach ($row['grades'] as $grade) {
echo $grade['grade'];
}
}

Related

Adding JSON stored in database to JSON string

I have some JSON stored in a database. Please assume I have a good reason for doing so. I then retrieved it from the DB and wish to include it in another JSON string. The following treats the JSON from the DB as standard strings instead of JSON as I desire. Will I need to loop over $json_from_db and first convert the cmd values to arrays/objects, or is there a better way?
<?php
$json_from_db=array(
array('id'=>1,'cmd'=>'{"a":{}}'),
array('id'=>3,'cmd'=>'{"b":{"name":"x","value":"xxx"}}'),
array('id'=>5,'cmd'=>'{"b":{"name":"y","value":"yyy"}}'),
array('id'=>9,'cmd'=>'{"c":{"name":"z","extra":"hello","more"=>1}}'),
);
$arr=array('a'=>"hello",'json'=>array('a'=>"hello"),'json_from_db'=>$json_from_db);
$json=json_encode($arr);
echo($json."\n\n");
print_r(json_decode($json));
OUTPUT
{"a":"hello","json":{"a":"hello"},"json_from_db":[{"id":1,"cmd":"\"a\":{}"},{"id":3,"cmd":"\"b\":{\"name\":\"x\",\"value\":\"xxx\"}"},{"id":5,"cmd":"\"b\":{\"name\":\"y\",\"value\":\"yyy\"}"},{"id":9,"cmd":"\"c\":{\"name\":\"z\",\"extra\":\"hello\",\"more\"=>1}"}]}
stdClass Object
(
[a] => hello
[json] => stdClass Object
(
[a] => hello
)
[json_from_db] => Array
(
[0] => stdClass Object
(
[id] => 1
[cmd] => "a":{}
)
[1] => stdClass Object
(
[id] => 3
[cmd] => "b":{"name":"x","value":"xxx"}
)
[2] => stdClass Object
(
[id] => 5
[cmd] => "b":{"name":"y","value":"yyy"}
)
[3] => stdClass Object
(
[id] => 9
[cmd] => "c":{"name":"z","extra":"hello","more"=>1}
)
)
)
Note that "b":{"name":"x","value":"xxx"} isn't valid json.
You need to decode all cmd's json-string from $json_from_db,
array_walk_recursive($json_from_db, function (&$item, $key) {
if ($key === 'cmd') {
$item = json_decode($item, true);
}
});
Then you can do
$arr = array(
'a' => "hello",
'json' => array('a'=>"hello"),
'json_from_db' => $json_from_db
);
$json = json_encode($arr);
Demo.

Array format Conversion in PHP

I have got an array which looks like the following:
Array ( [0] => Array ( [id] => 0 ) [1] => Array ( [id] => 0 ) [2] => Array ( [id] => 0 ) [3] => Array ( [id] => 0 ) [4] => Array ( [id] => 0 ) [5] => Array ( [id] => 0 ) [6] => Array ( [id] => 0 ) [7] => Array ( [id] => 0 ) [8] => Array ( [id] => 0 ) [9] => Array ( [id] => 0 ) [10] => Array ( [id] => 0 ) [11] => Array ( [id] => 4 ) [12] => Array ( [id] => 1 ) [13] => Array ( [id] => 2 ) [14] => Array ( [id] => 0 ) [15] => Array ( [id] => 0 ) [16] => Array ( [id] => 5 ) [17] => Array ( [id] => 1 ) )
and I would like to convert it into the following format:
[0,0,0,0,0,0,0,0,0,0,0,4,1,2,0,0,5,1]
How can I achieve this conversion using php?
All the help is appreciated.
PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column():
$result = array_column($array, 'id');
Simplest way: (assuming that you want it to actually output the string [5,6,...,2]
<?php
$ids=array();
foreach($yourArray as $k=>$v){
$ids[] = $v['id'];
}
$resultCsv = implode(',', $ids);
$result = '['.$resultCsv.']';
echo $result
?>
You can use array_map or array_walk:
With array_map:
$new = array_map(function ($value) { return $value['id']; }, $theArray);
print_r($new);
With array_walk:
array_walk($theArray, function (&$value, $key) { $value = $value['id']; };
print_r($theArray);
The main differences between these is that array_map produces a completely new array, and you dont get the key passed to the callback function. array_walk on the other hand works on the array by reference and the callback also gets the key.
Now its unclear to me whether you wanted the array restructured (as i provided solutions for) or if you want it in a specific string format like the [0,1,2,3,...] you provided. If you did want it in a string format all you need to do now is call json_encode on it:
// note if you used array_map pass the variable that holds the result of the call
// (in my example that would be $new); If you used array_walk then pass the same array
// you supplied as the argument to the call (in my exmaple that would be $theArray)
$arrString = json_encode($theArray);
You can process every member of the array and append every currently processed sub-array's member "id" to a new array:
<?php
// code
foreach $array as $member {
$newarray[]=$member["id"];
}
// code
?>

Merge arrays in an object

Im new to php and json. can you please suggest me on how to get the my desired output.
JSON File:
{
"1415772360":[
{"apple":"0"},
{"mango":"0"},
{"grapefruit":"0"},
{"melons":"12"},
{"peaches":"2"},
{"banana":"1"}
],
"1415772420":[
{"apple":"0"},
{"mango":"0"},
{"grapefruit":"0"},
{"melons":"7"},
{"peaches":"1"},
{"banana":"1"}
]
}
Desired Output
[
{
"minute":"1415772360",
"apple":"0",
"mango":"0",
"grapefruit":"0",
"melons":"12",
"peaches":"2",
"banana":"1”
},
{
"minute":"1415772420",
"apple:"0",
"mango":"0",
"grapefruit":"0",
"melons":"7",
"peaches":"1",
"banana":"1”
}
]
How can I do this in PHP?
I really appreciate your help. Thanks.
I would give json_decode a try. It won't get your desired output, but it will create an array from your JSON.
Documentation: http://php.net/manual/en/function.json-decode.php
My test:
$json = "{\"1415772360\":[{\"apple\":\"0\"},{\"mango\":\"0\"},{\"grapefruit\":\"0\"},
{\"melons\":\"12\"},{\"peaches\":\"2\"},{\"banana\":\"1\"}], \"1415772420\":
[{\"apple\":\"0\"},{\"mango\":\"0\"},{\"grapefruit\":\"0\"},{\"melons\":\"7\"},
{\"peaches\":\"1\"},{\"banana\":\"1\"}]}";
$new = json_decode($json);
print_r($new);
Output:
stdClass Object ( [1415772360] => Array ( [0] => stdClass Object ( [apple] => 0 )
[1] => stdClass Object ( [mango] => 0 ) [2] => stdClass Object ( [grapefruit] => 0 )
[3] => stdClass Object ( [melons] => 12 ) [4] => stdClass Object ( [peaches] => 2 )
[5] => stdClass Object ( [banana] => 1 ) ) [1415772420] => Array ( [0] => stdClass Object ( [apple] => 0 )
[1] => stdClass Object ( [mango] => 0 ) [2] => stdClass Object ( [grapefruit] => 0 )
[3] => stdClass Object ( [melons] => 7 ) [4] => stdClass Object ( [peaches] => 1 )
[5] => stdClass Object ( [banana] => 1 ) ) )
tyteen4a03 is correct that this just requires some looping to re-write the structure. This would be done as follows:
// Original JSON string
$json = '{
"1415772360":[
{"apple":"0"},
{"mango":"0"},
{"grapefruit":"0"},
{"melons":"12"},
{"peaches":"2"},
{"banana":"1"}
],
"1415772420":[
{"apple":"0"},
{"mango":"0"},
{"grapefruit":"0"},
{"melons":"7"},
{"peaches":"1"},
{"banana":"1"}
]
}';
// Convert JSON to array
$content = json_decode($json);
// Create new array container
$crate = array();
// Get minutes
foreach ($content AS $minute => $fruitbasket) {
$tmp = new stdClass;
$tmp->minutes = $minute;
// Get array of objects
foreach ($fruitbasket AS $fruits)
{
// Get object element and value
foreach ($fruits AS $key => $value)
{
// add to temporary object
$tmp->$key = $value;
}
}
// write to new array
$crate[] = $tmp;
}
print(json_encode($crate));

How to echo objects as string which is stored in an array?

I want to echo cer_type from this array
Array
(
[0] => stdClass Object
(
[id] => 1
[cer_type] => S.L.C.
)
[1] => stdClass Object
(
[id] => 3
[cer_type] => Intermediate
)
)
Try this:
<?php
echo $array[0]->cer_type;
// or if you want to loop through the array:
foreach ($array as $element) {
echo $element->cer_type;
}

how to extract the exact data from an array in php

i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}

Categories