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:53

Hey @Karan Adhikari Simple like below one:

<?php
    $arr1 = array(array("t1" => "test1"), array("t2" => "test2"), array("t3" => "test3"), array("t4" => "test4"), array("t5" => "test5"));
    echo "<pre>";
    print_r($arr1);//before
    $arr2 = array();
    foreach($arr1 as $val){ 
        $arr2 = array_merge($arr2, $val);
    }
    echo "<pre>";
    print_r($arr2); // after you get your answer
查看更多
萌系小妹纸
3楼-- · 2019-02-16 13:58

try something like this For your limited use case, this'll do it:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

This can be more generalized for when the subarrays have many entries to this:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
查看更多
混吃等死
4楼-- · 2019-02-16 14:00

You can use as follows :

$newArray = array();
foreach($arrayData as $key => $value) {
    foreach($value as $key2 => $value2) {
        $newArray[$key2] = $value2;
    }
}

Where $arrayData is your DB data array and $newArray will be the result.

查看更多
我只想做你的唯一
5楼-- · 2019-02-16 14:00

traverse the array and save the key value, Live Demo here.

<?php
    $array = array(array('t1' => 'test1'), array('t2' => 'test2'), array('t3' => 'test3'), array('t4' => 'test4'), array('t5' => 'test5'));
    $result = [];
    array_walk($array, function($value) use(&$result){
        foreach($value as $k => $v)
        {
            $result[$k] = $v;
        }
    });
    var_dump($result);
查看更多
Animai°情兽
6楼-- · 2019-02-16 14:00

i would recomment my way to convert all double-dimensional array to single-dimensional array.

<?php

  $single_Array = array();

  //example array
  $array = array(
    array('t1' => 'test1'), 
    array('t2' => 'test2'), 
    array('t3' => 'test3'), 
    array('t4' => 'test4'), 
    array('t5' => 'test5'));

 $size = sizeof($array);

 //loop to fill the new single-dimensional array
 for($count = 0; $count<sizeof($array);$count++)
 {
    //take the key of multi-dim array
    $second_cell = key($array[$count]);
    //set the value into the new array
    $single_array[$count] = $array[$count][$second_cell];
 }

 //see the results
 var_dump($single_array);


?>

with this script we can take keys and values to create new single-dimensional array.I hope that i was helpfull to you.

you can see the example here: Array Convert Demo

查看更多
走好不送
7楼-- · 2019-02-16 14:01

You can use this if you don't care about keeping the correct array keys

function flattenA(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}
print_r(flattenA($arr));
// Output
Array
(
    [0] => test1
    [1] => test2
    [2] => test3
    [3] => test4
    [4] => test5
)

Otherwise

function flattenB(array $array) {
    $return = array();
    array_walk_recursive($array, function($v,$k) use (&$return) { $return[$k] = $v; });
    return $return;
}
print_r(flattenB($arr));
// Output
Array
(
    [t1] => test1
    [t2] => test2
    [t3] => test3
    [t4] => test4
    [t5] => test5
)

Check both on Sandbox

From answer on similar question

查看更多
登录 后发表回答