How to convert multi-dimensional array into single

2019-02-16 13:36发布

After implementing database queries, I am getting the multi-dimensional array below.

Two Dimensional Array

Array
(
    [0] => Array
        (
            [t1] => test1
        )

    [1] => Array
        (
            [t2] => test2
        )

    [2] => Array
        (
            [t3] => test3
        )

    [3] => Array
        (
            [t4] => test4
        )

    [4] => Array
        (
            [t5] => test5
        )

)

but I want to convert it to a single dimensional array, like the format below.

One Dimensional Array

Array (
    t1 => test1
    t2 => test2
    t3 => test3
    t4 => test4
    t5 => test5
)

Please help me to do so

18条回答
做自己的国王
2楼-- · 2019-02-16 13:43

You can use array_reduce() to change values of array. In callback get key of item using key() and select first item using reset().

$newArr = array_reduce($oldArr, function($carry, $item){
    $carry[key($item)] = reset($item);
    return $carry;
});

Check result in demo

查看更多
【Aperson】
3楼-- · 2019-02-16 13:46

Try this function,

function custom_function($input_array)
{
    $output_array = array();
    for ($i = 0; $i < count($input_array); $i++) {
        for ($j = 0; $j < count($input_array[$i]); $j++) {
            $output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
        }
    }
    return $output_array;
}

$arr = custom_function($arr);
print_r($arr);

Give it a try, it will work.

查看更多
疯言疯语
4楼-- · 2019-02-16 13:48
// Multidimensional array
$arrdata = Array(
    '0' => Array(
        't1' => 'test1'
    ) ,
    '1' => Array(
        't2' => 'test2'
    ) ,
    '2' => Array(
        't3' => 'test3'
    )
);

// Convert to a single array
$data = array();
foreach($arrdata as $key => $value) {
    foreach($value as $key1 => $value1) {
        $data[$key1] = $value1;
    }
}
echo $data;
查看更多
Luminary・发光体
5楼-- · 2019-02-16 13:49

You can try traversing the array using PHP while list and each. I took sample code from PHP website the second example you can check it here

$arr = [['t1' => 'test1'],['t2' => 'test2'],['t3' => 'test3'],['t4' => 'test4'],['t5' => 'test5']];
$output = [];
while (list($key, $val) = each($arr)) {
    while (list($k, $v) = each($val)) {
    $output[$k] = $v;
}
}
print_r($output);

Output created is

Array
(
    [t1] => test1
    [t2] => test2
    [t3] => test3
    [t4] => test4
    [t5] => test5
)

You can test it on your own in this Sandbox example.

查看更多
Summer. ? 凉城
6楼-- · 2019-02-16 13:49

For your specific case, i would use array_reduce where i set the init value by an empty array

array_reduce($arr, function($last, $row) {
                return $last + $row;
            }, array());

Result :

array(
    't1' => 'test1',
    't2' => 'test2',
    't3' => 'test3',
    't4' => 'test4',
    't5' => 'test5'
)
查看更多
Juvenile、少年°
7楼-- · 2019-02-16 13:50

Assuming that source array is array of arrays and it has no the same keys:

<?php
$src = [
    ['t1'=>'test1'],
    ['t2'=>'test2'],
    ['t3'=>'test3'],
    ['t4'=>'test4'],
    ['t5'=>'test5'],
];
$result = call_user_func_array('array_merge', $src);

result via var_dump():

array(5) {
  ["t1"]=>
  string(5) "test1"
  ["t2"]=>
  string(5) "test2"
  ["t3"]=>
  string(5) "test3"
  ["t4"]=>
  string(5) "test4"
  ["t5"]=>
  string(5) "test5"
}
查看更多
登录 后发表回答