I need to analyze an object in my code, but when I do a var_dump (or print_r) it just prints the object out with no structure, for example:
[0]=> object(simple_html_dom_node)#2185 (9) { ["nodetype"]=> int(1) ["tag"]=> string(3) "div" ["attr"]=> array(1) { ["class"]=> string(36) "element element--collection internal" } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#2187 (
I need to see it in a more structured format so I can see what is going on, i.e.:
object(simple_html_dom_node)#2185 (9) {
["nodetype"]=> int(1)
["tag"]=> string(3) "div"
["attr"]=> array(1)
{
["class"]=> string(36) "element element--collection internal"
}
["children"]=> array(2) {
[0]=> object(simple_html_dom_node)#2187 (9)
Does anyone know how to do this?
The format you want is actually how var_dump() prints the object. The problem is that you're doing it in an HTML document, and the browser reformats it.
If you put it inside a <pre> tag, the browser will leave the formatting alone. So:
echo "<pre>"; var_dump($object); echo "</pre>";
Try using var_export(), this function will give you a more readable structure of the object or data.
Related
I'm currently getting a JSON response from a company's API and converting it into a PHP array like this:
$api_url = file_get_contents('http://example.com');
$api_details = json_decode($api_url, true);
When I run var_dump on $api_details, I am getting this:
array(2) {
["metadata"]=>
array(5) {
["iserror"]=>
string(5) "false"
["responsetime"]=>
string(5) "0.00s"
["start"]=>
int(1)
["count"]=>
int(99999)
}
["results"]=>
array(3) {
["first"]=>
int(1)
["result"]=>
array(2) {
[0]=>
array(4) {
["total_visitors"]=>
string(4) "3346"
["visitors"]=>
string(4) "3249"
["rpm"]=>
string(4) "0.07"
["revenue"]=>
string(6) "0.2381"
}
[1]=>
array(4) {
["total_visitors"]=>
string(6) "861809"
["visitors"]=>
string(6) "470581"
["rpm"]=>
string(4) "0.02"
["revenue"]=>
string(7) "13.8072"
}
}
}
}
I'm trying to do 2 things and can't figure out how to do either with a multidimensional array.
I need to check to see if metadata > iserror is false. If it is not false, I want to show an error message and not continue with the script.
If it is false, then I wants to loop through the results of results > result and echo the total_visitors, visitors, etc for each of them.
I know how to echo data from array, I guess I'm just getting confused when there's multiple levels to the array.
Anyone that can point me in the right direction would be much appreciated :)
You can iterate over arrays using foreach. You can read up on it here: http://php.net/manual/en/control-structures.foreach.php
Since you're using associative arrays, your code will look something like this:
if ($arr['metadata']['iserror']) {
// Display error here
} else {
foreach($arr['results']['result'] as $result) {
echo $result['total_visitors'];
echo $result['visitors'];
}
}
You'll have to tweak the code to fit exactly what you're doing, but this should get you over the line.
Hope that helps!
I just need to parse a JSON :
object(stdClass)#363 (3)
{
["type"]=> string(8) "champion"
["version"]=> string(6) "5.22.3"
["data"]=> object(stdClass)#362 (127) {
["Thresh"]=> object(stdClass)#366 (4) { ["id"]=> int(412) ["key"]=> string(6) "Thresh" ["name"]=> string(6) "Thresh" ["title"]=> string(18) "Garde aux chaînes" }
["Aatrox"]=> object(stdClass)#365 (4) { ["id"]=> int(266) ["key"]=> string(6) "Aatrox" ["name"]=> string(6) "Aatrox" ["title"]=> string(17) "Épée des Darkin" }
["Tryndamere"]=> object(stdClass)#368 (4) { ["id"]=> int(23) ["key"]=> string(10) "Tryndamere" ["name"]=> string(10) "Tryndamere" ["title"]=> string(11) "Roi barbare" } etc...
How to parse all the datas from this JSON with PHP by using object return.
Thanks in advance for help.
Use json_decode.
Returns as object:
json_decode($json_string);
Returns as associative array:
json_decode($json_string, true);
From what I guess you used json_decode and it returned object. You can now access object values using variable name let's say $var
echo $var->type; // will output champion
You can also convert json to array by providing second argument set to true
then you can access this data via
echo $var['type'];
I am trying to pull one single piece of data from a json encoded string. I managed to get the string into php's jason format, but I dont understand how to display the exact data I want (confirmations) Every time I try to access any of the data it just gives me a bracket({) and no data. I know I'm doing something wrong with retreiving the data from the variable, but I can't pu tmy finger on it.
Here's the data I want to parse, could someone please show me how to access the confirmations value from within the array of data? Thanks.
( https://projectbuilder.info/link.php?ID=jsontest&CMD=page ) [easier to read]
Source Code:
<?PHP
$return = file_get_contents("https://api.chain.com/v2/bitcoin/transactions/76e6f17cb940745255e2b8439eea5dae945a b148f1fbba98a9fb99c9a5801320?api-key-id=ae7317a1cd4ff0d12e49a77bfd8b9dec");
var_dump(json_decode($return));
echo $return[0]["confirmations"]; //one of my many attempts to get the info, also tried without the leading zero, and different numbers. I understand arrays, I just dont understand how the json data is formatted inside the array.
?>
Output Copy(what do I do with $return to get it's confirmation value of 2896?):
object(stdClass)#1 (11) {
["hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["block_hash"]=>
string(64) "00000000000000001646d024d4622a0e4a5c06299d7d776de041bc9c317be1f8"
["block_height"]=>
int(329486)
["block_time"]=>
string(20) "2014-11-11T02:10:21Z"
["chain_received_at"]=>
string(24) "2014-11-11T02:05:48.259Z"
["confirmations"]=>
int(2896)
["lock_time"]=>
int(0)
["inputs"]=>
array(2) {
[0]=>
object(stdClass)#2 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "4f7b6066396e422f1cabd60767093ec6fb4480b60f206c408ad50895541bc023"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1HPvAS96JXYUuLDs5CKNh61SvH6NJT1ykH"
}
["script_signature"]=>
string(213) "304602210087289ed01fd7d04e3c7eb5c38ea1944cbc3789658a1122610079a4f0421e2426022100fca6f5f4623bbac131f06ebdc7389ea0c76b4355da9508ecc7b02107385ee79d01 029a79a3cf6f8b90b7c1210e593a21e46c81ffbbe544eb2ab3ebbd89f33e4f8b2e"
["sequence"]=>
int(4294967295)
}
[1]=>
object(stdClass)#3 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "22de0dbb8d72fac9a6e8775f6f80fa3fc991c41d33fcd5081acb49c0479f2a62"
["output_index"]=>
int(1)
["value"]=>
int(7390022)
["addresses"]=>
array(1) {
[0]=>
string(34) "158kR5o6EWWhFEZfLqGDvkgfoyi2Ep2fhA"
}
["script_signature"]=>
string(209) "304402205e2f36c9c22e02767e3accc8bc609b74d5a23c58e4a8edbac24cb4baf1e3feaa022043ad33becd57c5dee03fd1c1d8be1524281f6f49ba1e44a1a2b340f89554c42d01 031181694e14973f71d45f5f3ab73ee0f30dfa3f488bf53c44e46cf9f4f3d3d722"
["sequence"]=>
int(4294967295)
}
}
["outputs"]=>
array(2) {
[0]=>
object(stdClass)#4 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1P5rwnk3GYbxgpxN9M9EziLrpvoih4c8JC"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 f23e1f6dd21bab989f18c14f26bf37b4e2372eef OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a914f23e1f6dd21bab989f18c14f26bf37b4e2372eef88ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(false)
}
[1]=>
object(stdClass)#5 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(1)
["value"]=>
int(7380022)
["addresses"]=>
array(1) {
[0]=>
string(34) "1DpY5Mu2qTNkwsnPwgqFnaZ1Kq7GpKDboy"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 8c9efff6e8500a36c16a939054a333d81ef23166 OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a9148c9efff6e8500a36c16a939054a333d81ef2316688ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(true)
}
}
["fees"]=>
int(10000)
["amount"]=>
int(7524497)
}
{
Have a look at the docs: http://php.net/json_decode
Convert the json-string to a php-array and save it in a variable. Then use it like any other array to get values from it. Like this:
$return = file_get_contents('http://etc.com/');
$decoded_return = json_decode($return);
echo $decoded_return['confirmations']; //Make sure the key exists, obviously
So keep in mind that $return is a string. The json_decode function returns an array, so you have to save that array in a (new - preferably) variable to get values from it.
I have an array name $json_output.
array(3) {
["ProductsSummary"]=>
array(2) {
["TotalPages"]=>
int(2)
["CurrentPage"]=>
int(1)
}
["Products"]=>
array(60) {
[0]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
[1]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
And so on. I need to unset ProductId and LastShopUpdate from each one.
What i tried:
<?php
foreach($json_output["Products"] as $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
?>
But it is not working. How could I do this?
When looping over an array using foreach, a copy is usually made. Changing something in the copy of course has no effect on the original. Try this:
foreach($json_output["Products"] as & $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
The & causes $bla to be a reference instead of a copy. Therefore it should resolve your problem.
I've read a large number of articles and Stack Overflow questions about converting an XML document or fragment into an array in PHP, but none that I've read so far have addressed my specific problem. Here's my dilemma, preceded by an example XML fragment:
<category>
<template>
<random>
<li>And a good</li>
<li>Pleasant</li>
<li>Good</li>
<li>Fantabulous</li>
</random>
<set name="TOD"><srai>time of day</srai></set> to you, <get name="name" />.
<random>
<li>How are you?</li>
<li>To what do I owe the pleasure of this visit?</li>
<li>May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!</li>
<li>I trust your <get name="TOD" /> is going well?</li>
<li>May your <get name="TOD" /> be as pleasant as possible.</li>
</random>
</template>
</category>
This is a REAL WORLD example of some of the XML my script will be dealing with. The sequence order of the XML tags needs to be preserved, as the parsed results need to be concatenated correctly in order to provide the proper result. So far, all of the methods for converting XML fragments into arrays have created arrays that no longer contain the correct order. As an example, here's a var dump of the above XML, once it's been converted into an array:
Template array Var Dump:
array(4) {
["random"]=>
array(2) {
[0]=>
array(1) {
["li"]=>
array(4) {
[0]=>
array(1) {
["text"]=>
string(10) "And a good"
}
[1]=>
array(1) {
["text"]=>
string(8) "Pleasant"
}
[2]=>
array(1) {
["text"]=>
string(4) "Good"
}
[3]=>
array(1) {
["text"]=>
string(11) "Fantabulous"
}
}
}
[1]=>
array(1) {
["li"]=>
array(5) {
[0]=>
array(1) {
["text"]=>
string(12) "How are you?"
}
[1]=>
array(1) {
["text"]=>
string(44) "To what do I owe the pleasure of this visit?"
}
[2]=>
array(1) {
["text"]=>
string(97) "May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!"
}
[3]=>
array(2) {
["text"]=>
array(2) {
[0]=>
string(12) "I trust your"
[1]=>
string(14) "is going well?"
}
["get"]=>
array(1) {
["#attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
}
}
[4]=>
array(2) {
["text"]=>
array(2) {
[0]=>
string(8) "May your"
[1]=>
string(27) "be as pleasant as possible."
}
["get"]=>
array(1) {
["#attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
}
}
}
}
}
["set"]=>
array(2) {
["#attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
["srai"]=>
array(1) {
["text"]=>
string(11) "time of day"
}
}
["text"]=>
array(2) {
[0]=>
string(7) "to you,"
[1]=>
string(1) "."
}
["get"]=>
array(1) {
["#attributes"]=>
array(1) {
["name"]=>
string(4) "name"
}
}
}
As can be seen, the array, when created, "lost" the sequence order of the XML fragment, and you can't iterate through the array in a linear manner to arrive at the proper response. This is the crux of my problem, and what I'm looking to "fix".
The method I used in this example was json_decode(json_encode($xml), true), but I've used other, more complex script functions, with pretty much the same results. So, just as I asked in the title of this post, how can I preserve the "tag order" when converting an XML fragment to an array in PHP?
Pretty sure there are no flags available with say simpleXML or json_decode. I don't think XML is intended to preserve that. The structure is not intended to convey that, can see how it leads to bad design. Specific to XML, you can get around this by use of sequence in an XSD. But your data looks more like a DOM.
As a work around have you looked at parsing it with as a DOM Document and stepping through it? Not too much code to parse it yourself leveraging that.