Able to see a variable in print_r()'s output, but not sure how to access it in code
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $
sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string
or integer
. There are as well compound datatypes, namely array
and object
. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $
sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable
is where you start, ->
is used to access object members which need to be named then (like a name for a variable) : handler
. As you've seen in your debug output that handler
is an object, you need to use again the ->
to access the view
member of it.
Now view
is different because it's an array. You access values of an array by using []
and putting the key in there. The key in my example is a number, 0
. And as the value of that array entry is an object again, in the next step you need to use ->
again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump
as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die();
after the var_dump
statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
Not var_dump() nor print_r() will show readable information.... but the same confusing output. Why?
try to preformat it for better readability:
echo "<pre>";
print_r($some_var);
echo "</pre>";
Accessing a value from a returned object
Try:
$list_id = $result[0]->list_id;
Create variable from print_r output
Amusingly the PHP manual contains an example that tries to recreate the original structure from the print_r output:print_r_reverse()
http://www.php.net/manual/en/function.print-r.php#93529
However it does depend on whitespace being preserved. So you would need the actual HTML content, not the rendered text to pipe it back in.
Also it doesn't look like it could understand anything but arrays, and does not descend. That would be incredibly difficult as there is no real termination marker for strings, which can both contain newlines and )
or even [0]
and =>
which could be mistaken for print_r delimiters. Correctly reparsing the print_r structure would be near impossible even with a recursive regex (and an actual parser) - it could only be accomplished by guesswork splitting like in the code linked above.. (There are two more versions there, maybe you have to try them through to find a match for your specific case.)
php and session: how get an associative array from a session variable?
$variable = $_SESSION['%http_user%']['username'];
How can I echo inside this array of values?
Item
is nested inside a couple of objects. Assuming your outer object is $response
, you are looking for:
$response->Items->Item[0]
items
is an object stdClass
, and item
is a property of that object. item
itself is an array, having the keys 0-9
you are looking for.
Each of those array elements is then an object stdClass
itself, so access its properties (which we can't see in your output) with the ->
operator.
$response->Items->Item[0]->someProperty
$response->Items->Item[9]->someOtherProperty
Edit: Changed item
to Item
, as it is capitalized in the sample output.
How to out put the result?
You are dealing with arrays, not objects.
So you either need $node->field_pimage['und'][0]['uri']
(if $node
itself is an object) or $node['field_pimage']['und'][0]['uri']
(if $node
is an array, too)
Looping through deep JSON tree
json_decode the json into an object and then loop:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.reddit.com/r/indie/comments/zc0lz/.json");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$json = curl_exec($curl);
curl_close($curl);
//decode the json
$json_obj = json_decode($json);
//loop the result set(array) and access the data->children sub array.
foreach($json_obj as $v){
foreach($v->data->children as $d){
if(isset($d->data->body)){
echo $d->data->body.'<br />'.PHP_EOL;
}
}
}
/*
Is anyone else bored with this? The first album was good for the moment it existed in, but the has since passed. It just seems their sound didn't mature very well.<br />
Yeah, from the songs I had heard, this album missed it. Too similar and not as exciting.<br />
half the album is just as amazing as the first. half is kind of dull<br />
Got this album today, maybe 2 decent-ish songs, but it sounds like they have tried to copy some of the sounds from the album way too closely. Real shame. 4/10<br />
If the player doesn't work, refresh the page and try again. You can also use the NPR link!<br />
Loved the sound of the first album, and I'm kind of happy they've not strayed to much away from this. Have to agree that it seems to be missing the 'awesomeness' that made the first songs so enjoyable to listen to.<br />
*/
?>
How to simulate array_reverse() using a recursive user-defined function?
A string and an array are two separate things. I cleared up your algorithm a bit:
<?php
$array = [1,2,3,4,5,6,7];
function reverseSequence(&$s) {
$len = count($s);
if($len < 2){
return;
}
$rest = array_slice($s, 1, $len - 2);
reverseSequence($rest);
$s = array_merge([$s[$len - 1]], $rest, [$s[0]]);
}
reverseSequence($array);
print_r($array);
The output obviously is:
Array
(
[0] => 7
[1] => 6
[2] => 5
[3] => 4
[4] => 3
[5] => 2
[6] => 1
)
Related Topics
Access a JavaScript Variable from PHP
When Should I Use MySQLi Instead of MySQL
How to Use Password Hashing With Pdo to Make My Code More Secure
How to Use MySQLi Prepared Statements
Strange Character Encoding of Stored Data , Old Script Is Showing Them Fine New One Doesn'T
Seo Friendly Url Results in CSS Img and Js Not Working
How to Create Friendly Url in PHP
Is "MySQLi_Real_Escape_String" Enough to Avoid SQL Injection or Other SQL Attacks
How to Extract Data from CSV File in PHP
MySQLi_Query() Expects At Least 2 Parameters, 1 Given
Search Form With One or More (Multiple) Parameters
Creating One Array from Another Array in PHP