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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊