0

I have an array as follows:

[
    0 => [
        'name' => 'CARD'
        'id' => '0'
    ]
    1 => [
        'name' => 'MOBILE'
        'id' => '1'
    ]
    2 => [
        'name' => 'GIFT'
        'id' => '2'
    ]
]

I want to change the key id to type in all the array. Is there a way to do this in Yii2 using ArrayHelper?

2 Answers 2

2

You can use getColumn() for this:

$result = ArrayHelper::getColumn($array, function ($data) {
    return [
        'name' => $data['name'],
        'type' => $data['id'],
    ];
});

But it will not really differ from array_map() or simple foreach.

Sign up to request clarification or add additional context in comments.

Comments

1

There is not an array helper for this but you could do this with a php foreach

foreach ($myArray as $key => $value) {
  $myArray[$key]['type'] = $value['id'];
  unset($myArray[$key]['id']); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.