2011-09-18 22 views
0

如何讓兩個數組相互對應?如何使兩個數組與php相互對應?

例如,這個第一月陣列我產生,

Array 
(
    [0] => Sep 
    [1] => Oct 
    [2] => Nov 
    [3] => Dec 
    [4] => Jan 
    [5] => Feb 
    [6] => Mar 
    [7] => Apr 
    [8] => May 
    [9] => Jun 
    [10] => Jul 
    [11] => Aug 
) 

這是2月陣列,

Array 
(
    [0] => Sep 
    [1] => May 
    [2] => Apr 
) 

但我想第二個月陣列跟隨與第1個月陣列相同的順序,將其作爲結果返回,

Array 
(
    [0] => Sep 
    [1] => Apr 
    [2] => May 
) 

是否有可能?

編輯:

我用來生成上述月陣列中的代碼,

# Set month array for the calendar and for the items. 
$months_calender = array(); 
$months_items = array(); 

# Set variable for requested month/year. 
$requested_year = set_variable($_REQUEST,'year'); 
$requested_month = set_variable($_REQUEST,'month'); 

# Set current month and curren year. 
$current_month = (int)date('m'); 
$current_year = (int)date('Y'); 

# Loop the 12 month and starts from the current month. 
for($x = $current_month; $x < $current_month+12; $x++) $months_calender[] = date('M', mktime(0, 0, 0, $x, 1)); 
print_r($months_calender); 
//echo $current_year; 

# Check if the requested by $_REQUEST does not exit then use the current month/year as the requested month/year. 
$requested_year = $requested_year? $requested_year : $current_year; 
$requested_month = $requested_month? $requested_month : date('m', mktime(0, 0, 0, $current_month, 1)); 
//var_dump($requested_year); 
//echo $requested_month; 

# Use the stored connection object from the class_page_controller.php, to process the query 
$items_monthly = $connection->fetch_all($sql_items_monthly,array($requested_year)); 
$total_items_monthly = $connection->num_rows($sql_items_monthly,array($requested_year)); 
//print_r($items_monthly); 

# Check if the total items is more than 0, then loop the result to make an 1-d array. 
if($total_items_monthly > 0) foreach($items_monthly as $item_monthly) $months_items[] = $item_monthly['archiveMonth']; 
print_r($months_items); 

的sql,

$sql_items_monthly = " 
    SELECT 
    DATE_FORMAT(p.pg_backdate, '%b') AS archiveMonth, 
    COUNT(p.pg_backdate) 

    FROM root_pages AS p 
    WHERE DATE_FORMAT(p.pg_backdate, '%Y') = ? 
    AND p.cat_id = '2' 
    AND p.parent_id = '6' 
    AND p.pg_hide != '1' 

    GROUP BY archiveMonth 
    ORDER BY p.pg_backdate DESC 
"; 

編輯:

它似乎我可以通過這個得到的結果,

$result = array_intersect($months_calender, $months_items); 
print_r($result); 

回報,

Array 
(
    [0] => Sep 
    [7] => Apr 
    [8] => May 
) 

即使keys不是從0開始2,但它並不重要。除非你有更好的解決方案。

+1

? –

+0

@Brian,請參閱我上面的修改。謝謝。 – laukok

回答

1

array_intersectksort一個簡單的組合將這樣的伎倆:

$array2 = array_intersect($array1, $array2); 
ksort($array2); 

See it in action

如果你想擁有這之後連續整數鍵某種原因,它也很容易重新索引結果數組:

$array2 = array_values($array2); 
+0

感謝Jon,我在我的編輯中發佈了相同的答案!大聲笑 – laukok

+0

@lauthiamkok:偉大的思想等;)無論如何,除了重寫創建數組的代碼(我沒有檢查過),我不認爲有更好的解決方案。 – Jon

+0

謝謝,喬恩! :-) – laukok

0

uksort似乎提供了一種可能性:http://php.net/manual/en/function.uksort.php

它允許您定義如何將陣列使用自己的排序功能進行排序。您可以使用自定義函數對陣列中的每個鍵執行搜索,然後按索引對它們進行排序。

0

使用array_intersect();

/** 
* @var array $all All months in the correct order 
* @var array $selected Selected months in any order 
*/ 
$result = array_intersect($all, $selected); 

編輯:我剛讀你的編輯,並注意到你有相同的答案。爲了解決該問題索引,只是包裝你在使用生成開始與陣列的結果array_values();

$result = array_values(array_intersect($all, $selected)); 
+0

謝謝廣告,現在得到答案! :-) – laukok

相關問題