我需要循环这个值吗?

时间:2016-06-23 05:15:04

标签: php arrays

Array
(
    [video_url] => Array
        (
            [0] => http://localhost/wbg/wp-content/uploads/2016/05/small.mp4
            [1] => http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4
        )

    [video_id] => Array
        (
            [0] => 128
            [1] => 125
        )

    [video_title] => Array
        (
            [0] => video title 1
            [1] => video title 2
        )

    [video_desc] => Array
        (
            [0] => video Description 1                                                                                              
            [1] => video Description 2                  
        )

)

这是print_r是否可以循环如下。

我需要输出视频详细信息循环此格式请帮助

如何循环此值。

video_title = video title 1
video_url   = http://localhost/wbg/wp-content/uploads/2016/05/small.mp4
video_desc  = video Description  1
video_id    = 128

1 个答案:

答案 0 :(得分:2)

您必须先通过以下代码格式化数组: -

<?php 
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1); // display those errors
$original_array = Array
(
    'video_url' => Array
        (
            '0' => 'http://localhost/wbg/wp-content/uploads/2016/05/small.mp4',
            '1' => 'http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4'
        ),

    'video_id' => Array
        (
            '0' => 128,
            '1' => 125
        ),

    'video_title' => Array
        (
            '0' => 'video title 1',
            '1' => 'video title 2'
        ),

    'video_desc' => Array
        (
            '0' => 'video Description 1',                                                                                             
            '1' => 'video Description 2'                 
        )

); // your original array

$final_array = array(); // an empty array

foreach ($original_array as $key=> $value){ // loop through original array

      foreach ($value as $key1=> $val){ // loop through sub-array

               $final_array[$key1][$key] = $val; // assign values to newly created array  here $key1 = 0,1  And $key= video_url,video_id,video_title,video_desc
      }

}

echo "<pre/>";print_r($final_array); // print final array
?>

输出: - https://eval.in/593947

Array
(
    [0] => Array
        (
            [video_url] => http://localhost/wbg/wp-content/uploads/2016/05/small.mp4
            [video_id] => 128
            [video_title] => video title 1
            [video_desc] => video Description 1
        )

    [1] => Array
        (
            [video_url] => http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4
            [video_id] => 125
            [video_title] => video title 2
            [video_desc] => video Description 2
        )

)

现在,您可以轻松使用foreach来打印值。感谢