Looping through array and output as pairs (divider for each second element)

后端 未结 9 539
旧时难觅i
旧时难觅i 2021-01-18 03:41

I have an array with anonymous elements. Elements are added to the array via php, like so:

$playlist = array();

while (databaseloop) {
  $playlist[] = $a_ti         


        
9条回答
  •  感情败类
    2021-01-18 04:16

    Well, maybe this is the most basic solution:

    for (var i = 0; i < arr.length; i += 2) {
        var title = arr[i];
        var len = arr[i+1];
    }
    

    However, I would recommend you to arrange $playlist as follows:

    while (databaseloop) {
        $playlist[] = array(
            "title" => $a_title,
            "length" => $a_length
        );
    }
    

    Then it will be easy to iterate the elements simply with:

    for (var i = 0; i < arr.length; i++) {
        var title = arr[i]['title'];
        var len = arr[i]['length'];
    }
    

提交回复
热议问题