2

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

4
  • Try array_flatten Commented Jul 7, 2017 at 5:38
  • hey @JoeBlack thanks Commented Jul 7, 2017 at 5:41
  • post answer i will accept it Commented Jul 7, 2017 at 5:41
  • Ok, posted as answer. Commented Jul 7, 2017 at 5:54

4 Answers 4

1

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

Comments

1

Try this:

foreach ($old as $data) {
            foreach ($data as $value) {
                $new[] = $value;
            }
        }
        print_r($new);
    }

In first foreach you are getting array inside array and in second foreach you will get the value. Insert these values in new array to get desired result. Use print_r to see the result

1 Comment

too complex see @JoeBlack answer
1

Simply you can use : call_user_func_array

<?php
$array = array (
  0 => 
  array (
    0 => 'Women',
  ),
  1 => 
  array (
    0 => 'children',
    1 => 'smile',
  ),
  2 => 
  array (
    0 => 'Abstract',
  ),
  3 => 
  array (
    0 => 'Lion',
    1 => 'Cheetah',
  ),
);

$result = call_user_func_array('array_merge', $array);

print_r($result);
?>

Output :

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

Check here : https://eval.in/829111

Ref : http://php.net/manual/en/function.call-user-func-array.php

Comments

0

Since Laravel 5.7 the syntax is Arr::flatten($array) so that now it looks like

use Illuminate\Support\Arr;

$array = [
    0 => [
        0 => "Women"
    ],
    1 => [
        0 => 'children',
        1 => 'smile'
    ],
    2 => [
        0 => 'Abstract'
    ],
    3 => [
        0 => 'Lion',
        1 => 'Cheetah'
    ]
];
Arr::flatten($array);

Output :

array:6 [
  0 => "Women"
  1 => "children"
  2 => "smile"
  3 => "Abstract"
  4 => "Lion"
  5 => "Cheetah"
]

Docs be here

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.