1

I have an array as follows

Array
(
    [0] => Array
        (
            [route_id] => 2/2A
            [direction] => right
            [bus_stop_count] => 1
            [bus_id] => Array
                (
                    [0] => 1000
                    [1] => 1002
                )

        )

    [1] => Array
        (
            [route_id] => 1
            [direction] => right
            [bus_stop_count] => 1
            [bus_id] => Array
                (
                    [0] => 1004
                )

        )

)

I want to get an array for bus_id like the following

Array
(
    [0] => 1000
    [1] => 1002
    [2] => 1004
)

Here is what i tried so far

$bus_ids = array_column($array, 'bus_id');

Array
(
    [0] => Array
        (
            [0] => 1000
            [1] => 1002
        )

    [1] => Array
        (
            [0] => 1004
        )

)
3
  • What have you tried so far or are you just wanting us to write this for you? Commented Apr 3, 2017 at 16:37
  • But you probably need a SELECT bus_id from t Commented Apr 3, 2017 at 16:41
  • @Styphon sorry mate i forgot to add it...check edit Commented Apr 3, 2017 at 16:44

2 Answers 2

4

That should work:

$a = array(...);
call_user_func_array('array_merge', array_column ($a, 'bus_id'))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help
0

You can use a for loop:

$newArr = Array();
foreach ($arr as $value) $newArr = array_merge($newArr, $value["bus_id"]);

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.