There is considera
Q: How to use var
1. Field of the In
Q: How to make a
"Mister?" "Miss, I
Inhibition of huma
I've been in love
Q: Where is my lo
Criminal Minds F.
As usual, I really

Category: Guitar
Amidst a tumultuou
Infection of mouse
The present invent
A major and possib
If this is your fi
Q: How to make su
It would be a crim
--- title: "List v
Q: Pass variable
Q: PHP : array is not displaying after looping? $data = $obj->getdetails($id); print_r($data); here is my print_r of $data... Array ( [id] => 2 [user_type] => 1 [email] => [first_name] => [last_name] => [phone] => ) Here is the function that is returning data... public function getdetails($id){ $str = file_get_contents("user.php?id=".$id); $s = json_decode($str, TRUE); return $s; } now when I do this.. foreach ($data as $test) { echo "
";

    print_r($test);
}

my test code is running absolutely fine and this is what I get when I do print_r($test)..
Array ( [id] => 2 [user_type] => 1 [email] => [first_name] => [last_name] => [phone] => ) 

I want to access a value inside $test like this...
echo $test[0];

But nothing is showing up.
Can someone point me to what is missing in this code ? Thanks a lot !!!

A:

$test is an array and index 1 is "email".
So instead of:
echo $test[0];
echo $test[1];

You can use:
echo $test['id'];
echo $test['email'];
echo $test['phone'];

A:

You need to index like this:
echo $test[0];
echo $test[1];

foreach ($data as $test) {
    echo "
";

    print_r($test);
}

and so on...

A:

It's because $data is an array which has indices such as 'id' and not 'email'.
So instead of using 
echo $test[0];

Try
echo $test['id'];

or simply
echo $test['id'][0];

See also: What are the differences between PHP's array(), [] and {} when assigning to them?

Note:
If you use the above it should work as if you were trying to use the variable with index. In other words, this works, $test['id'] should work, it's only shorter to say $test[0] like above. The documentation for [0] is here.

Example:
$var = [0, "hello"];
$a = "world";

echo $a[0]; // "world"
echo $var[0]; // "hello"

// This way it will print 'world' because it is the same as using it as an array
echo $var[0]; // "hello"

And as an array
// This works as well, just prints first element
echo $var[0];
// And this will do the same as this line
echo $var[0][0]; // "world"

So I hope this helped.