0

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.

3
  • I may be blind but your expected result has the same structure/number of dimensions as the input - only the keys in the "inner" arrays have been changed. Commented Nov 4, 2015 at 15:53
  • yes, i just want to convert the associative array into a simple array Commented Nov 4, 2015 at 15:57
  • See stackoverflow.com/questions/15191903/… Commented Nov 4, 2015 at 16:30

1 Answer 1

3

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,
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.