0

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;
Lance
  • 3,193
  • 2
  • 32
  • 49

2 Answers2

0

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?

newUserName02
  • 1,598
  • 12
  • 17
  • This make a lot of sense and works great as long as I know the Key name,right? If I don't however how can I get the name in the 3rd position? So for the example you have `dump($listofcarcodesnames['ICAR']['code']->code);` (which works great) how do I find the name of the key `ICAR` if all I know is I want the 3rd one down? – Lance Nov 17 '18 at 02:41
  • In that case, you'd have to do a `foreach` loop and keep an independent count variable, to simulate the "index" of an indexed array. Check my updated answer. – newUserName02 Nov 17 '18 at 03:27
  • I was afraid that I was going to end up with foreach loops the array can get very long and was hoping to avoid them but... oh well. Thank you very much for your help – Lance Nov 17 '18 at 03:47
  • @Lance You can always `break` out of the loop once you get what you want. You don't have to iterate over the whole thing. – newUserName02 Nov 17 '18 at 03:50
0
$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⏎
>>>     
  • Same question to the other response, what If is DON'T know I need `ICAR` I only know that I need the items in the group in the 3rd position? How and I get the 3rd `key` so that I can then get code `$listofcarcodesnames[3rdKey]['CODE']->code;` – Lance Nov 17 '18 at 03:01