【问题标题】:Looping through an array which has multiple arrays and last array is not in the same order?循环遍历具有多个数组和最后一个数组的数组的顺序不同?
【发布时间】:2018-01-13 18:19:46
【问题描述】:

我从另一个 php 函数获取以下数组,因此最后一个数组的顺序与其余数组不同:

Array
    (
        [2017] => Array
            (
                [Feb] => Array
                    (
                        [P] => 11
                        [T] => 0
                        [U] => 0
                        [E] => 0
                    )

                [Mar] => Array
                    (
                        [P] => 20
                        [T] => 0
                        [U] => 0
                        [E] => 0
                    )

                [Apr] => Array
                    (
                        [P] => 12
                        [T] => 0
                        [U] => 0
                        [E] => 0
                    )

                [May] => Array
                    (
                        [P] => 21
                        [T] => 0
                        [U] => 0
                        [E] => 0
                    )

                [Jun] => Array
                    (
                        [E] => 0
                        [P] => 7
                        [T] => 0
                        [U] => 1
                    )

            )

    )

为了将此 php 数组数据转换为 JS 数据源,我想出了以下 php 函数

   function phpToJS($inputPHP) {
            $return = '';
            foreach ($inputPHP as $year => $months) {
                foreach ($months as $monthValue => $codeValues) {
                    $sum = 0;
                    $return .= "['" . $year . " - " . $monthValue . "', ";
                    foreach ($codeValues as $key => $code) {
                        if ($key == '') { 
                            $return .= $code;
                        } else {
                            $return .= $code . ',';
                        }
                        $sum += $code;
                    }

                    $return .= ",'" . $sum . "'],";
                }
            }


return $return;
    }

它几乎可以工作,除了最后一个数组。由于顺序错误,似乎没有只看位置的关键“名称”,所以输出看起来像

 ['2017 - Feb', 11,0,0,0,'11'],
['2017 - Mar', 20,0,0,0,'20'],
['2017 - Apr', 12,0,0,0,'12'],
['2017 - May', 21,0,0,0,'21'],
['2017 - Jun', 0,7,0,1,'8'] <---wrong ['2017 - Jun', 7,0,1,0,'8']

那么我怎样才能确定正确的和数在正确的位置呢?

【问题讨论】:

  • 总是PTU,然后是E?然后直接访问这些键,而不是遍历$codeValues
  • 并且编码为 json 必须使用json_encode
  • 还有,为什么最后一个数组不一样?
  • 还有——和stackoverflow.com/questions/48241343/…有什么区别?
  • 阅读你所有的问题,你的错误在字符串ksort($output[$year][$month]);中。

标签: php arrays json multidimensional-array


【解决方案1】:
<?php

$data = Array(2017 => Array(
                'Feb' => Array(
                        'P' => 11,
                        'T' => 0,
                        'U' => 0,
                        'E' => 0
                    ),
                'Mar' => Array
                    (
                        'P' => 20,
                        'T' => 0,
                        'U' => 0,
                        'E' => 0,
                    ),
                'Apr' => Array
                    (
                        'P' => 12,
                        'T' => 0,
                        'U' => 0,
                        'E' => 0,
                    ),
                'May' => Array
                    (
                        'P' => 21,
                        'T' => 0,
                        'U' => 0,
                        'E' => 0,
                    ),
                'Jun' => Array
                    (
                        'E' => 0,
                        'P' => 7,
                        'T' => 0,
                        'U' => 1,
                    ),
            )

    );
function reOrder($codeValues) {
    return Array(
        'P' => $codeValues['P'],
        'T' => $codeValues['T'],
        'U' => $codeValues['U'],
        'E' => $codeValues['E'],
    );
}
function phpToJS($inputPHP) {
            $return = '';
            foreach ($inputPHP as $year => $months) {
                foreach ($months as $monthValue => $codeValues) {
                    $sum = 0;
                    $return .= "'" . $year . " - " . $monthValue . "', ";
                    $codeValues=reOrder($codeValues);
                    foreach ($codeValues as $key => $code) {
                        if ($key == '') { 
                            $return .= $code;
                        } else {
                            $return .= $code . ',';
                        }
                        $sum += $code;
                    }

                    $return .= ",'" . $sum . "',";
                }
            }


return $return;
}
echo phpToJS($data);
?>

试试这个。

【讨论】:

    【解决方案2】:

    第一件事。如果您了解输入和输出键的顺序并且这不是一项庞大的工作,您可以手动完成。例如

    public function orderMonth(array $month)
    {
        return [
            'P' => $month['P'],
            'T' => $month['T'],
            'U' => $month['U'],
            'E' => $month['E'],
        ];
    }
    
    $month = orderMonth($month);
    

    第二个音符。三重嵌套循环看起来很糟糕。最佳实践说即使使用 2 个嵌套级别也不是一件好事,所以你应该避免它。因此,您可以使用原生 php 函数(例如 array_sumimplode)来清理代码。

    foreach ($months as $month) {
        $sum = array_sum($month);
        $monthString = implode(',', array_merge($month, [$sum]));
        // other code
    }
    

    最后。正如我从要转换为 JSON 的函数名称中所理解的那样。为此,您可以使用 json_encode 函数而不是创建自己的自行车 :) 或者您想做什么?

    【讨论】:

    • tx,用于您的 orderMonth 函数。在我把它作为 inputArray 之前我需要使用它,对吗?关于您的第二个建议,我不确定如何将其合并到我的 phpToJS 函数中。这就是我到底需要的,见arrayToDataTable部分link
    【解决方案3】:

    您可以按键对数组 $months 进行排序。添加 ksort($months); 在 foreach $months 之前

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多