php - 如何将关联数组转换为一个数组

标签 php arrays laravel associative-array

我被困在这个问题上,我有多个关联数组,我想转换为一个:- 这是数组:-

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/

相关文章:

java - 将数组传递给测试类构造函数

c++ - C++ 中的整数集

Laravel Spark Next 在等待订阅时挂起

php - 我需要从 PHP 脚本执行 C 程序

php - PHP 有没有办法显示页面上的每个 MySQL 查询?

php - MySQL 类别列表与单独的类别表

java - 方法在应该返回 -1 时返回 NullPointerException

laravel - 如何指定 Laravel 为迁移表使用哪个数据库连接?

php - 管理员的 Laravel Mass Assignment

php - Codeigniter 循环连接多个结果的正确方法?