如果statement为true,我想向数组中添加更多行: 我尝试使用下面的代码添加但没有任何成功。
这是数组:
$pages = array(
'visit' => array('url' => ''),
'dashboard' => array('url' => 'index.php'),
'posts' => array('url' => 'edit.php', 'permission' => 'edit_posts'),
'pages' => array('url' => 'edit.php?post_type=page', 'permission' => 'edit_pages'),
'comments' => array('url' => 'edit-comments.php', 'permission' => 'edit_posts'),
'users' => array('url' => 'users.php', 'permission' => 'list_users'),
);
// now here i want to add this row to array if somthing is true...
if ($page_gallery) {
$gallery_page_id = $page_gallery->ID;
$pages['gallery'] => array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
}
答案 0 :(得分:1)
尝试使用 = 代替 =>
$pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
答案 1 :(得分:0)
您正在使用=>
运营商:
$pages['gallery'] => array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
在这种情况下并不真正有用。您应该使用简单的赋值(=
)运算符:
$pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
在=>
上下文中的键和数组值之间使用array(...)
运算符,但是您指定的值已经使用$pages['gallery']
指定了键。
新分配的数组元素的顺序默认只是将它们放在数组的末尾。如果你需要将它们放在另一个顺序中,那么在将所有元素添加到数组之后,你将在遍历数组之前使用其中一个数组排序函数(sort,usort,uasort,uksort等)。
答案 2 :(得分:0)
由于您要添加新的数组索引,因此必须使用赋值语句
if ($page_gallery) {
$gallery_page_id = $page_gallery->ID;
$pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
}