(
[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)
Related
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']);
This is my example json data
[
{"kode":"AX5","harga":"6200","status":"1","nama":"AXIS 5"},
{"kode":"AX10","harga":"11250","status":"1","nama":"AXIS 10"},
{"kode":"AX25","harga":"25750","status":"1","nama":"AXIS 25"},
{"kode":"AX50","harga":"50800","status":"1","nama":"AXIS 50"}
]
and i want to save the data to mysql with php, field product_id, price, status, name, anyone can help me please
my problem is, i dont know better code for me in php
You could use PHP
json_decode()
function to convert that json string into PHP variables.
You could then get those values and save them to the MySQL Database;
Source json_decode PHP Manual
you can use json_decode(). it takes a JSON encoded string and converts it into a PHP variable.
<?php
$json_data = '[{"kode":"AX5","harga":"6200","status":"1","nama":"AXIS 5"},{"kode":"AX10","harga":"11250","status":"1","nama":"AXIS 10"},{"kode":"AX25","harga":"25750","status":"1","nama":"AXIS 25"},{"kode":"AX50","harga":"50800","status":"1","nama":"AXIS 50"}]';
$array_data = json_decode($json_data);
echo '<pre>';
print_r($array_data);
foreach ($array_data as $event) {
echo 'Product_id:' . $event->kode;
echo "<br>";
echo 'status:' . $event->status;
echo "<br>";
}
then output is
Array
(
[0] => stdClass Object
(
[kode] => AX5
[harga] => 6200
[status] => 1
[nama] => AXIS 5
)
[1] => stdClass Object
(
[kode] => AX10
[harga] => 11250
[status] => 1
[nama] => AXIS 10
)
[2] => stdClass Object
(
[kode] => AX25
[harga] => 25750
[status] => 1
[nama] => AXIS 25
)
[3] => stdClass Object
(
[kode] => AX50
[harga] => 50800
[status] => 1
[nama] => AXIS 50
)
)
Product_id:AX5
status:1
Product_id:AX10
status:1
Product_id:AX25
status:1
Product_id:AX50
status:1
for more information
http://php.net/manual/en/function.json-decode.php
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];
I am getting a JSON response which looks like this:
stdClass Object
(
[location00] => Array
(
[0] => stdClass Object
(
[id_0] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Wanted by Aryurumoka
[gold_reward] => 58900
[event] => 0
[description] => Not provided.
)
)
)
)
)
For example, i am able to get [name] by $quests->location00[0]->id_0[0]->name.
Lets say i create a new variable $location = 'location00'. Now if i try $quests->$location[0]->id_0[0]->name', i am getting Undefined property: stdClass::$l error. I tried $location = 'location00[0]' as well however i have completly no idea why this happenes. How can i assign location00 to variable to use it while parsing JSON?
You can use associative array or $obj->{$var} :
<?php
$quests = json_decode($json, true);
$location = 'location00';
$name = $quests[$location][0]['id_0'][0]['name'];
I'd try to get new JSON, but you can interpolate object property retrieval with braces:
$quests->{$location}[0]
I have a SOAP Response from a Web Service and have extracted the XML data as a SimpleXMLElement. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array of SimpleXMLElement objects.
I am now trying to export this data into a MySQL Database which, according to my research, means turning the array into a String and then using mysql_query("INSERT INTO (whatever) VALUES (whatever)");. I have tried implode and serialize but neither work and I get the error:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
This is what the array I have created from the SimpleXMLELement looks like:
Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)
[journal] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => source
)
)
[publication] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => item
)
[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)
[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)
[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)
[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)
[author3] => SimpleXMLElement Object
(
[0] => Trewby, ID
)
[citations] => SimpleXMLElement Object
(
[#attributes] => Array
(
[local_count] => 15
[coll_id] => WOS
)
)
) ... etc ...
)
Can anyone help me with the method to get this data into my database, please? Do I need to change it into (yet) another format?
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
<?php
// your array (already built)
$arraySimpleXml = array(
"example1" => new SimpleXMLElement("<test>value</test>"),
"example2" => new SimpleXMLElement("<test>value2</test>")
);
// output array, to store in database
$result = array();
foreach($arraySimpleXml as $key => $simpleXml) {
$result[$key] = $simpleXml->asXML();
}
// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);
?>
So I worked out how to change the data into a standard array rather than an array of SimpleXMLElements so that I can successfully insert it into a MySQL database.
When iterating the SimpleXMLElement object to extract the data I needed I cast the type as String so that now my array has the format (as opposed to above):
Array
(
[0] => Array
(
[uid] => WOS:000238186400009
[journal] => JOURNAL OF ZOOLOGY
[publication] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
[year] => 2006
[author1] => Young, RP
[address] => Cent Sci Lab, Sand Hutton, Yorks, England
[author2] => Davison, J
[author3] => Trewby, ID
[citations] => 15
)
)
Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:
$uid = $record->UID;
I did:
$uid = (string)$record->UID;
For each of the data fields I required. This ensures the data is stored as a String and so removes the SimpleXMLElement format.