代码之家  ›  专栏  ›  技术社区  ›  Toby

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

  •  0
  • Toby  · 技术社区  · 14 年前

    如果从函数返回的多维数组是这样的。。

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

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

    现在我一直在做。。

    $new_array = array();
    
    foreach ($multi_array AS $row) {
        $new_array[] = $row['a']
    }
    
    4 回复  |  直到 14 年前
        1
  •  2
  •   Jim    14 年前

    如果这是你所有的要求,我认为这是最好的办法。其他的都会有相同的处理。即使在查看了数组函数之后,我也会说这是最好的方法。但是,您可以将其作为一个函数,使其更加通用:

    $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
  •  4
  •   user1105633    13 年前

    没有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());
    
        3
  •  0
  •   Md. Maruf Hossain    12 年前

    你可以尝试如下。。。。。。

        $multi_array = array(0 => array('a' => 5), 1 => array('a' => 8));
        $new_array = array();
    
        foreach ($multi_array AS $key => $value) {
            $new_array[] = $value['a'];
        }
    
        4
  •  0
  •   Nikola    11 年前

    我最近发现自己面临这个问题,我相信我找到了解决办法。 我将讨论问题本身,以及解决方案,试图解释这一过程中的一切。

    问题是我没有二维数组,但是数组可以有任意数量的数组在数组中,所以Brad F Jacobs的解决方案不能在这里应用,尽管它非常简单和实用。

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

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

    很容易生成一个树,其根目录是最初要删除的页面,但随后我需要一个所有子网页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;
    
    }
    

    这个解决方案在我的情况下是有效的,我相信这是解决此类问题的正确方法。为了满足你的特殊需要而改变它可能并不麻烦。

    希望这有帮助!