PHP:将多维数组转换为简单数组

时间:2015-11-04 15:49:47

标签: php arrays codeigniter multidimensional-array

这是我在这里的第一篇文章,我目前正在从学校开展一个项目。和 我无法从php多维数组中转换它:

Array
(
[0] => Array
    (
        [Program_ID] => 1001
        [Program_Name] => Computer Engineering
        [Program_NumHours] => 200
        [Curriculum_ID] => 101
    )

[1] => Array
    (
        [Program_ID] => 1002
        [Program_Name] => Civil Engineering
        [Program_NumHours] => 200
        [Curriculum_ID] => 102
    )

[2] => Array
    (
        [Program_ID] => 1003
        [Program_Name] => Electronics Engineering
        [Program_NumHours] => 200
        [Curriculum_ID] => 103
    )

[3] => Array
    (
        [Program_ID] => 1004
        [Program_Name] => Electrical Engineering
        [Program_NumHours] => 200
        [Curriculum_ID] => 104
    )
)

我想将该数组转换为:

Array
(
[0] => Array
    (
        [0] => 1001
        [1] => Computer Engineering
        [2] => 200
        [3] => 101
    )

[1] => Array
    (
        [0] => 1002
        [1] => Civil Engineering
        [2] => 200
        [3] => 102
    )

[2] => Array
    (
        [0] => 1003
        [1] => Electronics Engineering
        [2] => 200
        [3] => 103
    )

[3] => Array
    (
        [0] => 1004
        [1] => Electrical Engineering
        [2] => 200
        [3] => 104
    )
)

使用codeigniter从数据库中取出这些数据。提前致谢。

1 个答案:

答案 0 :(得分:3)

http://docs.php.net/array_values
http://docs.php.net/array_map

<?php
$input = array(
    array('Program_ID' => 1001, 'Program_Name' => 'Computer Engineering', 'Program_NumHours' => 200, 'Curriculum_ID' => 101),
    array('Program_ID' => 1002, 'Program_Name' => 'Civil Engineering', 'Program_NumHours' => 200, 'Curriculum_ID' => 102),
    array('Program_ID' => 1003, 'Program_Name' => 'Electronics Engineering', 'Program_NumHours' => 200, 'Curriculum_ID' => 103),
    array('Program_ID' => 1004, 'Program_Name' => 'Electrical Engineering', 'Program_NumHours' => 200, 'Curriculum_ID' => 104),
);

$result = array_map('array_values', $input);

var_export($result);

打印

array (
  0 => 
  array (
    0 => 1001,
    1 => 'Computer Engineering',
    2 => 200,
    3 => 101,
  ),
  1 => 
  array (
    0 => 1002,
    1 => 'Civil Engineering',
    2 => 200,
    3 => 102,
  ),
  2 => 
  array (
    0 => 1003,
    1 => 'Electronics Engineering',
    2 => 200,
    3 => 103,
  ),
  3 => 
  array (
    0 => 1004,
    1 => 'Electrical Engineering',
    2 => 200,
    3 => 104,
  ),
)