从数组中检索对象

I've been trying to read other examples that I've found via Google but I'm not 100% sure I understand exactly where I'm at myself. I've recently started PHP & Javascript and have been building a web-based game. Nothing extraordinary really, but I've been building the system from scratch one piece at a time. I'm able to register, log in, make a character, and load the room. I'm currently building the NPC initializer and tried to create a solution to deal with multiple NPCs in one room. I've got this code inside the getNPC.php that returns what I need to the ajax request.

            while($row2 = mysqli_fetch_assoc($result2))
            {
                array_push($npcarray, array(
                    'id' => $row2['id'],
                    'name' => $row2['name'],
                    'location' => $row2['location'],
                    'switches' => $row2['switches'],
                    'switchesonoff' => $row2['switchon-off']
                ));
            }

So I was ecstatic at this point since the browser developer's console logged this

[{"id":"1","name":"Girl","location":"1","switches":"1","switchesonoff":"1"},
 {"id":"2","name":"Deer","location":"1","switches":"1","switchesonoff":"0"}]

The only problem is that I don't know how to retrieve the entries separately so I can utilize them in the actual NPC initializing process.

My terminology may be off as far as far as objects and arrays go but I promise that I'm really trying!! If I had to describe my ideal learning style, it would likely be me uncuffing myself while burning at the stake.

You can create a loop after parsing the data -

var data = JSON.parse(data); // Use your response of ajax in data

    for (var i = 0; i < data.length; i++) {

       console.log(data[i].id)
       console.log(data[i].name)
    }

You can also use each loop of jquery. Hope this will help you

I thoght you want to parse data of arry you retrive.If any misunderstanding pls remind me.

code

while($row2 = mysqli_fetch_assoc($result2))
        {
            array_push($npcarray, array(
                'id' => $row2['id'],
                'name' => $row2['name'],
                'location' => $row2['location'],
                'switches' => $row2['switches'],
                'switchesonoff' => $row2['switchon-off']
            ));
        }
//after async
for(var key in npcarray) console.log(npcarray[key].id);//Replace id with what you want.