2017-01-26 36 views
0

我正在使用多維php數組來提供html代的數據,並且當我的兩個子數組(包含不同的鍵)包含相同的值時,我注意到了一些奇怪的行爲。例如,該陣列產生重複:PHP foreach複製具有相同內容的子陣列

$tableArray = Array(
    'rome' => Array(
     0 => Array(
      'home_prefix' => 'AWE', 
      'home_number' => '122', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to stuff' 
     ) 
    ), 
    'istanbul' => Array(
     0 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '103', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to Greek concepts of stretchiness' 
     ), 
     1 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '104', 
      'home_title' => 'Theory of Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to concepts of stretchiness' 
     ) 
    ), 
    'new york' => Array(
     0 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '103', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to Greek concepts of stretchiness' 
     ), 
     1 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '104', 
      'home_title' => 'Theory of Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to concepts of stretchiness' 
     ) 
    ) 
); 

foreach ($tableArray as $locationTab): 
       echo '<p>' . array_search($locationTab, $tableArray) . '</p>'; 
endforeach; 

輸出:

羅馬

伊斯坦布爾

伊斯坦布爾

但是,當我添加另一子陣列,以便最後兩個數組是沒有相同,沒有重複:

$tableArray = Array(
    'rome' => Array(
     0 => Array(
      'home_prefix' => 'AWE', 
      'home_number' => '122', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to stuff' 
     ) 
    ), 
    'istanbul' => Array(
     0 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '103', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to Greek concepts of stretchiness' 
     ), 
     1 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '104', 
      'home_title' => 'Theory of Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to concepts of stretchiness' 
     ) 
    ), 
    'new york' => Array(
     0 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '103', 
      'home_title' => 'Beginning Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to Greek concepts of stretchiness' 
     ), 
     1 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '104', 
      'home_title' => 'Theory of Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to concepts of stretchiness' 
     ), 
     2 => Array(
      'home_prefix' => 'RPED', 
      'home_number' => '104', 
      'home_title' => 'Theory of Stretching', 
      'abroad_prefix' => 'ARCH', 
      'abroad_number' => '111', 
      'abroad_title' => 'Intro to concepts of stretchiness' 
     ) 
    ) 
); 

輸出:

羅馬

伊斯坦布爾

紐約

我該如何解決這個問題,讓的foreach不重複子陣?雖然我的二級密鑰是獨一無二的,但可能會出現兩個或更多二級陣列中的值相同的情況。

+0

我已經編輯我的答案。我忘記了使用新創建的$ key變量 –

回答

0

函數array_search返回數組中與搜索模式匹配的第一個元素。這意味着,如果兩個元素具有相同的值,使用array_search第二個元素。如果要密鑰使用foreach這樣總是返回的第一個元素

的關鍵:

foreach ($tableArray as $key => $locationTab) { 
    echo '<p>' . $key . '</p>'; 
} 
0

從文檔:

array_search - 搜索給定值的數組,如果成功

什麼,你可以在這裏做的是後刪除該索引返回第一相應的密鑰輸出:

foreach ($tableArray as $locationTab) { 
    $loc = array_search($locationTab, $tableArray); 
    unset($tableArray[$loc]); 
    echo '<p>' . $loc . '</p>'; 
} 

我不明白你想用這段代碼到底想要什麼,bu這是一個解決方案。我想有一個更好的方法來做你想做的事。