2017-07-07 71 views
2

我被困在這方面,我有多個關聯數組,我想在轉換爲一個: - 這裏是數組: -如何關聯數組轉換到一個數組

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

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

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

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

我想輸出的東西像這樣: -

Array 
(

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

這裏到目前爲止,我曾嘗試: -

$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; 

我使用laravel框架的PHP。在此先感謝:)

+0

嘗試[array_flatten](https://laravel.com/docs/5.4/helpers #method-array-flatten) –

+0

嘿@JoeBlack謝謝 –

+0

發表回答我會接受它 –

回答

1

可以使用Laravel輔助函數array_flatten此:

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

$result = array_flatten($array); 

var_dump($result); 

輸出:

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

試試這個:

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

在第一的foreach你正在裏面陣列陣列和第二的foreach你會得到的值。將這些值插入到新數組中以獲得所需的結果。使用print_r看到的結果

+0

太複雜請參閱@JoeBlack回答 –

1

只要您可以使用: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); 
?> 

輸出:

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

入住這裏:https://eval.in/829111

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

+0

Upvote for your answer also –