Below is the dump that I got from mongo. I need to fetch the opening artist name.
Array
(
[_id] => MongoId Object
(
[$id] => 51c9b63b6f7cb5f8229f27b7
)
[s20] => Array
(
[opening] => Array
(
[artist] => Array
(
[name] => Jay Z
)
[music] => Array
(
[name] => 99 problems
)
)
)
So, I tried:
foreach($mongo_dump as $key=>$value){
echo "<pre>KEY: " . print_r($key["s20"]["opening"]["artist"]["name"]) . "</pre>"; // line # 16
echo "<pre>VALUE: " . print_r($value) . "</pre>";
echo "\n\n";
}
However, I did not get the artist name. I received the following PHP warning:
PHP Warning: Illegal string offset 's20' in /var/www/Code/analytics/fetch_top_5_opening_artists.php on line 16
As Blaine mentions, $key isn't an array. The way that you are traversing the dump is incorrect. $key becomes a string in the context of the foreach loop. Try doing something like this:
if ($key == "s20") {
echo "<pre>KEY: " . print_r($value["opening"]["artist"]["name"]) . "</pre>";
}
the value itself is array() so your forloop is not going work unless you setupup nested. Here is example of neted for loop.
foreach($mongo_dump as $key )
{
{
foreach($key as $subkey)
{
echo $subkey
echo "\n\n";
}
}
Related
I want to get the last element in the JSON decoded array,
Error I am getting
Warning: end() expects parameter 1 to be array
$data output
stdClass Object (
[contact001] => stdClass Object ( [age] => 33 [name] => Robert [tel] => 87778787878787878 )
[contact002] => stdClass Object ( [age] => 33 [name] => Calvin [tel] => 87778787878787878 )
)
PHP code:
$namejson = $firebase->get(DEFAULT_PATH . '/name/');
$data=json_decode($namejson);
foreach ($data as $key => $value) {
echo end($key);
}
Warning: end() expects parameter 1 to be array
When you decode your $namejson json string to array, you can use array_keys to get array of keys, that are present in your array. Next, you need the last key from this array, use array_pop for this. And because the last key is a string, you can use simple ++ operator to increment the value:
$data = json_decode($namejson, true);
$keys = array_keys($data);
$last_key = array_pop($keys);
echo 'Last key: ' . $last_key;
$last_key++;
$next_key = $last_key;
echo 'Next key: ' . $next_key;
$namejson = $firebase->get(DEFAULT_PATH . '/name/');
$data=json_decode($namejson,true);
foreach ($data as $key => $value) {
echo end($value);
}
I have an array, the output of print_r is:
Array ( [0] => Array ( [ID] => 1 [col1] => 1 ) [1] => Array ( [ID] => 2 [col1] => 2 ) )
Could you help of kind of array it is? So that I could research more about it? What I want is to get ID and col1 values
I've tried to use foreach:
foreach($array_name as $key=>$value){
print "$key holds $value\n";
}
The output I get is 0 holds Array 1 holds Array
And I would simply like to get:
1 1
2 2
It's a multi dimensional array, or an array where each element is another array. So you'll need to loop twice. Try this to look at it:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$key = $val\n";
}
}
Or, to get your just added desired output, do this:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$val ";
}
print "\n";
}
Or this:
foreach($array_names as $arr)
{
print $arr['ID'] . " " . $arr['col1'] . "\n";
}
or a few other ways but you should be getting the picture.
I'm newbie to PHP and trying to get a string from mysql query result.
I'm run this code:
$results = $query->rows;
echo '<pre>';
print_r($results);
echo '</pre>';
And get this result:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => Array
(
[location] => someaddress
)
)
How can I get 'someaddress' to string?
foreach($results as $key=>$val){
echo $val['location'] . "<br/>";
}
no need to convert to string.simply iterate to the loop and store all location to some array if more are there.
Try this
foreach($results as $key => $value)
{
echo $value['location'] . "<br/>";
}
I have got the below array
Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
I would like to display as
Contact Number
35443545
<hr>
Address
vvvv
My code
foreach($address_box_content as $k=>$address)
{
echo '<h2>'.$address[$k].'</h2><p>'.$address[$k+1].'</p>';
}
But for some reason it is printing the first 2 lines and then displaying a notice 'Undefined offset:2'
What you have is an array. One whose elements are (also) arrays. Each array that you have stored has 2 elements, 0 and 1.
When you loop, $k is the index of the main (outer) array. Its value doesn't make any sense in the inner array. You just need to loop over the outer array, and print the 0 and 1 elements from the inner one.
foreach($address_box_content as $address)
{
echo '<h2>'.$address[0].'</h2><p>'.$address[1].'</p>';
}
Well you're getting undefined offset error because you're using $k which is the index of the outer array
You can do something like:
foreach($address_box_content as $addresses){
foreach($addresses as $address){
echo '<h2>', $address, '</h2><p>', $address, '</p>';
}
}
And if you want to get the index of inner array:
foreach($address_box_content as $addresses){
foreach($addresses as $key => $address){
echo '<h2>', $address[$key], '</h2><p>', $address[$key], '</p>';
}
}
This is your array
$data = Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
simple way to print array
for($i=0;$<count($data);$i++)
{
echo "<h2>".$data[$i][$0]."</h2>"."<p>".$value[$i][1]."</p>"."<hr />";
}
well, for each element you define a key and a value which are $k and $address.
$k will be 0 and 1, $address will be 0 and 1, and then 0 and 1.
the undefined offset error is because you call $k plus 1 that on the second foeach iteration try to access to position 1+1 (2) doesn't find anything.
you could use something like:
foreach($address_box_content as $data => $value) {
echo "<h2>" . $value[0] . "</h2>"
. "<p>" . $value[1] . "</p>"
. "<hr />";
}
I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";