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

尝试在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?

    打赏 评论
  • duanqu9279 2018-11-17 02:37
    $listofcarcodesnames['ICAR']['CODE']->code;
    

    Should give you "Intermediate"

    Tried in Laravel Tinker

    php artisan tinker
    Psy Shell v0.9.7 (PHP 7.2.0 — cli) by Justin Hileman
    >>> $lisofcarcodesnames = []
    => []
    >>> $lisofcarcodesnames['ICAR'] = []
    => []
    >>> $lisofcarcodesnames['ICAR']['code'] = new stdClass
    => {#3041}
    >>> $lisofcarcodesnames['ICAR']['code']->code = "Intermediate"
    => "Intermediate"
    >>> echp json_encode($lisofcarcodesnames)
    PHP Parse error: Syntax error, unexpected T_STRING on line 1
    >>> echo json_encode($lisofcarcodesnames)
    {"ICAR":{"code":{"code":"Intermediate"}}}⏎
    >>> echo dump($lisofcarcodesnames)
    array:1 [
      "ICAR" => array:1 [
        "code" => {#3041
          +"code": "Intermediate"
        }
      ]
    ]
    ArrayPHP Notice:  Array to string conversion in C:/Users/Charlie Guerreroeval()'d code on line 1
    >>> echo $lisofcarcodesnames['ICAR']['code']->code
    Intermediate⏎
    >>>     
    
    打赏 评论

相关推荐 更多相似问题