PHP:将关联数组打印到另一个数组中

时间:2013-03-06 12:10:16

标签: php arrays

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

以下是代码:

“$ posted_fields”,是由多个数组组成的assosiative数组,每个数组包含一个已发布字段的数据。语法如下:我的发布字段(复选框)=数组('key'=>'字段的名称','值'=>'数组(字段值)',...)。

The Loop就像这样:

$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”结果的assosiative数组。

$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',array(),array(),...]而不是['relationship'=> 'AND',array(array(),array(),...]

我希望信息清晰

谢谢

1 个答案:

答案 0 :(得分: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>";
        }
    }