将多维数组转换为单维数组

时间:2010-11-18 16:16:44

标签: php arrays multidimensional-array

如果从函数中返回一个像这样的多维数组..

array(0 => array('a' => 5), 1 => array('a' => 8))

但我真的需要键'a'的内容,这是我转换的最佳方式。

目前我一直在做类似的事情。

$new_array = array();

foreach ($multi_array AS $row) {
    $new_array[] = $row['a']
}

4 个答案:

答案 0 :(得分:4)

没有foreach:

$array = array(0 => array('a' => 1), 1 => array('a' => 8));

$new_array = array_reduce($array, function ($result, $current) {$result[]=current($current); return $result;}, array());

答案 1 :(得分:2)

如果这是您的全部要求,我认为这是最好的方式。其他任何东西都会有相同的处理。即使在查看了Array函数之后,我也会说这是最好的方法。但是,你可以使它成为一种功能,使其更具通用性:

$array = array(0 => array('a' => 1), 1 => array('a' => 8));

$new_array = flatten($array, 'a');    

function flatten($array, $index='a') {
     $return = array();

     if (is_array($array)) {
        foreach ($array as $row) {
             $return[] = $row[$index];
        }
     }

     return $return;
}

但是,我会说你拥有的将是最有效的方式。

答案 2 :(得分:0)

你可以尝试像跟随......

    $multi_array = array(0 => array('a' => 5), 1 => array('a' => 8));
    $new_array = array();

    foreach ($multi_array AS $key => $value) {
        $new_array[] = $value['a'];
    }

答案 3 :(得分:0)

我最近发现自己面临这个问题,我相信我找到了解决方案。 我将回顾问题本身,以及解决方案,试图解释一切。

问题在于我没有二维数组,但是数组中可能有任意数量的数组,因此Brad F Jacobs的解决方案不适用于此,尽管它非常简单和实用

我不得不使用一个名为'webpage'的自引用数据库表,其中一列是'parentWebpageId',它引用了同一个表中某些其他行的Id。这样,如果你的循环正确,就可以构建并轻松管理树形结构。

我很容易创建一个函数,它应该从一维自引用数组生成一个多维数组,但是当我试图创建一个相反的函数时出现了问题。我需要这个,因为如果我想删除某个网页,也应该删除它的所有子网,以保持自我参照的完整性。

很容易生成一个树,其根目录是最初要删除的页面,但之后我需要一个包含所有子网页ID的列表,以便删除所有这些。

所以,我的结构是这样的:

webpage1
    id
    title
    ...
    childWebpageArray
webpage2
    id
    title
    ...
    childWebpageArray
        webpage2.1
            id
            url
            ...
            childWebpageArray
        webpage2.2
            id
            url
            ...
            childWebpageArray
                webpage2.2.1
                    id
                    url
                    ...
                    childWebpageArray
                webpage2.2.2
                    id
                    url
                    ...
                    childWebpageArray
        webpage2.3
            id
            url
            ...
            childWebpageArray
webpage3
    id
    title
    ...
    childWebpageArray

正如你所看到的,深度可以永远存在。

我想出的是:

function flattenMultidimensionalArray($multidimensionalArray) {

    // Set anchor.
    ohBoyHereWeGoAgain:

    // Loop through the array.
    foreach ($multidimensionalArray as $key1 => $value1) {

        // Check if the webpage has child webpages.
        if (isset($multidimensionalArray[$key1]["childWebpageArray"]) && (count($multidimensionalArray[$key1]["childWebpageArray"]) > 0)) {

            // If it does, loop through all the child webpages, and move them into the initial multi-dimensional array.
            foreach ($multidimensionalArray[$key1]["childWebpageArray"] as $key2 => $value2) {
                $multidimensionalArray[] = $multidimensionalArray[$key1]["childWebpageArray"][$key2];
            }

            // Unset element's child pages, because all those child pages, along with their child pages
            // have been moved into the initial array, thus reducing the depth of a multi-dimensional array by 1.
            unset($multidimensionalArray[$key1]["childWebpageArray"]);
        }
    }

    // Loop once again through the whole initial array, in order to check if any of the pages has children
    foreach ($multidimensionalArray as $key => $value) {

        // If a page which has children is found, kick the script back to the beginning, and go through it again.
        if (isset($multidimensionalArray[$key]["childWebpageArray"]) && (count($multidimensionalArray[$key]["childWebpageArray"]) > 0)) {
            goto ohBoyHereWeGoAgain;
        }
    }

    // In the end, when there are no more pages with children, return an array of pages.
    return $multidimensionalArray;

}

这个解决方案在我的案例中起作用,我认为这是解决这类问题的正确方法。为了满足您的特殊需求,改变它可能不是一件容易的事。

希望这有帮助!

相关问题