php结果集到数组

时间:2016-04-01 09:26:22

标签: php mysql arrays resultset

我知道它早已被问过很多次但是相信我,我根据我的确切要求进行了研究,但却找不到正确的方法。

我需要来自mysql的php resultset的简单数组。

这是我的代码

[1] => Array
        (
            [country_code] => US
            [userscnt] => 727
        )

    [2] => Array
        (
            [country_code] => UY
            [userscnt] => 53
        )

    [3] => Array
        (
            [country_code] => VC
            [userscnt] => 4
        )

当我为$ countries []执行print_r时,我得到如下输出。

$countries = array(
            "US" => 727,
            "UY" => 53,
            "VC" => 4,
}

我需要的是像这样的数组

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone-no" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Video Canvas Test</title>
    <script>

    var video;
    var canvas;
    var ctx;
    var info;
    var pass = 0;

    window.onload = function()
    {
        video = document.createElement("video");
        video.oncanplay = function(){ initializeCanvas(); }
        video.onerror = function(e){ console.error("video problem"); }
        video.loop = true;
        video.controls = "true";
        video.src = "video/big-buck-bunny_trailer.webm";
        video.style.width = "400px";
        video.style.height = "300px";
        video.style.position = "absolute";
        video.style.left = "20px";
        video.style.top = "20px";
        video.style.backgroundColor = "#8080FF";

        canvas = document.createElement("canvas");
        canvas.style.backgroundColor = "#8080FF";
        canvas.style.width = "400px";
        canvas.style.height = "300px";
        canvas.style.position = "absolute";
        canvas.style.left = "420px";
        canvas.style.top = "20px";

        ctx = canvas.getContext("2d");

        info = document.createElement("p");
        info.innerHTML = window.navigator.userAgent;
        info.style.position = "absolute";
        info.style.width = "200px";
        info.style.height = "auto";
        info.style.position = "absolute";
        info.style.left = "20px";
        info.style.top = "320px";

        document.body.appendChild(video);
        document.body.appendChild(canvas);
        document.body.appendChild(info);
    }

    function initializeCanvas()
    {
        console.log("Video ready to play. Init canvas.");
        ctx.canvas.width = 640; // I am 100% sure this is correct video size.
        ctx.canvas.height = 360;
        console.log("Video size: " + video.videoWidth + "x" + video.videoHeight);
        ctx.font = "30px Arial";
        updateCanvas();
    }

    function updateCanvas()
    {
        pass ++;

        ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
        ctx.drawImage(video,0,0);
        ctx.fillText("Pass: " + pass,30,120);

        window.requestAnimationFrame( updateCanvas );
    }
    </script>
</head>
<body>
</body>
</html>

我该如何解决?

提前非常感谢你。

2 个答案:

答案 0 :(得分:0)

您可以修改您的while循环

$countries[$nodes_country_set['country_code']] = $nodes_country_set['userscnt'];

答案 1 :(得分:0)

您也可以像这样修改数组:

$result = [];    
    array_walk($data,function($v,$k)use (&$result){
        $result[$v['country_code']] =  $v['userscnt'];
    });    
print_r($result);

<强>输出:

Array
(
    [US] => 727
    [UY] => 53
    [VC] => 4
)