0

I'm trying to create an array that looks like below.

$arr = array(
    'post_type'         => 'smart_contract',
    'post_status'       => 'publish',
    'author'            => $user_id,
    'meta_query'        => array(
            'relation' => 'OR',
                array(
                    'key'     => 'contract_type',
                    'value'   => 'erc721-nft',
                    'compare' => '=',
                ),
                array(
                    'key'     => 'contract_type',
                    'value'   => 'erc721-sbt',
                    'compare' => '=',
                ),
        ),
); 

But if you look at meta_query, I'm expecting more than two nested arrays (value => erc721-nft and value => erc721-sbt). So, I'm using a for-loop to add these in as below. I'm using array_merge but it overwrites the values instead of adding in more nested arrays.

foreach ( $contract_types as $contract_type ) {
    
    $or_array2 = array(
        'key'     => 'contract_type',
        'value'   => $contract_type,
        'compare' => '=',
    );
    
    $or_array = array_merge( $or_array, $or_array2 );

}

How can I append more nested arrays without any overwriting?

ratib90486
  • 89
  • 1
  • 7
  • Does this answer your question? [php - push array into array - key issue](https://stackoverflow.com/questions/9554799/php-push-array-into-array-key-issue) – Shahan M Jun 29 '23 at 16:40

2 Answers2

0

so you just want to add the contracts to the meta_query under relation => OR right ? You could do something like this:

$arr = array(
    'post_type'         => 'smart_contract',
    'post_status'       => 'publish',
    'author'            => '1234',
    'meta_query'        => array(
        'relation' => 'OR',
    ),
);

$contract_types = array(
    array(
        'key'     => 'contract_type',
        'value'   => 'erc721-nft',
        'compare' => '=',
    ),
    array(
        'key'     => 'contract_type',
        'value'   => 'erc721-sbt',
        'compare' => '=',
    )
);

foreach ($contract_types as $contract_type) {
    $arr['meta_query'][] = $contract_type; // add contract type to $arr array
}

^ above will result in:

Array
(
    [post_type] => smart_contract
    [post_status] => publish
    [author] => 1234
    [meta_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => contract_type
                    [value] => erc721-nft
                    [compare] => =
                )

            [1] => Array
                (
                    [key] => contract_type
                    [value] => erc721-sbt
                    [compare] => =
                )

        )

)
Xander-Nova
  • 129
  • 2
0

I'm not quite sure what you want to achieve, you should improve your question, meanwhile you could try with:

$or_array = array(); // Initialize the array

foreach ( $contract_types as $contract_type ) {
    
    $or_array2 = array(
        'key'     => 'contract_type',
        'value'   => $contract_type,
        'compare' => '=',
    );
    
    $or_array[] = $or_array2; // Add the array to the main array
}

// Add 'relation' key separately
$meta_query = array('relation' => 'OR') + $or_array;

// Now, use $meta_query for the main $arr
$arr = array(
    'post_type'   => 'smart_contract',
    'post_status' => 'publish',
    'author'      => $user_id,
    'meta_query'  => $meta_query,
);
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46