用于前端目的的多维到简单数组

时间:2018-03-17 14:15:40

标签: php arrays multidimensional-array

我有如下数组:

Array(
    [top_menu] => Array(
        [li] => Array(
            [a] => Array(
                [clr] => #FF00FF
            )

            [a:hover] => Array(
                [clr] => #FF6600
            )
        )
    )

    [wrap-zfcnt] => Array(
        [a] => Array(
            [clr] => #99CC00
        )

        [p] => Array(
            [ffam] => Comic Sans MS
            [clr] => #FF0000
            [fsz] => 9px
        )

        [a:hover] => Array(
            [clr] => #666699
            [txtd] => underline
        )
    )

    [sidebar-zfcnt] => Array(
        [bgi] => url(/res/uploads/webist/0/3/6815/res/18291/logo.png?1)
    )
)

我想将其转换为如下所示的单维数组:

[top_menu~li~a--clr] => #FF00FF
[top_menu~li~a:hover--clr] => #FF6600
[wrap-zfcnt~a--clr] => #99CC00
[wrap-zfcnt~p--fsz] => 9px
[wrap-zfcnt~p--ffam] => Comic Sans MS
[wrap-zfcnt~p--clr] => #FF0000
[wrap-zfcnt~a:hover--txtd] => underline
[wrap-zfcnt~a:hover--clr] => #666699
[sidebar-zfcnt--bgi] => url(/res/uploads/webist/0/3/6815/res/18291/logo.png?1)

以下是我的功能:

function m_dim_to_s_dim_css ($array, $el='') {
    $style = '';
    $style_block = '';

    $i=0;
    foreach ($array as $element => $styles ) {
        if ( is_array($styles) ) {
            if($el != '') {
                $element = $el.'~'.$element;
            }

            $cStyle = m_dim_to_s_dim_css($styles, $element);
            $style .= $cStyle;
        } else {
            if($i > 0) {

            }
            $style_block .= "--".$element;
        }
        $i++;
    }

    if (!empty($style_block)) {
        $style .= $el.$style_block."=>".$styles."|||";
    }

    return $style;
}

这就是它的回归:

Array
(
    [top_menu~li~a--clr] => #FF00FF
    [top_menu~li~a:hover--clr] => #FF6600
    [wrap-zfcnt~a--clr] => #99CC00
    [wrap-zfcnt~p--ffam--clr--fsz] => 9px
    [wrap-zfcnt~a:hover--clr--txtd] => underline
    [sidebar-zfcnt--bgi] => url(/res/uploads/webist/0/3/6815/res/18291/logo.png?1)
    [] => 
)

感谢任何帮助

2 个答案:

答案 0 :(得分:1)

她有正确的递归循环:

<?php
$inArray = [
  'top_menu' => [
    'li' => [
      'a' => ['clr' => '#FF00FF'],
      'a:hover' => ['clr' => '#FF6600'],
    ],
  ],
  'wrap-zfcnt' => [
    'a' => ['clr' => '#99CC00'],
    'p' => [
      'ffam' => 'Comic Sans MS',
      'clr' => '#FF0000',
      'fsz' => '9px',
    ],
    'a:hover' => [
      'clr' => '#666699',
      'txtd' => 'underline',
    ],
  ],
  'sidebar-zfcnt' => ['bgi' => 'url(/res/uploads/webist/0/3/6815/res/18291/logo.png?1)',],
];

$reducedArray = [];

function walkStyleArray($toWalk, $keyPrefix = "") {
  global $reducedArray; //need for access outside function definied array
  if(is_array($toWalk)) {
    foreach($toWalk as $key => $value) {
      if(is_array($value)) {
        walkStyleArray($value, $keyPrefix . ($keyPrefix === "" ?"":"~") . $key);
      } else {
        $newKey = $keyPrefix  . ($keyPrefix === "" ?"":"--") . $key;
        $reducedArray[$newKey] = $value;
      }
    }
  }
}

walkStyleArray($inArray);

// print_r($inArray);
print_r($reducedArray);

最后结果:

Array
(
    [top_menu~li~a--clr] => #FF00FF
    [top_menu~li~a:hover--clr] => #FF6600
    [wrap-zfcnt~a--clr] => #99CC00
    [wrap-zfcnt~p--ffam] => Comic Sans MS
    [wrap-zfcnt~p--clr] => #FF0000
    [wrap-zfcnt~p--fsz] => 9px
    [wrap-zfcnt~a:hover--clr] => #666699
    [wrap-zfcnt~a:hover--txtd] => underline
    [sidebar-zfcnt--bgi] => url(/res/uploads/webist/0/3/6815/res/18291/logo.png?1)
)

为记录编辑1:

将数组越走越深,直到值满足(这是reurrency break case),然后创建reducedArray的元素,不需要任何其他任何计数。

编辑2个没有global语句的walkStyleArray函数

$reducedArray = [];

function walkStyleArray(&$resultArray, $toWalk, $keyPrefix = "") {
  if(is_array($toWalk)) {
    foreach($toWalk as $key => $value) {
      if(is_array($value)) {
        walkStyleArray($resultArray, $value, $keyPrefix . ($keyPrefix === "" ?"":"~") . $key);
      } else {
        $newKey = $keyPrefix  . ($keyPrefix === "" ?"":"--") . $key;
        $resultArray[$newKey] = $value;
      }
    }
  }
}

walkStyleArray($reducedArray, $inArray);

答案 1 :(得分:0)

您正在寻找的功能称为array_reduce

您有一个像$a = array(array(1,2,3), array(4,5), array(6,7,8,9))

这样的数组

当你print_r那个数组时,它将为你提供这样一个多维数组结构:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
        )

    [2] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
            [3] => 9
        )
)

并且您希望将其合并为一个单独的数组,如下所示:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

你只需制作一个新阵列

$a是您原来的多维数组。 $result是您新的一维数组。

$result = array_reduce($a, 'array_merge', array());

希望我能帮助你解决问题。