I have this array (decoded from JSON, output with print_r):
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[item] => te
[date] => 13.10
)
[1] => stdClass Object
(
[item] => te
[date] => 13.10
)
[2] => stdClass Object
(
[item] => tr
[date] => 13.10
)
)
)
But now I have to remove all the duplicates.
If I try $result = array_unique($array, SORT_REGULAR);
$result is null.
Can someone spot my mistake?
This is a stdClass object, not an array. When you decode by using the function json_decode, you need to pass the parameter "true" to have an array:
$array = json_decode($json, true);
Edit: As people noticed in the comments, the actual array exists in $array['data'], so the array_unique must be applied on $array['data'] instead of $array.
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'];
}
}
I have a JSON similar to this one:
{"request":
{"Target":"Affiliate_Offer","Format":"json","Service":"HasOffers","Version":"2","NetworkId":"adattract","Method":"getPayoutDetails","api_key":"bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891","offer_id":"9463"},"response":
{"status":1,"httpStatus":200,"data":{"offer_payout":
{"payout":"0.820"}},"errors":[],"errorMessage":null}}
And I want to select only value of payout which is 0.820.
Here what I try
<?php
include 'db.php';
$result = mysql_query("SELECT * FROM ada WHERE require_approval='0' ORDER BY i2 ASC LIMIT 0,10") or die(mysql_error());
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {
echo "<div>";
$r = $row['id'];
$ri = $row['i2'];
// $ri contains no. 1 2 3 4 5..//
$json = '';
}
$mydata = json_decode($json, true);
$data = $mydata["response"]["data"];
$pay = $data[$ri]["offer_payout"][payout];
echo "</div>";
mysql_query("INSERT INTO thumb(img,id) VALUES('$nth','$oid')");
echo $pay; //output of 0.8200 but not work plz here i need help//
}
?>
In the vain of 'Give a man a fish and he will feed his family for a day. Teach a man to fish and he will feed his family forever' here is how you find out what a json object looks like.
$json = '{"request":
{"Target":"Affiliate_Offer","Format":"json","Service":"HasOffers","Version":"2","NetworkId":"adattract","Method":"getPayoutDetails","api_key":"bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891","offer_id":"9463"},"response":
{"status":1,"httpStatus":200,"data":{"offer_payout":
{"payout":"0.82000"}},"errors":[],"errorMessage":null}}';
$mydata = json_decode($json,true);
print_r($mydata);
Will output this :-
Array
(
[request] => Array
(
[Target] => Affiliate_Offer
[Format] => json
[Service] => HasOffers
[Version] => 2
[NetworkId] => adattract
[Method] => getPayoutDetails
[api_key] => bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891
[offer_id] => 9463
)
[response] => Array
(
[status] => 1
[httpStatus] => 200
[data] => Array
(
[offer_payout] => Array
(
[payout] => 0.82000
)
)
[errors] => Array
(
)
[errorMessage] =>
)
)
And now you know how to address any and all fields
So the field you want will be
$pay = $mydata["response"]["data"]["offer_payout"]["payout"];
Now you decided to convert a JSON Object to an array, there is no need to do that, and in some ways an object is even easier to address and uses less key strokes than an array.
So leave the ,true out of the json_decode and you get this
$json = '{"request":
{"Target":"Affiliate_Offer","Format":"json","Service":"HasOffers","Version":"2","NetworkId":"adattract","Method":"getPayoutDetails","api_key":"bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891","offer_id":"9463"},"response":
{"status":1,"httpStatus":200,"data":{"offer_payout":
{"payout":"0.82000"}},"errors":[],"errorMessage":null}}';
$mydata = json_decode($json);
print_r($mydata);
Which outputs :-
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Offer
[Format] => json
[Service] => HasOffers
[Version] => 2
[NetworkId] => adattract
[Method] => getPayoutDetails
[api_key] => bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891
[offer_id] => 9463
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[offer_payout] => stdClass Object
(
[payout] => 0.82000
)
)
[errors] => Array
(
)
[errorMessage] =>
)
)
Now the field you want to address is :-
$pay = $mydata->response->data->offer_payout->payout;
You can get values from the json as like that:
// your json
$json = '{"request":
{"Target":"Affiliate_Offer","Format":"json","Service":"HasOffers","Version":"2","NetworkId":"adattract","Method":"getPayoutDetails","api_key":"bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891","offer_id":"9463"},"response":
{"status":1,"httpStatus":200,"data":{"offer_payout":
{"payout":"0.82000"}},"errors":[],"errorMessage":null}}';
// json_decode() function with second param true for array format
$json_decoded = json_decode($json,true);
echo $json_decoded['response']['data']['offer_payout']['payout']; //0.82000
How can you get this in your code?
You just need to change these two lines in your code:
$data = $mydata["response"]["data"];
$pay = $data["offer_payout"]['payout']; //0.82000
$pay = $mydata["response"]["data"]["offer_payout"]["payout"];
You might want to cast it to float, as it is a string in the JSON data you provided:
$pay = (float) $mydata["response"]["data"]["offer_payout"]["payout"];
First you need convert json to array
$array =json_decode($json,TRUE); //$json is converted to array
Now you can retrieve any property from the array
The complete code example is
$json = '{"request":
{"Target":"Affiliate_Offer","Format":"json","Service":"HasOffers","Version":"2","NetworkId":"adattract","Method":"getPayoutDetails","api_key":"bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891","offer_id":"9463"},"response":
{"status":1,"httpStatus":200,"data":{"offer_payout":
{"payout":"0.82000"}},"errors":[],"errorMessage":null}}';
$mydata = json_decode($json,true);
print_r($mydata);
OutPut:
Array ( [request] => Array ( [Target] => Affiliate_Offer [Format] => json [Service] => HasOffers [Version] => 2 [NetworkId] => adattract [Method] => getPayoutDetails [api_key] => bd2e82f029c50b582db85868b7c0b8ab99d095886ab079f87b464bb68486c891 [offer_id] => 9463 ) [response] => Array ( [status] => 1 [httpStatus] => 200 [data] => Array ( [offer_payout] => Array ( [payout] => 0.82000 ) ) [errors] => Array ( ) [errorMessage] => ) ) 1
From the result you can retrieve any array property
Eg-
echo $mydata['response']['status'];
OutPut:
1
Hope this will help you.
$mydata = json_decode($json, true);
$data = $mydata['response']['data'];
print_r($data);
print_r($data['offer_payout']['payout']);
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.
I need to convert below array:-
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
)
to below array:-
Array
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
I tried to do it by typecasting. But It didn't give me the output.
I also checked this link
For Above $result = (array)($array[0]) works fine for me.
But if I have the below then what will I do?
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
[1] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 20.00
)
)
Try this
$array = (array)($array[0]);
try this
$yourArray = array();
$i=0;
foreach ($yourObject as $key => $value) {
$yourArray[$i]['id'] = $value->id;
$yourArray[$i]['risk_reference'] = $value->risk_reference;
$yourArray[$i]['risk_version'] = $value->risk_version;
$yourArray[$i]['bsi'] = $value->bsi;
$i+=1;
}
print_r($yourArray);
http://php.net/get_object_vars
Gets the accessible non-static properties of the given object according to scope.
Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value.