Balan K
Balan K

Reputation: 47

I need to loop this value?

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                  
        )

)

This is print_r Is it possible to loop like below.

I need output for video detail loop this format please help

How to loop this value.

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

Upvotes: 1

Views: 59

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You have to format your array first through below code:-

<?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
?>

Output:- 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
        )

)

Now you can easily use foreach to print values. Thanks

Upvotes: 2

Related Questions