【发布时间】: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 已经针对这个特殊情况给出了更合适的建议。