i have a Two fields 1. allowances which is contain this data
{"medical":"600","transport":"350","food":"900"}
and another one 2. house rent which is contain this data
2550.00
now i want to get a result in third column like this
{"medical":"600","transport":"350","food":"900","house_rent":"2550.00"}
so far i tried this
$allowances=json_decode($salary->allowances);
$house_rent = array('House Rent' => $salary->house_rent);
$allowances_logs=array_push($allowances,$house_rent);
$salary->allowances_logs = $allowances_logs;
but it gives me following error"array_push() expects parameter 1 to be array, object given". Help me achieve this result. Any help will be appreciated
First, add true as second argument to json_decode(), and you will retrieve the results as an array instead of an object.
Second, with the two arrays, do:
$merged = array_merge($arr1, $arr2);
true Add second argument in json_decode(), so you can see below example
$mainArr = json_decode('{"medical":"600","transport":"350","food":"900"}',true);
$house_rent = array('House Rent' => 2550.00);
$printArr = array_merge($mainArr, $house_rent);
print_r(json_encode($printArr));
Output
{"medical":"600","transport":"350","food":"900","House Rent":2550}
First convert your json to array.
You can do this with json_decode php function.
json_decode function convert your json to object or associative array.
First argument on this function is the json string being decoded.
When is second argument are TRUE, returned objects will be converted into associative arrays.
$testJson = '{"medical":"600","transport":"350","food":"900"}';
$testArr = json_decode($testJson, true);
Output - Array ( [medical] => 600 [transport] => 350 [food] => 900 )
Now you can add new item to your array.
$testArr['house_rent'] = '2550.00';
Your array now look like this.
Array ([medical] => 600 [transport] => 350 [food] => 900 [house_rent] => 2500.00)
At the end you convert your array to json. For this you can use php json_encode function.
$testJson = json_encode($testArr);
Full Example
$testJson = '{"medical":"600","transport":"350","food":"900"}';
$testArr = json_decode($testJson, true);
$testArr['house_rent'] = '2500.00';
$testJson = json_encode($testArr);
echo $testJson;
Have tried a number of permutations to get a value from a PHP session. The session is an array of objects I think with key value pairs. This is the structure as outputted by a key/value foreach loop
Array
(
[laboratory_roster] => Array
(
[employee_entrance] => stdClass Object
(
[step] => employee_entrance
[employee_first_name] => asdfasd
[employee_last_name] => fasdfasdfv
[employee_access_code] => valid
[employee_email] => blah#blah.com
[employee_state_origin] => NY
[employee_kit_for_whom] => employee_kit_for_employee
)
)
)
This is the foreach loop that I wrote to display the output above:
foreach($_SESSION['laboratory_roster']['employee_entrance'] as $key=>$value)
{
// and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
What I wish to do is simply assign the value of the innermost value to a variable. Nothing works. Have tried:
$first_name = $_SESSION['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['employee_entrance'][1];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'][0];
Nothing works! It's probably so simple how to get the innermost value into a PHP variable, but I am not getting it. Thanks for help!
Two ways:
1 Convert to stdClass: $stdClass = json_decode(json_encode($_SESSION)); Then access with [].
2 Convert to array: $array = json_decode(json_encode($_SESSION), true); Then access with ->.
Try:
$first_name = $_SESSION['laboratory_roster']['employee_entrance']->employee_first_name;
As employee_entrance is an object (instance of stdClass) and not an array.
I have this JSON array data:
[{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Mondsee","price":"1.309"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Innerschwand","price":"1.321"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"St. Gilgen","price":"1.317"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"}]
And I am not sure how to properly get the region and price data out. I tried this:
$array = json_encode($jsondata);
$json = json_decode($array, true);
echo $json['region'];
echo $json['price'];
But nothing appears. Any suggestions?
try this:
$array = json_encode($jsondata);
$json = json_decode($array, true);
foreach($json as $data){
echo $data['region'] . "<br>";
echo $data['price'] . "<br>";
}
The json_encode isn't needed, as you say - you already have that JSON data. Simply decode it, then loop over it. You are currently trying to access the first entry, but the conversion from JSON to PHP will add numeric indexes/array keys to the PHP array which don't exist in the JSON.
foreach over the array:
$jsondata = '[{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Mondsee","price":"1.309"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Innerschwand","price":"1.321"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"St. Gilgen","price":"1.317"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"}]';
$json = json_decode($jsondata, true);
foreach($json as $each)
echo $each['region'] . ': ' . $each['price'] . PHP_EOL;
Example
Edit: a print_r of the decoded array (as mentioned by Marcin Orlowski) would show you its structure in PHP, similar to this:
Array
(
[0] => Array
(
[region] => Abersee
[price] => 1.298
)
[1] => Array
(
[region] => Fuschl am See
[price] => 1.319
)
I been looking thru the posts here all day but can't figure out what I'm doing wrong. (I'm new to php and json)
Here is my code that work.
$json = '{"id":1234,"img":"1.jpg"}';
$data = json_decode($json, true);
echo $data["img"];
But when the json respond is this
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
it's a big harder for me. then img is a child of demo1? How to get it?
Thx. :)
Figuring out the array indices
As you're new to PHP, I'll explain how to figure out the array indces of the required array value. In PHP, there are many functions for debugging — print_r() and var_dump() are two of them. print_r() gives us a human-readable output of the supplied array, and var_dump() gives us a structured output along with the variable types and values.
In this case, print_r() should suffice:
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
$data = json_decode($json, true);
// wrap the output in <pre> tags to get a prettier output
echo '<pre>';
print_r($data);
echo '</pre>';
This will produce the following output:
Array
(
[demo1] => Array
(
[0] => Array
(
[id] => 1234
[img] => 1.jpg
)
)
[userId] => 1
)
From there, it should be pretty easy for you to figure out how to access the required vaule.
$data['demo1'][0]['img'];
Creating a helper function for ease of use
For ease of use, you can create a helper function to make this process easier. Whenever you want to view the contents of an array, you can simply call dump_array($array); and be done with it. No more messing around with <pre> tags or print_r().
Function code:
function dump_array($array) {
echo '<pre>' . print_r($array, TRUE) . '</pre>';
}
Usage example:
$arr = ['foo' => range('a','i'), 'bar' => range(1,9)];
dump_array($arr);
after decoding :
echo $data->demo[0]->img;
Basically, a { in JSON leads to a -> (it's an object).
And a [ to a [], it's an array.
Hello I have decoded a json string that I sent to my server and Im trying to get the values from him.
My problem is that I cant get the values from the inner arrays.
This is my code:
<?php
$post = file_get_contents('php://input');
$arrayBig = json_decode($post, true);
foreach ($arrayBig as $array)
{
$exercise = $array['exercise'];
$response["exercise"] = $exercise;
$response["array"] = $array;
echo json_encode($response);
}
?>
When I get the answer from my $response I get this values:
{"exercise":null,"array":[{"exercise":"foo","reps":"foo"}]}
Why is $array['exercise'] null if I can see that is not null in the array
Thanks.
From looking at the result of $response['array'], it looks like $array is actually this
[['exercise' => 'foo', 'reps' => 'foo']]
that is, an associative array nested within a numeric one. You should probably do some value checking before blindly assigning values but in the interest of brevity...
$exercise = $array[0]['exercise'];
Because of the [{...}] you are getting an array in an array when you decode your array key.
So:
$exercise = $array['exercise'];
Should be:
$exercise = $array[0]['exercise'];
See the example here.