I have a object as shown below and i want to extract data from
stdClass Object
(
[day1] => stdClass Object
(
[0] => 12.06.2015
[part1] => Array
(
[0] => 19.00
[1] => 22.00
)
[part2] => Array
(
[0] =>
[1] =>
)
)
)
How will i get date as shown above with key 0. I can get others as
$string->day1->part1[0]
How can i get date "12.06.2015" ? This seems to be complicated.
JSON string for reference
{"day1":{"0":"12.06.2015","part1":["19.00","22.00"],"part2":["",""]},"day2":{"0":"13.06.2015","part1":["09.00","12.00"],"part2":["13.00","17.00"]}}
used json_decode to decode it.
You can use this:
echo $string->day1->{0};
Or my preference, decode as an array with the second argument set to true in json_decode() to use this:
echo $string['day1'][0];
Related
Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));
I cannot find how to get the results from this JSON post in PHP.
stdClass Object
(
[api_job_id] => 398438bf-c0a5-46fc-8774-70d2425e1ce7
[data] => Array
(
[0] => stdClass Object
(
[type] => MESSAGE
[message_id] => 15125005817130024103
[to] => xxx
[error_code] => 0
[#meta] => stdClass Object
(
[error] => stdClass Object
(
[error_desc] => NO_USER
[error_code] => 9
)
)
)
)
)
as you can see the meta has a # icon before it.
I can read all data to vars instead of the data in the #meta
I tried many ways like:
$result = $arrayResponse['#meta']['error']['error_desc'];
it's not working in PHP because of the # icon.
Any idea how I can get the values from these errors in #meta?
To refer to an object attribute with a name that doesn't make a valid variable, you can use braces:
$foo = json_decode($string);
var_dump($foo->{'#meta'});
Or pass a truthy value to json_decode() as the second argument, and you'll get back an array instead of an object:
$foo = json_decode($string, true);
var_dump($foo['#meta']);
I'm sorry if this is newbie question but I don't understand how to access the [ids] value in the JSON array via PHP.
Why is this not working?
$jsonResponse = json_decode($response,true);
print $jsonResponse[2]["ids"];
This is the JSON array:
Array
(
[0] => analytics#gaData
[1] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123455&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel&metrics=ga:visits,ga:pageviews&start-date=2013-01-01&end-date=2020-01-01
[2] => Array
(
[start-date] => 2013-01-01
[end-date] => 2020-01-01
[ids] => ga:123455
[dimensions] => ga:eventCategory,ga:eventAction,ga:eventLabel
[metrics] => Array
(
[0] => ga:visits
[1] => ga:pageviews
)
[start-index] => 1
[max-results] => 1000
)
[3] => 1000
[4] => 9
There doesn't seem to be anything wrong with your code. Your code should be printing, what seems to me, the object ga:123455 if the code is executed. When printing this, this object will be converted to a string. PHP will do (string) ga:123455 (invoke the __toString() method on ga:123455) to convert an object to a string. If this method is absent, there must be a warning that ga:123455 cannot be converted to a string. __toString() might also return an empty string.
I would suggest debugging it by doing print var_dump( $jsonResponse[2]["ids"] ); and print var_dump( (string) $jsonResponse[2]["ids"] );.
(
[addF] => stdClass Object
(
[0] => stdClass Object
(
[productID] => 33
[fQty] => 11
[fPW] => 11
[fP] =>
[fH] => PVC
[fB] => SideBelt
[fP] => Single Pleat
[fPanelT] => stdClass Object
(
)
)
)
[addWP] => stdClass Object
(
)
[addRBC] => stdClass Object
(
)
[addRB] => stdClass Object
(
)
[addT] => stdClass Object
(
)
)
{"ErrorMessage":true}
The above output is base on the follow code below.
$arrOutput["ErrorMessage"]=print_r($objData);
I have use json to pass the array to PHP but i can't get the data.
i try to set the data but i have no value.
$ProductID=isset($objData->allData->addF[0]->productID) ? $objData->allData->addF[0]->productID : "123";
i tried to print_r will return true print will output 1
$objData->allData
it output 1
$objData->addF
also output 1
i don't understand y i can't set the value after i have decode it.
$objData=json_decode(stripslashes($Data));
Jquery part
allData.addF=addF;
allData.addWP=addWP;
allData.addRBC=addRBC;
allData.addRB=addRB;
allData.addT=addT;
//convert the data to json
var dataString = $.toJSON(allData);
$.post('test.php',"Data="+escape(dataString),function(data)
{
var obj=$.parseJSON(data);
alert(obj.ErrorMessage);
});
I need help to retrieve the data from it.
$ProductID=isset($objData->allData->addF->{"0"}->productID)
When I print_r($var) I get the result below.
SimpleXMLElement Object
(
[SEND_FILE] => SimpleXMLElement Object
(
[FILEID] => 123
[GUID] => 456
[SUMMARY] => SimpleXMLElement Object
(
[NB_PAYMENTS] => 1
)
)
)
How can I get the value of the FILEID element in a variable? If I do
print $result->SEND_FILE->FILEID[0]
then I just get the number - what I want, no mention of a SimpleXML Object.
But if I put this variable in an array, as such
$res['file_id'] = $result->SEND_FILE->FILEID[0]
and then print_r($res) I get:
Array
(
[file_id] => SimpleXMLElement Object
(
[0] => 307466
)
)
How can I get it to remove the [0] / SimpleXMLElement Object?
This will look not too elegant, but try casting the result to integer (if the type is known):
$res['file_id'] = (int)$result->SEND_FILE->FILEID[0]
Why do you append the [0] at the end? You dont need that. You should simply do
print $result->SEND_FILE->FILEID;
And that should be enough.