I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)
Related
I have a variable and when I output it with print_r like this:
print_r($sort_order[$field->name]);
I get this:
Array ( [0] => Array ( [sort_order] => 92 ) )
but I only need the value which is 92. How can I do so it outputs only that when echoing it? example:
echo $sort_order[$field->name];
should output simple
92
Your $sortOrder array is actually an array of arrays, like:
[
[ 'sort_order' => 92 ]
]
That's why you can't print it like you expect.
Try:
echo $sort_order[0]['sort_order'];
Output:
92
The print_r() function is used to print human-readable information about a variable.
You can do both print and echo to output the required value:
echo $sort_order[$field->name];
print $sort_order[$field->name];
Hope this helps.
The command print_r displays the variable in a human readable way. So if you need to know all the info in a variable (in particular for large arrays), then you use that. For other use, e.g. when you only need to know the content (in I guess 99.999% of all the cases) you should either use echo as you already mentioned it or print (althoug, they are more or less the same).
Please consider this links for futher information
http://php.net/manual/en/function.print-r.php
What's the difference between echo, print, and print_r in PHP?
I am trying to take an array of filenames and output the following...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image4.jpg";}}}
This is what I have so far...
<?php
$serialized_data = serialize(array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg'));
echo $serialized_data . '<br>';
?>
But this is giving me...
a:4:{i:0;s:34:"http://www.example.com/image1.jpg";i:1;s:34:"http://www.example.com/image2.jpg";i:2;s:34:"http://www.example.com/image3.jpg";i:3;s:34:"http://www.example.com/image4.jpg";}
Where am I going wrong?
There is nothing wrong with the serialized array. You're just not creating the array like you want it to be. PHP can't guess how you really want your array to be, so you have to tell PHP how you want it to be. So what you need to do is to change the input array correctly.
You're giving
array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg')
and that's completely different from the serialized array how it should be. Your Array needs to look like this
array('docs' => array(array('property_imgurl' => 'http://www.example.com/image1.jpg'), array('property_imgurl' => 'http://www.example.com/image2.jpg'), array('property_imgurl' => 'http://www.example.com/image3.jpg'), array('property_imgurl' => 'http://www.example.com/image4.jpg')))
Look at this eval
You're just missing the array key definitions.
$serialized = array(array('docs' => array(array('property_imgurl' => 'http://www.example.com/image4.jpg'))));
As you can see, each URL has a key of property_imgurl and each of those array is part of a parent array with a key of docs
Here's the eval.in
while I was searching through some file in a php library, i found some documents like this
a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}
This is definitely not JSON. Does any one know what is this? or is it a custom syntax for the guy who wrote the library ?
That is a serialized data and not any programming language syntax.
For your understanding...
<?php
$arr = ['a'=>1,'b'=>2,'cnt'=>5];
echo serialize($arr);
OUTPUT :
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:3:"cnt";i:5;}
Language in Php more info on this link unserialize
This is basically a serailize form of array
of follwoing array
Array
(
[0] => Array
(
[cnt] => 1
)
)
You can get it back by into array
$a = 'a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}';
$unserailize_a = (unserialize($a));
To convert an array into string us the Serialize
$a=Array ([storage] => [submitted] => 1 [values] => Array ( [q] => googl [op]))
How can I get the value of q from this array $a->q doesn't give me the value. Why?
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
echo $a['values']['q'];
This is 2-d array you can get value like above.
you can also use the foreach to get retrieve values of arrays.
You should use $a['values']['q'].
to get it do this:
echo $a['values']['q'];
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
Check this foreach tutorial for u can get more information
please tell me what is below code doing? is it creating array with two values or its creating to string index which will take value later?
var $requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
It is assigning index 0 to value MaxResponses and 1 to value AvailableOnlyIndicator.
So array with two values.
Note: var keyword won't work in you are under php 5+.
That code isn't valid php. This is:
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
That code is creating an array with two elements.
The code should be:
<?php array('MaxResponses', 'AvailableOnlyIndicator'); ?>
And the array would be:
Array
(
[0] => MaxResponses
[1] => AvailableOnlyIndicator
)
It's creating an array with two values, with an automatic numeric index. Syntax is slightly off, but it should work.