[英]insert a key => value pair in a PHP associative array at a given position
as mentionned in the title i'd like to add at a given position a key => value pair after checking with an if statement wether the value not empty && > 1.正如标题中提到的,我想在使用 if 语句检查值是否不为空 && > 1 后,在给定的 position 添加一个键 => 值对。
The problem is when I add my key => value in the $content array the pair is added at the end of my array.问题是当我在 $content 数组中添加我的 key => 值时,该对被添加到我的数组末尾。 I want the pair to be added after the 'version' key.我希望在“版本”键之后添加这对。
here is my foreach loop and a piece of the resulting array:这是我的 foreach 循环和结果数组的一部分:
foreach ($nodeChildrenResult['hits']['hits'] as $child) {
if ($child['_type'] === 'folder') {
$content[] = array(
'id' => intval($child['_id']),
'type' => $child['_type'],
'name' => $child['_source']['node']['basename']
);
}else {
$content[] = array(
'id' => intval($child['_id']),
'type' => $child['_type'],
'name' => $child['_source']['node']['name'],
'filename' => $child['_source']['node']['basename'],
'extension' => $child['_source']['node']['extension'],
'date' => [
"import" => $child['_source']['node']['date']['import'],
"update" => $child['_source']['node']['date']['update']
],
'version' => $child['_source']['node']['version'],
'locked' => (isset($child['_source']['node']['locked']) && $child['_source']['node']['locked'] == 1) ? true : false,
'link' => (in_array($child['_source']['node']['extension'], $allowedExtensionForCollab) ) ? [ 'collab' => "/v2/MODULES/COLLAB/?node=".intval($child['_id'])] : [],
'workflow' => $results[intval($child['_id'])]
);
if (!empty($child['_source']['node']['pageCount']) and $child['_source']['node']['pageCount'] > 1) {
$content['pageCount'] = $child['_source']['node']['pageCount'];
}
3 (10) => [
id => 3,
type => 'file',
name => 'tomates',
filename => 'tomates.png',
extension => 'png',
date (2) => [
import => '20201019T122023+0200',
update => '20201019T142127+0200'
],
version => 2,
locked => true,
link (1) => [
collab => '/v2/MODULES/COLLAB/?node=3'
],
workflow (3) => [
0 (2) => [
step (2) => [
id => 1,
name => 'prétraitement'
],
status (3) => [
id => 1,
name => 'resizing',
color => '#c80078'
]
],
1 (2) => [
step (2) => [
id => 2,
name => 'validation'
],
status (3) => [
id => 4,
name => 'validation',
color => '#488818'
]
],
2 (2) => [
step (2) => [
id => 3,
name => 'impression'
],
status (3) => [
id => 5,
name => 'en impression',
color => '#c80078'
]
]
],
pageCount => 3
]
You can use a short if after you assign the version key.分配版本密钥后,您可以使用短 if 。 If the condition is false just set it to null.如果条件为假,只需将其设置为 null。
<?php
$pageCount = 123;
$a[] = [
'version' => '1.0',
'pageCount' => (!empty($pageCount) && $pageCount > 1) ? $pageCount : null,
'name' => 'Peter'
];
var_dump($a);
Oh yes I forgot to mention.哦,是的,我忘了提。 If pagecount is less than one I don't want to display the key 'pageCount'.如果 pagecount 小于 1,我不想显示键“pageCount”。 That's the crux of the problem here.这就是这里问题的症结所在。
I've found the solution.我找到了解决方案。 array_filter does the job if null:如果 null,array_filter 可以完成这项工作:
$content[] = arry_filter( array(
'id' => intval($child['_id']),
'type' => $child['_type'],
'name' => $child['_source']['node']['name'],
'filename' => $child['_source']['node']['basename'],
'extension' => $child['_source']['node']['extension'],
'date' => [
...
'version' => $child['_source']['node']['version'],
'pageCount' => (!empty($child['_source']['node']['pageCount']) && $child['_source']['node']['pageCount'] > 1) ? $child['_source']['node']['pageCount'] : null,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.