2

Currently I am getting the following output

Array
(
    [0] => Array
    (
        [name] => car
    )

    [1] => Array
    (
        [name] => bike
    )

)

what I need is:

Array
(
    [0] => car
    [1] => bike
)

What I have tried:

print_r(reset($get_vehicle_names));
print_r(current($get_vehicle_names));

foreach($get_vehicle_names as $key => $value)
{
    $newArr[$key] = $value;             
}

but it doesn't seem to be working

1
  • 3
    $arr_output = array_column($arr, 'name') this is it. Commented May 18, 2016 at 5:54

2 Answers 2

8

Just use array_column, you will get what you want.

$arr = array(
        array("name" => "car"),
        array("name" => "bike")
    );
$arr_output = array_column($arr, 'name');

echo '<pre>';
print_r($arr_output);

Result:

Array
(
    [0] => car
    [1] => bike
)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$newArr = array();
foreach($arr as $key=>$value)
{
$newArr[$key] = $value['name']; // You just need to store $value['name']
}
print '<pre>';print_r($newArr);

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.