1

I have an array. This array have a sub arrays. I want to take sub array value to main array keys. Here is my example array:

Array
(
    [0] => Array
        (
            [index-id] => 12
            [title] => Example Title
            [description] => Example Description
        )
    [1] => Array
        (
            [index-id] => 32
            [title] => Example Title
            [description] => Example Description
        )
)

i want to take index-id to main array key my array must be like this

Array
(
    [12] => Array
        (
            [index-id] => 12
            [title] => Example Title
            [description] => Example Description
        )
    [32] => Array
        (
            [index-id] => 32
            [title] => Example Title
            [description] => Example Description
        )
)

How can i do this?

devugur
  • 1,339
  • 1
  • 19
  • 25

2 Answers2

2
$temp = [];
foreach($arr as $k => $v){
    $temp[$v['index-id']] = $v;
}
print_r($temp);

Where $temp is result array, $arr is your array.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Rahul
  • 18,271
  • 7
  • 41
  • 60
2

Short solution using array_column and array_combine functions:

// $arr is your initial array
$result = array_combine(array_column($arr, 'index-id'), $arr);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105