php - 如何在php中将多维关联数组转换为一维数组?

标签 php mysql

我有一个关于如何将多维数组转换为一维数组的快速查询

$teachers=array(array('post_id' => "John Doe",'video_id' => array('Government','English')), array('post_id' => "Steven Lee",'video_id' => array("Math","Science", "PE")),array('post_id' => "Jean Perot", 'video_id' => array("French", "Literature")));

最佳答案

试试这个

function array_values_recursive($ary)  {
    $lst = array();
    foreach( array_keys($ary) as $k ) {
        $v = $ary[$k];
        if (is_scalar($v)) {
            $lst[] = $v;
        } elseif (is_array($v)) {
            $lst = array_merge($lst,array_values_recursive($v));
        }
    }
    return array_values(array_unique($lst)); // used array_value function for rekey
}

$teachers=array(
    array('post_id' => "John Doe",'video_id' => array('Government','English')), 
    array('post_id' => "Steven Lee",'video_id' => array("Math","Science", "PE")),
    array('post_id' => "Jean Perot", 'video_id' => array("French", "Literature")));

$flat = array_values_recursive($teachers);
print_r($flat); // OUTPUT : Array ( [0] => John Doe [1] => Government [2] => English [3] => Steven Lee [4] => Math [5] => Science [6] => PE [7] => Jean Perot [8] => French [9] => Literature )

关于php - 如何在php中将多维关联数组转换为一维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445624/

相关文章:

php - jQuery UI 上的 CKEditor 内联编辑

php - Imagick setColor 不适用于 php

php - 从字符串中的链接中删除基本 URL

php - Symfony2 Monolog 到电子邮件错误为什么 swiftmailer.transport.real 是不存在的服务

mysql - 有条件地选择表格的某些部分

mysql - 如何在 Talend 中将 mysql 数据公开为 SOAP Web 服务

php - 编辑表单 MySQL 时从数据库填充下拉列表

php - 无法 ECHO PostgreSQL 查询结果 - PHP

mysql - 如何使用数据表从 Laravel 中的数据透视表检索数据

带有 mysql 变量的 Mysql 查询在 Zend Framework 1 中不起作用