具有相同索引名称的PHP Change Array格式

时间:2016-02-23 10:44:18

标签: php arrays multidimensional-array

希望你们所有人都做得很好。

实际上我有一个如下所示的数组。

 try {
        URL url = new URL("http://myurl.com/wp-json/wp/v2/posts/219");
        String encoding = Base64.encodeBase64String((txtUserName.getText() + ":" + txtPassword.getText()).getBytes());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Authorization", "Basic " + encoding);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestMethod("POST");
        connection.connect();

        ObjectMapper post = new ObjectMapper();
        ObjectNode node = post.createObjectNode();
        node.put("title", "test1234");

        OutputStream os = connection.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os, "UTF-8");
        outputStreamWriter.write(post.toString());
        outputStreamWriter.flush();
        outputStreamWriter.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

上面的数组,我需要改变如下。

[and] 
    => 
        Array 
            (
                [0] => Array ( [Model.qualifications LIKE ] => %"1":"1"% ) 
                [3] => Array ( [Model.qualifications LIKE ] => %"4":"1"% ) 
                [6] => Array ( [Model.qualifications LIKE ] => %"7":"1"% ) 
            ) 

任何人都可以告诉我该怎么做。

任何帮助将不胜感激。

由于

3 个答案:

答案 0 :(得分:0)

您无法使用相同的键值映射数组。相反,如果您的密钥保持不变,您可以使用下面的内容:

[and] 
    => array(
             [Model.qualifications LIKE ] => array(
                                           [0] => %"1":"1"%
                                           [1] => %"4":"1"%
                                           [2] => %"7":"1"%
                                     )
            )

答案 1 :(得分:0)

如果数组中的所有值都不同,那么您可以将值作为键和键作为值,并且可以迭代为,

foreach($array as $key => $val){

}

然后根据需要使用键和值。

答案 2 :(得分:0)

原因:您的数组键是相同的。你可以这样做。

<?php 
$arr = array("and" => array (
            0 => array ("Model.qualifications LIKE" => '%"1":"1"%' ), 
            3 => array ("Model.qualifications LIKE" => '%"4":"1"%' ), 
            6 => array ("Model.qualifications LIKE" => '%"7":"1"%' ), 
            ));

$newarr = array();
foreach ($arr["and"] as $key1 => $value1) {
    $newarr[$value1[key($value1)]] = key($value1);
}
echo "Before : <pre>".print_r($arr,1)."</pre></br>";
echo "After : <pre>".print_r($newarr,1)."</pre>";
?>

输出:

之前:

Array
(
    [and] => Array
        (
            [0] => Array
                (
                    [Model.qualifications LIKE] => %"1":"1"%
                )

            [3] => Array
                (
                    [Model.qualifications LIKE] => %"4":"1"%
                )

            [6] => Array
                (
                    [Model.qualifications LIKE] => %"7":"1"%
                )

        )

)


After :

Array
(
    [%"1":"1"%] => Model.qualifications LIKE
    [%"4":"1"%] => Model.qualifications LIKE
    [%"7":"1"%] => Model.qualifications LIKE
)