简体   繁体   中英

Accessing Multidimensional Arrays JSON

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: 控制台映像 However I can drill down into the console.log(json); 追溯

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

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 );
}

在控制台中尝试json[0].jjones.firstName ,您可以看到json[0]是具有属性jjones对象,而jjones没有属性firstName

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.

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

要么

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM