Php从json数组中获取随机jsonobjects

时间:2016-06-10 13:29:19

标签: php json

我想随机化我的json对象的顺序。这是输出:

output

到目前为止,这是我的代码:

// check for empty result
    if (mysql_num_rows($result1) > 0) {
        // looping through all results
        // products node

        while ($row = mysql_fetch_array($result1)) {
            // temp user array
            $feedMain["user_id"]         = $row["user_id"];

            // push single product into final response array
            array_push($response["feedMain"], $feedMain);
        }
        // success
        $response["success"] = 1;

    } 


    // check for empty result
    if (mysql_num_rows($result2) > 0) {
        // looping through all results
        // products node

        while ($row = mysql_fetch_array($result2)) {
            // temp user array
            $feedMain["user_id"]         = $row["user_id"];

            // push single product into final response array
            array_push($response["feedMain"], $feedMain);
        }
        // success
        $response["success"] = 1;

    } 

    // check for empty result
    if (mysql_num_rows($result3) > 0) {
        // looping through all results
        // products node

        while ($row = mysql_fetch_array($result3)) {
            // temp user array
            $feedMain["user_id"]         = $row["user_id"];
            // push single product into final response array
            array_push($response["feedMain"], $feedMain);
        }
        // success
        $response["success"] = 1;

        // echoing JSON response        
        echo json_encode($response, JSON_UNESCAPED_UNICODE);
    }

我尝试过类似的事情:

echo json_encode(shuffle($response), JSON_UNESCAPED_UNICODE);

和其他代码片段,但没有什么不会对我有用.. 我只是想随机化json对象的顺序。感谢。

result1,2和3是mysql语句:)

1 个答案:

答案 0 :(得分:1)

$response["feedMain"]不会深入探索阵列。你应该洗牌的是shuffle($response["feedMain"]); echo json_encode($response, JSON_UNESCAPED_UNICODE);

getPersistentUser