Search code examples
phparraysmultidimensional-arrayconditional-statementsdynamic-arrays

Insert array inside array that it's inside array


I have an array that contains other arrays.

$args = array (
    'post_type' => 'clientes',
    'posts_per_page'    => -1,
    'orderby' => 'post_title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key'       => 'fecha_alta',
            'compare'   => '!=',
            'value'     => NULL,
            ),
        array(
            'relation' => 'OR',
            array(
                'key'       => 'fecha_baja',
                'compare'   => '>',
                'value'     => $trimestre['end'],
                ),
            array(
                'key'       => 'fecha_baja',
                'compare'   => '=',
                'value'     => NULL,
                ),
            ),
        ),
    );

I want to insert an array inside meta_query array based on a condition

if ($_GET['responsable']=='Pep' 

insert this array

array(
            'key'       => 'responsable',
            'compare'   => '=',
            'value'     => $responsable,
            ),

What is the best method to do this?

Thank you so much


Solution

  • just make like this:

    $arr = [];
    
    if(true){
    $arr = [array data with true condition];
    }else{
    $arr = [array data with false condition];
    }
    ....
    $args = array (
        'post_type' => 'clientes',
        'posts_per_page'    => -1,
        'orderby' => 'post_title',
        'order' => 'ASC',
        'meta_query' => $arr[]
    ) 
    

    or if you want to push data into meta array then you can use that according to your need