douzhaochan6468 2018-11-17 02:03
浏览 932
已采纳

尝试在Laravel中获取特定的数组键和值的一部分

I am very new to both PHP and Laravel. I want to dump specific information out of an array. I prefer not to use a foreach loop as I know the one I want.

Here is the array :

enter image description here

I want to display ICAR - Intermediate

I have tried :

             dump($listofcarcodesnames[2]);
             dump($listofcarcodesnames[2]['code']);

and

             dump($listofcarcodesnames[2]->code);

and

             dump($listofcarcodesnames->code[2]);

and

             dump($listofcarcodesnames->$code[2]);

I am sure this is something simple that I am missing in the syntax but I can't figure it out. Without using a for loop to loop through every key and value how can I get both the key ICAR and the code part of the value of a given pair?

**CLEARIFICATION : **

Both of the responses below from @newUserName02 and @Carlos Gurrero partially give me what I need

$listofcarcodesnames['ICAR']['CODE']->code;  

does work IF I know I am looking for ICAR but if I only know I need the information from the 3rd position how do I get

$listofcarcodesnames[3rdKey]['CODE']->code;
  • 写回答

2条回答 默认 最新

  • ds2010630 2018-11-17 02:25
    关注

    You have to get the values in an array by their keys. This is an associative array with strings as keys, with an object nested inside.

    Quick breakdown of your example:

    • The value at the key 'ICAR' is an array with 2 other keys: 'code' and 'vehicle_name'
    • The value at the key 'code' is an object with one property: code

    To access the value inside you can do this:

    // this will get you the value 'Intermediate'
    dump($listofcarcodesnames['ICAR']['code']->code);
    

    http://php.net/manual/en/language.types.array.php

    NOTE: PHP allows you to have any string or integer as an array key, so the following is a perfectly valid array:

    $array = [
        0 => 'first index',
        1 => 'second index',
        '' => 'empty string key',
        'anotherkey' => 'string key',
        'nested' => [
            'nested' => [
                'nested' => 'etc'
            ],
            0 => [
                0 => 'hello',
                1 => 'world',
            ],
        ],
    ];
    
    dump($array[0]);                            // 'first index'
    dump($array[1]);                            // 'second index'
    dump($array['']);                           // 'empty string key'
    dump($array['anotherkey']);                 // 'string key'
    dump($array['nested']['nested']['nested']); // 'etc'
    dump($array['nested'][0][0]);               // 'hello'
    

    As for getting 'ICAR' it depends. Since it isn't a value in the array, and you already know that's the specific thing you want, you could just code it in where you need it.

    echo 'ICAR - ' . $listofcarcodesnames['ICAR']['code']->code;
    

    EDIT: if you don't know what the exact key name is, but you happen to know it's the 3rd one, you can do something like this:

    $count = 0;
    foreach($listofcarcodesnames as $key => $val) {
        if($count === 2) {
            echo $key . ' - ' . $listofcarcodesnames[$key]['code']->code;
        }
        $count++;
    }
    

    But be careful with this method. When inside PHP, the keys in an associative array will maintain the same order. But if you're passing data between PHP and JavaScript, then the JS object's keys are NOT guaranteed to have a consistent order.

    Does JavaScript Guarantee Object Property Order?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”