2014-01-21 139 views
0

我怎麼會去分裂這個陣列通過每個這些視頻訪問和環...拆分此多維數組

array(1) { 
[0]=> array(2) 
{ 

    [0]=> array(3) 
    { 
     ["title"]=> string(27) "A test title for this video" 
     ["video_item"]=> string(70) "http://dev.test/wp-content/uploads/2014/01/1.Introduction3.mp4" 
     ["video_image"]=> string(78) "http://dev.test/wp-content/uploads/2014/01/1.Introduction3_thumb23.jpg" 
    } 

    [1]=> array(3) 
    { 
     ["title"]=> string(13) "asdf fads fad" 
     ["video_item"]=> string(67) "http://dev.test/wp-content/uploads/2014/01/Spring-Mower.mp4" 
     ["video_image"]=> string(75) "http://dev.test/wp-content/uploads/2014/01/Spring-Mower1_thumb6.jpg" 
    } 

} 
} 

這是我使用的,但顯然該代碼不工作

的一部分
// this gets the array 
$videos = get_post_meta(get_the_ID(), 'video_items', false); 

$vid = array(); 
$img = array(); 
foreach($videos as $video) { 
    $vid[] = $video['video_item']; 
    $img[] = $video['video_image']; 

} 
+0

你想分割數組的方式是?輸出的例子會很好。 – wallyk

+3

你可以使用'foreach($ videos [0] as $ video)' –

+0

看看我的答案,你在數組中有一個數組,所以你需要訪問原始數組的第一個元素,它是數組您在開始迭代每個數組之前感興趣 – Pavan

回答

2

您有一個數組中的數組,所以你需要訪問的第一個元素,你開始通過內部的

每個數組迭代你得到後陣所以才加入這行之前$videos = fullArray[0];

// this gets the array as you did in your original code block 
$fullArray = get_post_meta(get_the_ID(), 'video_items', false); 

//But then you actually needed to add the below line. This gets the first 
//element of the array which happens to be an array and actually contains the array you 
//originally wanted to iterate through 
$videos = fullArray[0]; 

$vid = array(); 
$img = array(); 

foreach($videos as $video) { 
    $vid[] = $video['video_item']; 
    $img[] = $video['video_image']; 
} 

echo "video urls " . $vid . "\n"; 
echo "image urls " . $img; 
0

也許你可以使用array_chunk來創建它。

http://us2.php.net/manual/en/function.array-chunk.php

,如文檔中說:

<?php 
$input_array = array('a', 'b', 'c', 'd', 'e'); 
print_r(array_chunk($input_array, 2)); 
print_r(array_chunk($input_array, 2, true)); 
?> 

將打印

Array 
(
    [0] => Array 
     (
      [0] => a 
      [1] => b 
     ) 

    [1] => Array 
     (
      [0] => c 
      [1] => d 
     ) 

    [2] => Array 
     (
      [0] => e 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [0] => a 
      [1] => b 
     ) 

    [1] => Array 
     (
      [2] => c 
      [3] => d 
     ) 

    [2] => Array 
     (
      [4] => e 
     ) 

) 
0

試試這個

foreach($array as $key => value) 
{ 
    if(is_array($value)) 
    { 
      foreach($value as $k => $v)ev 
      { 
        foreach($v as $k1 => $v1) 
        { 
          echo $k1 .'=>'.$v1.PHP_EOL; 
        } 
      } 
    } 
} 

更好的將使用RecursiveIte rator here