0

I have a situation where one array is needed to be (appended or pushed) into multiple arrays I am testing against. My project is WordPress based, but this is a general PHP question. If I have the following setup:


    $tq_exact_matches = array('relation' => 'AND');
    $tq_any_matches = array('relation' => 'OR');

    //I WANT MY ARRAY BELOW TO ALSO BE STORED IN $tq_any_matches[], NOT JUST $tq_exact_matches[]
    //HOW CAN I DO THAT WITHOUT WRITING IT TWICE?

        $tq_exact_matches[] = array(
            'taxonomy' => 'focus-area',
            'field'    => 'name',
            'terms'    => $pluck_term_names_fa,
        );

2
  • 1
    The same one or a copy of it? Assign it to a variable first which you can clone or push as is to both arrays. Commented Sep 8, 2022 at 20:58
  • Thank you so much! I can't believe its that easy. I should of thought of that! Commented Sep 8, 2022 at 20:59

2 Answers 2

2

Use variable.

$tq_exact_matches = array( 'relation' => 'AND' );
$tq_any_matches   = array( 'relation' => 'OR' );

// I WANT MY ARRAY BELOW TO ALSO BE STORED IN $tq_any_matches[], NOT JUST $tq_exact_matches[]
// HOW CAN I DO THAT WITHOUT WRITING IT TWICE?

$extra_tax_query = array(
    'taxonomy' => 'focus-area',
    'field'    => 'name',
    'terms'    => $pluck_term_names_fa,
);

$tq_exact_matches[] = $extra_tax_query;
$tq_any_matches[]   = $extra_tax_query;
Sign up to request clarification or add additional context in comments.

Comments

0

You may do it like this

    $tq_exact_matches = array('relation' => 'AND');
$tq_any_matches = array('relation' => 'OR');

//I WANT MY ARRAY BELOW TO ALSO BE STORED IN $tq_any_matches[], NOT JUST $tq_exact_matches[]
//HOW CAN I DO THAT WITHOUT WRITING IT TWICE?

    $m = array(
        'taxonomy' => 'focus-area',
        'field'    => 'name',
        'terms'    => 'we'
    );
    
    
    list($tq_exact_matches,$tq_any_matches) = [$tq_exact_matches + $m, $tq_any_matches + $m  ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.