how to convert associative array in to one array

Issue

I am stuck on this i have multiple associative array and i want to convert in to one:-
Here is the array:-

Array
(
[0] => Array
    (
        [0] => Women
    )

[1] => Array
    (
        [0] => children
        [1] => smile
    )

[2] => Array
    (
        [0] => Abstract
    )

[3] => Array
    (
        [0] => Lion
        [1] => Cheetah
    )
)

I want output something like this:-

Array
(

[0] => Women
[1] => children
[2] => smile
[3] => Abstract
[4] => Lion
[5] => Cheetah
)

Here i have tried so far:-

$getKeywords =  DB::table('contributor_images')->select('keywords')->get();
$getKeywords = json_decode(json_encode($getKeywords),true);
    foreach($getKeywords as $keyword){
        $AllKeywords[] = $keyword['keywords'];
    }
    foreach ($AllKeywords as $key => $ExplodeKeywords) {
        $searchkeywords[] = explode(',',$ExplodeKeywords);
    }
    echo "<pre>"; print_r($searchkeywords); die;

I am using laravel framework of php. THANKS IN ADVANCE 🙂

Solution

You can use Laravel helper function array_flatten for this:

$array = [
    0 => [
        0 => 'Women',
    ],
    1 => [
        0 => 'children',
        1 => 'smile',
    ],
    2 => [
        0 => 'Abstract',
    ],
    3 => [
        0 => 'Lion',
        1 => 'Cheetah',
    ],
];

$result = array_flatten($array);

var_dump($result);

Output:

array (size=6)
  0 => string 'Women' (length=5)
  1 => string 'children' (length=8)
  2 => string 'smile' (length=5)
  3 => string 'Abstract' (length=8)
  4 => string 'Lion' (length=4)
  5 => string 'Cheetah' (length=7)

Answered By – Joe Black

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published