How to get array object key value in php

I have an array of objects and I need to convert to key value pairs, my code is

i have a model as: model setting.php

public function currency_info[]{
     $currency_info = [
                ['code' => 'AED', 'name' => 'United Arab Emirates Dirham'],
                ['code' => 'ANG', 'name' => 'NL Antillian Guilder'],
                ['code' => 'ARS', 'name' => 'Argentine Peso'],
                ['code' => 'AUD', 'name' => 'Australian Dollar'],
                ['code' => 'BRL', 'name' => 'Brazilian Real'],
              ]
}

and controller as:

SettingController.php

public function index[]
{
        $setting = new Setting[];
        $currency_list = $setting->currency_info[];

        $result = [];
        foreach [$currency_list as $currency] {
            $result[$currency->code] = $currency->name;
        }

        return $result;
}

i get the following error:

ErrorException [E_NOTICE] Trying to get property 'code' of non-object

Where am I doing wrong and how can I correct this?

[PHP 4, PHP 5, PHP 7, PHP 8]

array_keysReturn all the keys or a subset of the keys of an array

Description

array_keys[array $array]: array

array_keys[array $array, mixed $search_value, bool $strict = false]: array

If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

Parameters

array

An array containing keys to return.

search_value

If specified, then only keys containing this value are returned.

strict

Determines if strict comparison [===] should be used during the search.

Return Values

Returns an array of all the keys in array.

Examples

Example #1 array_keys[] example

The above example will output:

Array
[
    [0] => 0
    [1] => color
]
Array
[
    [0] => 0
    [1] => 3
    [2] => 4
]
Array
[
    [0] => color
    [1] => size
]

See Also

  • array_values[] - Return all the values of an array
  • array_combine[] - Creates an array by using one array for keys and another for its values
  • array_key_exists[] - Checks if the given key or index exists in the array
  • array_search[] - Searches the array for a given value and returns the first corresponding key if successful

pat dot leblanc at gmail dot com

11 years ago

It's worth noting that if you have keys that are long integer, such as '329462291595', they will be considered as such on a 64bits system, but will be of type string on a 32 bits system.

for example:


will return on a 64 bits system:


but on a 32 bits system:


I hope it will save someone the huge headache I had :]

Sven [bitcetera.com]

16 years ago

Here's how to get the first key, the last key, the first value or the last value of a [hash] array without explicitly copying nor altering the original array:

phpnet at holodyn dot com

8 years ago

Since 5.4 STRICT standards dictate that you cannot wrap array_keys in a function like array_shift that attempts to reference the array. 

Invalid:
echo array_shift[ array_keys[ array['a' => 'apple'] ] ];

Valid:
$keys = array_keys[ array['a' => 'apple'] ];
echo array_shift[ $keys ];

But Wait! Since PHP [currently] allows you to break a reference by wrapping a variable in parentheses, you can currently use:

echo array_shift[ [ array_keys[ array['a' => 'apple'] ] ] ];

However I would expect in time the PHP team will modify the rules of parentheses.

Robert C.

6 years ago

Keys from multi dimensional array to simple array

Want to traverse an multi dimensional array and get the keys back in a single dimensional array? This will do the trick:

el dot quick at gmail dot com

11 years ago

Sorry for my english...

I wrote a function to get keys of arrays recursivelly...



I hope it will be usefull

Regards

jochem

16 years ago

might be worth noting in the docs that not all associative [string] keys are a like, output of the follow bit of code demonstrates - might be a handy introduction to automatic typecasting in php for some people [and save a few headaches]:

Chủ Đề