【问题标题】:Flatten a PHP Array one level based on key根据键将 PHP 数组展平一层
【发布时间】:2011-07-08 02:44:02
【问题描述】:

我目前有一个输出如下的数组:

Array
(
  [events_response] => Array
    (
      [total_events] => 3
      [0] => Array
        (
          [events_list] => Array
            (
              [0] => Array
                (
                  [event] => Array
                    (
                      [id] => 41
                      [ages] => All
                      [buy_link] =>
                      [created] => 2011-06-06 22:13:02
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 7
                              [name] => Metrodome
                              [website] =>
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => 123 Any Street
                                      [city] => My City
                                      [state_province] =>
                                      [postal_code] => 12345
                                      [country] =>
                                    )
                                )
                            )
                        )
                      [1] => Array
                        (
                          [collaborators_list] => Array
                            (
                              [0] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 3
                                      [name] => derp
                                      [website] => http://derp.com/
                                    )
                                )
                              [1] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 4
                                      [name] => "Foo" Bar
                                      [website] => http://www.foobar.com/
                                    )
                                )
                            )
                        )
                      [2] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [1] => Array
                (
                  [event] => Array
                    (
                      [id] => 64
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:35:52
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [2] => Array
                (
                  [event] => Array
                    (
                      [id] => 65
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:36:12
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
            )
        )
    )
)

我需要做的基本上是删除键是 int 的级别。因此,例如,[total_events] 将与 [events_list] 处于同一级别,而两者之间没有 [0]。其他有[<int>] 键的区域也是如此。

我正在使用 PHP,并尝试了各种扁平化它的尝试,但未能完全实现。

提前致谢!

【问题讨论】:

    标签: php arrays multidimensional-array flat


    【解决方案1】:
    function flattenByRemoveingIntKeys($array)
    {
        foreach ($array as $key => $val)
        {
            if (is_array($val))
                $array[$key] = flattenByRemoveingIntKeys($val);
            else if (is_numeric($key))
            {
                $array .= $val[$key];
                unset($array[$key]);
            }
        }
    }
    

    没有真正测试过,但类似的东西应该可以工作。

    【讨论】:

    • 试过了,似乎根本没有改变数组。还是有点搞砸了……
    • “致命错误:无法取消设置字符串偏移量...” on unset($array[$key])
    • 就像我说的,没有经过测试,但想法就在那里。如果你使用递归函数,你应该可以更容易地做到这一点。
    【解决方案2】:

    试试这个:

    function RemoverIntKeys($array) { 
       foreach($array as $key => $value) {
            if(preg_match("/^\d+$/",$key)) {
             unset($array[$key]); 
           }
      }
               return $array;
    }
    
    $array = array("foo" => "baa", 2 => "hehe", "test" => "..", 4 => "hello"); 
    $newarray = RemoverIntKeys($array); 
    echo "<pre>";
    print_r($newarray); 
    

    输出:

     Array
        (
            [foo] => baa
            [test] => ..
        )
    

    【讨论】:

    • 没那么简单,他要把数据保留在编号键下面。因此,您必须将这些数据放在编号键下方,并将其向上移动。
    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 2013-09-12
    • 1970-01-01
    • 2014-04-20
    • 2021-11-15
    • 2011-02-11
    • 2014-11-21
    • 2018-10-08
    相关资源
    最近更新 更多