0

I have an array of people and I need to access individual values within the array such as first name.

The array is as follows:

$people = array(
'jjones' => array('firstName' => 'Jim', 'lastName' => 'Jones', 'age' => 20, 'major' => 'Computer Science', 'phone' => '212-460-9393', 'email' => 'jjones@miamioh.edu', 'state' => 'OH'),
'asmith' => array('firstName' => 'April', 'lastName' => 'Smith', 'age' => 19, 'major' => 'Mechanical Engineering', 'phone' => '913-939-3929', 'email' => 'asmith@miamioh.edu', 'state' => 'WY'),
'pstemple' => array('firstName' => 'Pat', 'lastName' => 'Stemple', 'age' => 21, 'major' => 'Theater Performance', 'phone' => '917-222-2232', 'email' => 'pstemple@miamioh.edu', 'state' => 'NY'),
'jjones1' => array('firstName' => 'Janet', 'lastName' => 'Jones', 'age' => 22, 'major' => 'Botany', 'phone' => '817-332-9392', 'email' => 'jjones1@miamioh.edu', 'state' => 'CA'),
'llerner' => array('firstName' => 'Leon', 'lastName' => 'Lerner', 'age' => 18, 'major' => 'Biology', 'phone' => '315-444-3494', 'email' => 'llerner@miamioh.edu', 'state' => 'OH'),
'mmeyer' => array('firstName' => 'Margret', 'lastName' => 'Meyer', 'age' => 24, 'major' => 'Interactive Media Studies', 'phone' => '219-333-0303', 'email' => 'mmeyer@miamioh.edu', 'state' => 'OH'),
'achaudhry' => array('firstName' => 'Anik', 'lastName' => 'Chaudhry', 'age' => 19, 'major' => 'Management Information Systems', 'phone' => '914-555-5555', 'email' => 'achaudhry@miamioh.edu', 'state' => 'NY'),
'sdogg' => array('firstName' => 'Snoop', 'lastName' => 'Dogg', 'age' => 42, 'major' => 'Botany', 'phone' => '414-333-2433', 'email' => 'sdogg@miamioh.edu', 'state' => 'CA'),
'bclinton' => array('firstName' => 'Bill', 'lastName' => 'Clinton', 'age' => 25, 'major' => 'Political Science', 'phone' => '933-440-3033', 'email' => 'bclinton@miamioh.edu', 'state' => 'AK'),);

I have set up my system so it will accept a partial query. Ie. If I want the count of how many IDs contain JJ I will get two. When I set up my javascript however, I cannot seem to access the information about each of the IDs. For example when using JJ:

        console.log(count);
        console.log(json);
        console.log(json[0]);
        console.log(json[0].firstName);

Will return the following in the console window: Console Image However I can drill down into the console.log(json); Drilldown

I need a way to display directly display a value like the first name of JJONES in the console.

Nick
  • 93
  • 9

4 Answers4

1

A PHP associative array, when JSON encoded is transformed into an object, not an array. So you can't use [0] to access the first. To access the jjones object, you can do:

console.log( json.jjones.firstName );
console.log( json['jjones'].firstName );

To iterate over them you can do:

for(var key in json){
    // key is jjones, asmith etc
    console.log( json[key].firstName );
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Try json[0].jjones.firstName, in console You can see that your json[0] is object with property jjones and ther is no property firstName

Łukasz Szewczak
  • 1,851
  • 1
  • 13
  • 14
0

Javascript and PHP have very different ideas of an array.

PHP allows both traditional numeric arrays as well as associative arrays.

JavaScript has numeric arrays and it uses objects as associative arrays.

This post goes into detail.

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
0
   console.log(json[0]['jjones'].firstName);

or

   console.log(json[0].jjones.firstName);
Govind Singh
  • 15,282
  • 14
  • 72
  • 106