【问题标题】:PHP formatting Array to A New ArrayPHP 将数组格式化为新数组
【发布时间】:2019-05-19 04:06:24
【问题描述】:

大家好,我是 PHP 的初学者,希望在性能方面以最佳方式转换此数组:

$old = array(
    20 =>
        array(
            'name' => 'Heels',
            'path' => '1/2/10/15/20',
        ),
    15 =>
        array(
            'name' => 'Sandals',
            'path' => '1/2/80/96/15',
        ),
    10 =>
        array(
            'name' => 'Trainers',
            'path' => '1/2/80/96/10',
        ),
);

到这里:

$new = array(
    20 =>
        array(
            'value' => 20,
            'label' => 'Trainers > Sandals > Heels',
        ),
);

肯定会有大量记录爆炸路径并使用 id 映射它们会降低性能,只是想知道是否有更有效的方法,如果可能,谢谢。

【问题讨论】:

  • 如果path(你解释创建亲戚)不同,我迷失了价值20的来源?请提供输入和输出的最小、完整或可验证示例。
  • 20 是每条路径的结束编号,但它也是关键,因此如果有意义,最终结果需要排除每条路径中的每个键名称值 1 和 2。跨度>
  • @NewHorizo​​n 这没有意义。你能在这里澄清你的逻辑吗?您说“20 是每条路径的结束编号”,但 '1/2/80/96/15' 中没有 20。
  • 这些只是虚拟数据的假数字,但所有内容都会匹配最后三个数字。
  • 提问时发布正确的数据

标签: php arrays foreach mapping


【解决方案1】:

如果我理解正确,您正在尝试获取与每个类别相关的最新路径并将其作为面包屑输出。

您可以先对键(id)进行排序,然后循环遍历创建面包屑的数组。

arsort($paths); # This gives the desired output in OP but makes more sense to use krsort() to sort DESC not ASC

$breadcrumb = (object) array (
    'value' => array_keys($paths)[count($paths) - 1], # Get the highest id - if using krsort() use array_keys($paths)[0]
    'labels' => implode(' > ', array_column($paths, 'name'));
);

# Update derived from The fourth bird's answer which skips the need for the foreach().
# Concept is to build an array of the labels to then make look pretty with the > effect

这是demo

输出:

object (stdClass) (2) {
    ["value"] => int(20)
    ["labels"] => string(26) "Trainers > Sandals > Heels"
}

【讨论】:

    【解决方案2】:

    另一个选项可能是首先创建键和名称的映射器。然后您可以从映射器中获取密钥来创建路径:

    $result = [];
    $mapper = array_combine(array_keys($old), array_column($old, 'name'));
    foreach ($old as $key => $value) {
        $path = implode(' > ', array_map(function($x) use ($mapper) {
            return $mapper[(int)$x];
        }, explode('/', $value['path'])));
    
        $result[$key] = ['value' => $key,'label' => $path];
    }
    
    print_r($result);
    

    Php demo

    【讨论】:

    • 性能的好主意,也很好地使用array_column(),而不是循环创建一个全新的变量来使用implode()的名称。希望你不介意我偷那个来更新我的问题;)很好的答案。
    • 这个答案是最动态的解决方案,我不明白为什么它没有更多的选票。我欣赏不仅将 OP 输入与输出相匹配的概念,而且允许放置更多输入以查询 OP 输出的意图。老实说,很好的解决方案,让我研究了“映射器”。
    【解决方案3】:

    这是硬编码的方式,但我认为您需要提供更多信息才能获得动态解决方案。

    <?php
    
    $old = array(
        20 =>
            array(
                'name' => 'Heels',
                'path' => '1/2/10/15/20',
            ),
        15 =>
            array(
                'name' => 'Sandals',
                'path' => '1/2/80/96/15',
            ),
        10 =>
            array(
                'name' => 'Trainers',
                'path' => '1/2/80/96/10',
            ),
    );
    
    
    ksort($old);
    $breadcrumbs = [];
    $currentKey = 0;
    
    foreach ( $old as $itemKey => $item) {
        $currentKey = $itemKey;
        $breadcrumbs[] = $item;
    
    }
    $new = [$currentKey] = [
        'value' => $currentKey,
        'label' => implode(' > ', $breadcrumbs)
    ];
    
    printf($new);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2020-02-27
      相关资源
      最近更新 更多