Patrick Mateo
Patrick Mateo

Reputation: 3

PHP: convert multidimensional array to simple array

this is my first post here, im currently working on a project from school. and im having trouble converting this from php multidimensional array:

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

i want to convert that array into this:

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

this data was fetch out from the database using codeigniter. thanks in advance.

Upvotes: 0

Views: 602

Answers (1)

VolkerK
VolkerK

Reputation: 96159

see http://docs.php.net/array_values
and 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);

prints

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,
  ),
)

Upvotes: 3

Related Questions