Change Index Key to Name

I have built a multidimensional array for usage in a custom WordPress query. Although it is WordPress I feel that this is more a basic PHP question.

The array that I have now outputs everything properly, accept for the index value. I need it to be a string instead of an integer.

Read More

Here is what I need my ouput to be

Array (
    [post_type] => property
    [posts_per_page] => -1
    [tax_query] => Array (
        [0] => Array (
            [taxonomy] => state
            [field] => slug
            [terms] => illinois
        )
        [1] => Array (
            [taxonomy] => illinois_county
            [field] => slug
            [terms] => fulton
        )
    )
)

And here is what is actually being output

Array (
    [post_type] => property
    [posts_per_page] => -1
    [0] => Array (
        [0] => Array (
            [taxonomy] => state
            [field] => slug
            [terms] => illinois
        )
        [1] => Array (
            [taxonomy] => illinois_county
            [field] => slug
            [terms] => fulton
        )
    )
)

The only difference being the key of the second array which is 0 in mine and needs to be tax_query.

To get this Array I am declaring $tax_query_array = array(); and then dropping my child arrays as needed depending on what variables are present in the url, such as $tax_query_array[] = $state_array; and $tax_query_array[] = $county_array;. Then finally calling $tax_query_array where i need the final multidimensional array output.

Only thing stopping me is the initial [0] which needs to instead be [tax_query].

Here is the full code:

$tax_query_array = array();

$tax_query_array['tax_query'][] = $state_array;
$tax_query_array['tax_query'][] = $county_array;
$tax_query_array['tax_query'][] = $price_range_array;

$taxonomy_args = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    $tax_query_array
}

Output from changing $tax_query_array[] = $county_array; to $tax_query_array['tax_query'][] = $county_array; via MikeBrant:

Array (
    [post_type] => property
    [posts_per_page] => -1
    [0] => Array (
        [tax_query] => Array (
            [0] => Array (
                [taxonomy] => state
                [field] => slug
                [terms] => illinois
            )
            [1] => Array (
                [taxonomy] => illinois_county
                [field] => slug
                [terms] => fulton
            )
        )
    )
)

Related posts

Leave a Reply

1 comment

  1. Try this code:

    $taxonomy_args = array(
        'post_type' => 'property',
        'posts_per_page' => -1,
        'tax_query'=> array($state_array, $county_array, $price_range_array)
    }