0

I need to change the format of an array returned in CodeIgniter, leaving a field of the database as index, I made the same querys in PHP and CodeIgniter and both are different, any suggestions? I am using result_array () in CodeIgniter and also result ();

I need this:

Array
(
    [1_1] => Array
        (
            [0] => 1_1
            [1] => 16
            [2] => ch
            [3] => Chemistry
        )

    [1_2] => Array
        (
            [0] => 1_2
            [1] => 17
            [2] => ch
            [3] => Chemistry
        )

    [2_3] => Array
        (
            [0] => 2_3
            [1] => 18
            [2] => ch
            [3] => Chemistry
        )

    [2_5] => Array
        (
            [0] => 2_5
            [1] => 19
            [2] => ch
            [3] => Chemistry
        )

    [9_1] => Array
        (
            [0] => 9_1
            [1] => 20
            [2] => ch
            [3] => Chemistry
        )

)

Query results:

Array
(
    [0] => Array
        (
            [pos] => 1_1
            [tbl_id] => 16
            [sub_id] => ch
            [sub_name] => Chemistry
        )

    [1] => Array
        (
            [pos] => 1_2
            [tbl_id] => 17
            [sub_id] => ch
            [sub_name] => Chemistry
        )

    [2] => Array
        (
            [pos] => 2_3
            [tbl_id] => 18
            [sub_id] => ch
            [sub_name] => Chemistry
        )

    [3] => Array
        (
            [pos] => 2_5
            [tbl_id] => 19
            [sub_id] => ch
            [sub_name] => Chemistry
        )

    [4] => Array
        (
            [pos] => 9_1
            [tbl_id] => 20
            [sub_id] => ch
            [sub_name] => Chemistry
        )

)

How can I delete the array that is at the beginning? Thank you.

3
  • 2
    Use array_column to reindex, but why remove string keys in inner arrays??? Commented Jan 22, 2019 at 20:29
  • trivial with a foreach() loop Commented Jan 22, 2019 at 20:31
  • simply use foreach as others says Commented Jan 23, 2019 at 7:02

1 Answer 1

1
$array = [
    [
        'pos' => '1_1',
        'tbl_id' => 16,
        'sub_id' => 'ch',
        'sub_name' => 'Chemistry'
    ]
];


$result = [];
foreach ($array as $data) {
    $values = array_values($data);

    $result[$data['pos']] = $values;
}

This will give you what you want.

Array
(
    [1_1] => Array
        (
            [0] => 1_1
            [1] => 16
            [2] => ch
            [3] => Chemistry
        )

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