0

我想在另一个数组中打印一个关联数组。

这是代码:

“$posted_fields”,是一个由多个数组组成的关联数组,每个数组包含一个发布字段的数据。语法如下:my posted field(checkboxes) = array('key'=>'THE NAME OF THE FIELD', 'value'=>'array(FIELD VALUES)', ...)。

循环是这样的:

$posted_fields = array();
foreach($post_values as $field_name => $field_value){


           $posted_fields[] = array('key'   => $field_name,
             'value' => $field_value,
             'compare' => 'IN',
            );



}

我得到以下结果:

$posted_fields = array(
                    array
                    (
                        "key" => "property_country",
                        "value" => array
                            (
                                "es",
                                "fr",
                                "ca",
                            ),

                        "compare" => "IN"
                    ),array
                    (
                        "key" => "property_city",
                        "value" => array
                            (
                                "montreal"
                            ),

                        "compare" => "IN"
                    )

            );

这是我想做的:

“$args”是我想打印“$posted_field”结果的关联数组。

$args = array(
            'posts_per_page ' => -1,
            'post_type' => 'property',
            'post_status' => 'publish',             
            'meta_query' => array(
                'relationship' => 'AND',

                $posted_fields  

            ),
            'fields' => 'ids',
        );

而不是拥有这个

$args = array(
            'posts_per_page ' => -1,
            'post_type' => 'property',
            'post_status' => 'publish',             
            'meta_query' => array(
                'relationship' => 'AND',
                array(
                    array
                    (
                        "key" => "property_country",
                        "value" => array
                            (
                                "es",
                                "fr",
                                "ca",
                            ),

                        "compare" => "IN"
                    ),array
                    (
                        "key" => "property_city",
                        "value" => array
                            (
                                "montreal"
                            ),

                        "compare" => "IN"
                    )

                )
            ),
            'fields' => 'ids',
        );

我想得到这样的结果:

$args = array(
            'posts_per_page ' => -1,
            'post_type' => 'property',
            'post_status' => 'publish',             
            'meta_query' => array(
                'relationship' => 'AND',

                    array
                    (
                        "key" => "property_country",
                        "value" => array
                            (
                                "es",
                                "fr",
                                "ca",
                            ),

                        "compare" => "IN"
                    ),array
                    (
                        "key" => "property_city",
                        "value" => array
                            (
                                "montreal"
                            ),

                        "compare" => "IN"
                    )

                ),

            'fields' => 'ids',
        );

这里的区别在 ['relationship' => 'AND',] 之后。我想要 `['relationship' => 'AND', array(), array(), ...] 而不是 ['relationship' => 'AND', array(array(), array(), 。 ..]

我希望信息是明确的

谢谢

4

1 回答 1

0

这可能会帮助您更接近解决方案。我之前使用下面的函数来扩展多维数组:

$input_size = (count($multidim_array));
for($row = 0; $row < $input_size; $row++ )
    {
    echo "the row number $row<br>";

        foreach($multidim_array[$row] as $key => $value)
        {
            echo "key $key, value $value<br>";
        }
    }
于 2013-03-06T12:38:06.923 回答