1

I have an array called $arr containing some information about users. Using $arr I want to create a new associative array with specific keys. That's what I got so far:

$groups = [];
foreach($arr as $val) {
    $groups['first_key_name'] = $val->ID;
    $groups['second_key_name'] = $val->login;
}

What I'm trying to achieve is a new array that has the following format:

'first_key_name' => $val->ID
'second_key_name' => $val->login
'first_key_name' => $val->ID
'second_key_name' => $val->login

The problem with my current approach is when I var_dump($groups) I only get one key with an empty value although the array should contain at least 10 entries.

The output of var_dump($groups):

array:1 [▼
  "first_key_name" => "4"
]

What am I doing wrong?

5
  • What does a var_dump of $arr show? Commented Jan 9, 2018 at 17:42
  • 4
    "What I'm trying to achieve is a new array that has the following format: [...]" - that is not possible, you can not use the same array key more than once. Commented Jan 9, 2018 at 17:45
  • The trick is to use just [] to denote a new array item with numerical key Commented Jan 9, 2018 at 17:53
  • 1
    You can't have duplicate keys in an array. Commented Jan 9, 2018 at 17:54
  • If you could have duplicate keys, how would it know which one to return when you did echo $groups['first_key_name']? Have you thought about what you're trying to do? Commented Jan 9, 2018 at 17:56

3 Answers 3

4

You are overwriting your variables each time round the loop in this code

$groups = [];
foreach($arr as $val) {
    $groups['first_key_name'] = $val->ID;
    $groups['second_key_name'] = $val->login;
}

So instead do

$groups = [];
foreach($arr as $val) {
    $groups[] = [
                'first_key_name'  => $val->ID
                'second_key_name' => $val->login
               ]; 
}

This will create something like this

[0]
    [
    'first_key_name' = 1,
    'second_key_name' = 99
    ]
[1]
    [
    'first_key_name' = 2,
    'second_key_name' = 199
    ]
etc
Sign up to request clarification or add additional context in comments.

Comments

1

You approach is overwriting the key value every time. That's why you need to use 2d array.

You can try like this:

$groups = [];
foreach($arr as $val) {
    $groups[] = ['first_key_name' => $val->ID, 'second_key_name' => $val->login];
}

Comments

0

What happens here, is you are overwriting first_key_name and second_key_name in each turn of the loop. But you want to get an array with the new key=>value pairs.

To achieve that you have to append a new item to your array called $groups, like this:

$groups = [];
foreach ($arr as $val) {
    $groups[] = [
        'first_key_name'  => $val->ID,
        'second_key_name' => $val->login
    ];
}

You may also use array_map for this:

$groups = array_map(function ($val) {
    return [
        'first_key_name'  => $val->ID,
        'second_key_name' => $val->login,
    ];
}, $arr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.