0

I have an array. after print_r($arr) as below:

Array ( 
  [0] => Array ( 
      [groupid] => 5 
      [radminid] => 1 
      [type] => system 
      [system] => private 
  ) 
  [1] => Array ( 
      [groupid] => 10 
      [radminid] => 2 
      [type] => system 
      [system] => private 
  )
)

I would like to change the array key to groupid, something like $arr[$arr[groupid]] and I tried

foreach($array as $key => $value){
    $arr[$value] = $arr[$arr['groupid']];
}

How to use the $arr[groupid] as $arr key? below is what I need.

Array ( 
  [5] => Array ( 
      [groupid] => 5 
      [radminid] => 1 
      [type] => system 
      [system] => private 
  ) 
  [10] => Array ( 
      [groupid] => 10 
      [radminid] => 2 
      [type] => system 
      [system] => private 
  )
)

Thank you.

5 Answers 5

9

You can use

$array = array_column($array, null, 'groupid');

array_column — Return the values from a single column in the input array

Note:
column_key The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

In case you don't have the support of array_column. Please see link.

Demo

Output

Array
(
    [5] => Array
        (
            [groupid] => 5
            [radminid] => 1
            [type] => system
            [system] => private
        )

    [10] => Array
        (
            [groupid] => 10
            [radminid] => 2
            [type] => system
            [system] => private
        )

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

2 Comments

Hi, my php version is 5.2.8, feel doesn't support array_column.. is it any other solution for lower version
you are so nice.
1

array_column is what you are looking for,

$array = array_column($array,null,"groupid");

Comments

1

You only need to iterate through your array and replace your key with your wanted key.

$newArray = array();
for($i=0;$i<count($array);$i++){
    $newArray[$array[$i]['groupid']] = $array[$i];
}
var_dump($array);

2 Comments

since my php version < 5.5, and this is suitable for me, but I don't know why, if using $i<count($array), just equal to 10, but actual in array have 20
Should not happend at all see the offical doc php.net/manual/de/function.count.php
0

You may use array_reduce for this kind of operations (sandbox):

$array = array_reduce($array, function ($accu, $curr) {
    $accu[$curr['groupid']] = $curr;
    return $accu;
}, []);

Output:

Array
(
    [5] => Array
        (
            [groupid] => 5
            [radminid] => 1
            [type] => system
            [system] => private
        )

    [10] => Array
        (
            [groupid] => 10
            [radminid] => 2
            [type] => system
            [system] => private
        )

)

1 Comment

That might work, but it doesn’t make the most sense … purpose of that function is to reduce the array to a single value. Using array_walk as suggested in a different answer makes much more sense, logically.
0

Use array_walk

 array_walk($a, function($v) use(&$r){$r[$v['groupid']] = $v;});

Working example : https://3v4l.org/rB0UT

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.