Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Adding to a multidimensional array

    I've tried searching for this but I just can't seem to get my head around it, hopefully someone can help.

    This is the code I have:

    PHP Code:
    $args = array(
        
    'cat' => 5,
        
    'meta_query' => array(
            array(
                
    'key' => '_mf_write_panel_id',
                
    'compare' => '=',
                
    'value' => 1
            
    )
        ),
    'meta_key' => 'number_of_bedrooms',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    ); 
    and I want to turn the array into this:-

    PHP Code:
    $args = array(
        
    'cat' => 5,
        
    'meta_query' => array(
            array(
                
    'key' => '_mf_write_panel_id',
                
    'compare' => '=',
                
    'value' => 1
            
    ),
            array(
                
    'key' => 'swimming_pool',            
                
    'compare' => '=',
                
    'value' => 'yes');
        ),
    'meta_key' => 'number_of_bedrooms',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    ); 
    But... I need to add it in after the initial array has been created.

    I've tried all sorts of variations of this:-

    PHP Code:
    $args['meta-query'] = array('key' => 'swimming_pool','compare' => '=','value' => 'yes'); 
    and array_push and anything else I can think of but it doesn't seem to be working.

    It's to build a Wordpress query, so I need to use conditionals to decide whether to add the extra arrays into the query based on a search form on the preceding page.

    Many thanks for any help offered.

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    PHP Code:
    <?php

    $args 
    = array(
        
    'cat' => 5,
        
    'meta_query' => array(
            array(
                
    'key' => '_mf_write_panel_id',
                
    'compare' => '=',
                
    'value' => 1
            
    )
        ),
    'meta_key' => 'number_of_bedrooms',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    );  


    array_push($args['meta_query'], array(
                
    'key' => 'swimming_pool',            
                
    'compare' => '=',
                
    'value' => 'yes'));
                
        
    print_r($args);
    ?>

  • Users who have thanked Adee for this post:

    yokelrobin (11-21-2011)

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Perfect, thanks - could have sworn I tried something very similar to that but obviously not.

    Thanks again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •