【问题标题】:Move or shift php array keys [duplicate]移动或移动php数组键[重复]
【发布时间】:2020-10-17 02:13:30
【问题描述】:

不太清楚如何正确表达这一点,但我正在寻找一些帮助来移动/移动数组键,以便顶级数组不包含另一个只有一个项目的数组。基本上是这样的:

[0] => Array
    (
        [0] => Array
            (
                [_id] => 3
                [title] => Award winning wedding venue
                [subtitle] => Creating a website to reflect the prestige of the brand
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [_id] => 5
                [title] => Bringing storytelling to life
                [subtitle] => Bringing storytelling to life
            )

    )

变成这样:

[0] => Array
    (
        [_id] => 3
        [title] => Award winning wedding venue
        [subtitle] => Creating a website to reflect the prestige of the brand
    )

[1] => Array
    (
        [_id] => 5
        [title] => Bringing storytelling to life
        [subtitle] => Bringing storytelling to life
    )

几乎只是将数组键向上移动一个。

原始数组是使用以下方法创建的:

// Start with manual relation otherwise default to next/prev    
    foreach ($item['related'] as $id) {
      
      $related[] = perch_collection('Projects', [
        'filter' => [
          [
            'filter' => '_id',
            'match'  => 'eq',
            'value'  => $id,
          ],
            // Item is enabled
          [
            'filter' => 'status',
            'match' => 'eq',
            'value' => 'enabled',
          ],
        ],
        'skip-template' => true,
      ], true);
    }

【问题讨论】:

  • 您是否可以控制数组的创建方式。这通常是解决此类问题的最有效方法。
  • 我做了一部分,我将编辑问题以显示代码示例。
  • 尝试将方法调用的最后部分更改为 ], true)[0]; 并加上额外的 [0] 来表示获取结果的第一个元素。
  • 感谢@NigelRen,这也有效。虽然我没想到。
  • 不打算用stackoverflow.com/q/6193946/2943403 结束这个,因为Grumpy 已经针对这个特殊情况给出了更合适的建议。

标签: php arrays sorting


【解决方案1】:

解决此问题的最佳方法是从源头上解决。这看起来像是从数据库接收的数据集,因此您可以尝试以正确的格式生成它,而不是在收到数组后尝试对其进行操作。大多数 DAL 都有方法来操作结果集的返回类型。

但是,如果这是不可能的,并且您总是只有一个嵌套元素,那么这个循环应该可以解决问题。

for($i = 0; $i <= count($array); $i++) {
    $shifted[$i] = $array[$i][0];
}

【讨论】:

  • 不是每个循环都调用count(),使这种方法非常昂贵吗?当然,要解决这个问题,您可以在循环之前声明 $array 的计数。除非您正在执行数千行,否则它可能也并不重要。
  • @GrumpyCrouton 是的,我只是在午休时间快速写了那件事。此外,count 的性能非常好,甚至数千行也可能不会导致明显的性能下降。但总的来说,在循环之前声明计数会更简洁。
【解决方案2】:

最好修改数组的创建,而不是事后更改。

// Start with manual relation otherwise default to next/prev    
foreach ($item['related'] as $id) {
  
    $related[] = perch_collection('Projects', [
        'filter' => [
            [
                'filter' => '_id',
                'match'  => 'eq',
                'value'  => $id,
            ],
            // Item is enabled
            [
                'filter' => 'status',
                'match' => 'eq',
                'value' => 'enabled',
            ],
        ],
        'skip-template' => true,
  ], true)[0];
}

注意perch_collection() 函数调用末尾的[0]。这与我的答案的第二部分基本相同,只是发生得更早。


话虽如此,如果您仍然想在 创建原始数组后更改它,您可以使用一个简单的 foreach 循环并引用原始数组。

foreach($array as &$arr) {
    $arr = $arr[0];
}

$arr 前面使用&amp;reference。这意味着循环将改变原始数组,因此它可以防止临时数组的开销。


mickmackusa 告诉我另一个使用array_column() 的解决方案,它完全避免了循环。

$array = array_column($array, 0);

【讨论】:

  • array_column() 也可以用来扫地。 stackoverflow.com/a/45417345/2943403,但我同意最好不要一开始就造成混乱。
  • @mickmackusa 这是一个很酷的解决方案,我经常使用array_column(),但从未想过将它与编号索引一起使用。
  • 我无意冒犯,但我最终找到了一个副本,提供了所有必要的指导并提出了问题。
  • @mickmackusa 不,这就是网站的工作方式!没有冒犯。感谢您找到合适的骗子。
  • 我查看了之前关闭的帖子,觉得它偏离了我想扩展到这个问题的指导。这是故意的。不,这是不标准的做法。理想情况下,所有 Stack Overflow 重复项都应该“折叠”到最好和最早的问题。
猜你喜欢
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
相关资源
最近更新 更多