如何使用PHP将多维数组转换为单一数组?

13

在实现数据库查询后,我得到了以下多维数组。

二维数组

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

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

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

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

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

)

但我想将它转换为单一维度的数组,格式如下:

单一维度数组

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

我该怎么做?


1
它是通过MySQL查询动态生成的。在HTML中,你会看到什么?@AbdulWaheed - Karan Adhikari
展示创建第一个数组的代码,那里是你遇到问题的地方。 - RiggsFolly
1
可能是将多维数组转换为一维数组的重复问题。 - Rahul
@RyanVincent 这个问题有多个答案,包含不同的解决方案。但我认为这个问题对其他人很有帮助,却没有得到足够的关注。我使用了“悬赏”功能来向其他用户展示这个问题,并获得了新的解决方案。 - Mohammad
@RyanVincent Stackoverflow 将会评判。 获胜的最高票答案必须至少从2分开始才能获得赏金 - Mohammad
显示剩余4条评论
18个回答

1
这会起作用。
$array = array_column($array, 't1');

注意:此函数array_column在PHP 5.5中引入,因此在早期版本中无法使用。

1

嗨 @Karan Adhikari,简单如下:

<?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

1
请尝试这个函数:

function array_merging($multi_array) { 
    if (is_array($multi_array)) { 
        $new_arr = array(); 
        foreach ($multi_array as $key => $value) { 
            if (is_array($value)) { 
            $new_arr = array_merge($new_arr, array_merging($value)); 
            } 
            else { 
                $new_arr[$key] = $value; 
            } 
        } 
        return $new_arr; 
    }
    else {
        return false;
    }
}

使用此函数:

$your_multi_arr = array(array(array('t1'=>'test1'),array('t2'=>'test2'),array('t3'=>'test3'),array('t4'=>'test4')));
$arr1 = array_merging($your_multi_arr);
echo "<pre>";
print_r($arr1);

希望这对您有用。

1
您可以尝试使用PHP的whilelisteach来遍历数组。我从PHP网站上获取了示例代码,第二个示例可以在这里查看。
$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);

创建的输出为

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

你可以在这个 沙盒 示例中自己测试。


0
`$result = "Query";  $key_value = array();`

      foreach ($result as $key => $value) {
          $key_value[$key['']] = $value[''];
      }
     //for checking //echo "<pre>" ; print_r($key_value) ; exit;
      return $key_value;

请填写 $key['在 SQL 查询中给出的字段名称']$value['在 SQL 查询中给出的字段名称'](两者相同)。

0

这对我有效

 $result = [];
              foreach($excelEmails as $arr)
              {
                  foreach ($arr as $item){
                      $result = array_merge($result , $item);
                  }
              }
              dd($result);

0
遍历数组并保存键值,在此处查看演示
<?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);

-1

我建议将所有的二维数组转换为一维数组。

<?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);


?>

通过这个脚本,我们可以使用键和值创建新的单维数组。希望对您有所帮助。
您可以在这里查看示例:Array Convert Demo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接