0

I have this array generated:

Code:

$user_info = file_get_contents('http://localhost/rest-server/webservice/api.php?action=get_user&id='.$_GET["id"]);      
$user_info = json_decode($user_info, true);     
print_r($user_info); 

Output:

Array ( [0] => 
    Array ( [user_info] => 
        Array ( [id] => 12 
                [first_name] => Test1 
                [last_name] => User1 
                [email] => [email protected] 
                [country] => ABC 
                [city] => XYZ 
              )
          )
      ) 

How can I use this array? So that I fetch this data like : `$user_info['first_name'];

I want to put a While Loop on Array for Multiple Records like this too:

Array ( [0] => Array ( [user_list] => Array ( [id] => 12 [first_name] => Test1 [last_name] => User1 [email] => [email protected] [country] => ABC [city] => XYZ ) ) [1] => Array ( [user_list] => Array ( [id] => 13 [first_name] => Test2 [last_name] => User2 [email] => [email protected] [country] => XYZ [city] => ABC ) ) ) 
7
  • This is an array of arrays to to access the first "first_name" you need to reference it by $user_info[0]["first_name"]; Commented Oct 23, 2013 at 5:07
  • I tried it this way but its not working Commented Oct 23, 2013 at 5:10
  • What happened? Error? Nothing? Commented Oct 23, 2013 at 5:16
  • This is what you need $user_info[0]["user_info"]["first_name"]; Commented Oct 23, 2013 at 5:20
  • OK but what if i have Multiple Records like this : Commented Oct 23, 2013 at 5:20

4 Answers 4

2

Your output is given as multi-dimensional array.

You can try to access it like so:

// Update to go through all items in $user_info:
foreach ($user_info as $index => $value)
{
    echo "For item {$index}:\n",
         "First name is: {$value['user_info']['first_name']}\n",
         "Email is     : {$value['user_info']['email']}\n";
    // etc.
}


// Old post to print individual record:

// Print the first name:
echo $user_info[0]['user_info']['first_name'];

// Print the email:
echo $user_info[0]['user_info']['email'];

// etc.

Links for your reference:

  1. PHP.net on array
  2. PHP.net on foreach()
Sign up to request clarification or add additional context in comments.

3 Comments

Well this worked for single record but not for a multiple records like this:
Array ( [0] => Array ( [user_list] => Array ( [id] => 12 [first_name] => Test1 [last_name] => User1 [email] => [email protected] [country] => ABC [city] => XYZ ) ) [1] => Array ( [user_list] => Array ( [id] => 13 [first_name] => Test2 [last_name] => User2 [email] => [email protected] [country] => XYZ [city] => ABC ) ) )
Didn't know you have more than one item in your output. I have updated the answer above. For information, please see: PHP.net on array and PHP.net on foreach()
0

You need this ?

echo  $user_info[0]['first_name'];

Comments

0
foreach($yourArray as $array) {
    echo $array['user_info']['first_name'];
}

Comments

0
 echo $user_info[0]['user_info']['id'] 
 echo $user_info[0]['user_info']['first_name']
 echo $user_info[0]['user_info']['last_name'] 
 echo $user_info[0]['user_info']['email']
 echo $user_info[0]['user_info']['country'] 
 echo $user_info[0]['user_info']['city']

It gives you output like below with particular array value.

It is for perticular this array if you want to fetch array that is position on [1] then you must use foreach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.