0

2つのアレイを相互に対応させるにはどうすればよいですか?

たとえば、これは私が生成した1か月目の配列です。

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
)

しかし、2か月目の配列を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まで開始されていませんが、問題ではありません。より良い解決策がない限り。

4

3 に答える 3

1

との単純な組み合わせでうまくarray_intersectいきksortます。

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

実際の動作をご覧ください

何らかの理由でこの後に連続した整数キーが必要な場合は、結果の配列のインデックスを簡単に再作成することもできます。

$array2 = array_values($array2);
于 2011-09-18T23:03:20.797 に答える
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));
于 2011-09-18T23:07:56.060 に答える
0

uksortは可能性を提供しているようです:http://php.net/manual/en/function.uksort.php

独自の並べ替え関数を使用して配列を並べ替える方法を定義できます。カスタム関数を使用して、各キーの配列を検索し、インデックス順に並べ替えることができます。

于 2011-09-18T22:52:18.343 に答える