PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Yii2- ArrayHelper change index of array

 January 09, 2022     yii, yii2     No comments   

Issue

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?


Solution

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.



Answered By - rob006
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing