我被困在这个问题上,我有多个关联数组,我想转换为一个:- 这是数组:-
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;
我正在使用php的laravel框架。提前致谢:)
最佳答案
您可以使用 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)
关于php - 如何将关联数组转换为一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44963224/