분할

2014-01-21 2 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를 사용할 수 있습니다.

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

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을 사용하는 것입니다 시도

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 
     ) 

)