2012-11-29 33 views
-2

我剛剛製作了一個刮腳本,它的工作原理。現在我得到這樣的輸出:php arrays multidimension

Array 
(
    [0] => Array 
     (
      [0] => example stuff 
      [1] => example stuff 
      [2] => example stuff 
      [3] => example stuff 
     ) 

[1] => Array 
    (
     [0] => /spellen/thumbs/a/aladdinwildride_6524.jpg 
     [1] => /spellen/thumbs/t/toystory3_3476.jpg 
     [2] => /spellen/thumbs/f/fred-flintstone-bobsled_2180.jpg 
     [3] => /spellen/thumbs/m/madagascar-2.jpg 
    ) 

[2] => Array 
    (
     [0] => spel Alladin Wide Rit 
     [1] => spel Toy Story 3 
     [2] => spel Bedrock Bobsledding blowout 
     [3] => spel Madagascar 2 Ontsnap naar Afrika 

    ) 

[3] => Array 
    (
     [0] => /spellen/cartoons/2096/alladin-wide-rit.html 
     [1] => /spellen/cartoons/1989/toy-story-3.html 
     [2] => /spellen/cartoons/1362/bedrock-bobsledding-blowout.html 
     [3] => /spellen/cartoons/237/madagascar-2-ontsnap-naar-afrika.html 

    ) 

[4] => Array 
    (
     [0] => l 
     [1] => l 
     [2] => l 
     [3] => l 

    ) 

[5] => Array 
    (
     [0] => Speel Alladin Wide Rit online 
     [1] => Speel Toy Story 3 online 
     [2] => Speel Bedrock Bobsledding blowout online 
     [3] => Speel Madagascar 2 Ontsnap naar Afrika online 

    ) 

[6] => Array 
    (
     [0] => e 
     [1] => e 
     [2] => e 
     [3] => e 

    ) 

[7] => Array 
    (
     [0] => /images/thumb_frame_top.gif 
     [1] => /images/thumb_frame_top.gif 
     [2] => /images/thumb_frame_top.gif 
     [3] => /images/thumb_frame_top.gif 

    ) 

[8] => Array 
    (
     [0] => f 
     [1] => f 
     [2] => f 
     [3] => f 

    ) 

現在我想一個簡單的foreach返回我的每$matches[1][0]$matches[2][0]$matches[3][0]

我一直試圖讓這個小時,但沒有運氣。有誰能夠幫助我?

+0

'的foreach($爲$陣列匹配){回聲$陣列[0] ; }'應該這樣做。很難確切地告訴你需要什麼。嘗試一下,看看我們是否接近。 – phpisuber01

+0

@Tornado請不要將讚美添加爲編輯。 –

+0

查看下面更新的答案。 – moonwave99

回答

0
$result = array(); 
foreach ($matches as $match) { 
    $result[] = array(
     $match[1], 
     $match[2], 
     $match[3], 
    ); 
    // or unset($match[0]); if you don't need the original array anymore 
    // or array_splice($match, 0, 1); if you also want to rearrange the keys 
} 
+0

嗨,以及我不需要$匹配[1] [0] $匹配[2] [0]和$匹配[3] [0]一個簡單的數組。剩下的我不需要 –

+0

@StefandeWal你想要這個嗎? – Carlos

+0

千斤頂,謝謝你的回答。那是我需要的答案。再次感謝:P –

0
foreach($matches as $match){ 

    // $match[0] is what you need 

} 

如果你想要的結果包裹在一個新的數組,可以減少原來的數組:

$newArray = array_reduce($matches, function(&$result, $match){ 

    $result[] = $match[0]; 

}); 
0

是這是你想要的嗎?

for ($i = 0; $i < count($matches); $i++) 
{ 
    print $matches[$i][0]; 
} 
+0

我會盡力解釋。 我想一個新的數組只中有: 匹配[1] [0] $比賽[2] [0] 和 $比賽[3] [0] –

+0

你總是隻需要這些3個變量?如果是的話,爲什麼不只是手動訪問它們並將它們推入數組? – Zim84

+0

這就是我想要的。但我不能得到它的PHP代碼吧:P –

0
foreach ($matches as $j => $match) { 
    foreach ($match as $i => $mi) { 
     if ($i != 0) unset($matches[$j][$i]); 
    } 
} 
0

如果只想前三:

$newArray = array(
    $matches[1][0], 
    $matches[2][0], 
    $matches[3][0] 
); 

結果:

Array 
    (
     [0] => /spellen/thumbs/a/aladdinwildride_6524.jpg 
     [1] => spel Alladin Wide Rit 
     [2] => /spellen/cartoons/2096/alladin-wide-rit.html 
    ) 
+0

這幾乎是正確的。這只是原始陣列中存在的42個之一。怎麼能得到其餘的? –

+0

你是什麼意思剩下的,你說你只想要第一個3?你想爲每個父元素使用'0'元素嗎? – MrCode

+0

這是我想在一個陣列: $匹配[1] [0] $匹配[2] [0] $匹配[3] [0] $匹配[1] [1] $比賽[2] [1] $匹配[3] [1] $匹配[1] [2] $匹配[2] [2] $匹配[3] [2] –