download12749 2017-02-16 03:33
浏览 87

从数组中检索对象

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.

  • 写回答

2条回答 默认 最新

  • duanaigua4033 2017-02-16 03:47
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度