-3

i want to add more rows to array if statement is true: i tried to add with the code below but without any success.

  1. how to add this row in the right way?
  2. how can i set the order for all the rows that i will add by code?

this is the array:

    $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');
    }
user2413244
  • 221
  • 2
  • 14

3 Answers3

1

try this use = instead of =>

 $pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
Osama Jetawe
  • 2,693
  • 6
  • 21
  • 39
  • Thanks! feel so stupid, because i have tried also only with = but maybe i miss something! and how can i reorder the array? after i add "gallery" to array i dont want that "gallery" will be the last in the array.so i want to change the positions for "gallery". – user2413244 Apr 20 '15 at 07:15
  • can you help me with the second question? – user2413244 Apr 20 '15 at 07:22
  • where do you have to insert gallery? – Osama Jetawe Apr 20 '15 at 07:24
  • this code: $pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit'); will insert the gallery key as the last in the array. so i want to change this position. – user2413244 Apr 20 '15 at 07:26
  • check this post there is an example http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php – Osama Jetawe Apr 20 '15 at 07:28
0

You are using a => operator:

$pages['gallery'] => array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');

which is not really useful in this context. You should be using a simple assignment (=) operator:

$pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');

The => operator is used between the key and the value of an array within the array(...) context, but you are assigning a value, having already specified the key with your $pages['gallery'].

The order of newly assigned array elements defaults to simply putting them at the end of the array. If you need to put them in another order then after you have added all elements to the array then you would use one of the array sorting functions (sort, usort, uasort, uksort, etc.) before you traversed the array.

Peter Bowers
  • 3,009
  • 1
  • 8
  • 18
0

Since you are adding a new array index, you will have to use the assignment statement

if ($page_gallery) {
    $gallery_page_id = $page_gallery->ID;
    $pages['gallery'] = array ('url' => 'post.php?post='. $gallery_page_id .'&action=edit');
}