【问题标题】:How to make two arrays correspond with each other with php?php如何使两个数组相互对应?
【发布时间】:2011-09-18 22:46:18
【问题描述】:

如何让两个数组相互对应?

例如,这是我生成的 1st-months 数组,

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
)

但我希望 2nd-months 数组遵循与 1st-months 数组相同的顺序,将其作为结果返回,

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但没关系。除非你有更好的解决方案。

【问题讨论】:

  • 你是用什么来生成数组的?
  • @Brian,请看我上面的编辑。谢谢。

标签: php arrays multidimensional-array monthcalendar


【解决方案1】:

array_intersectksort 的简单组合就可以解决问题:

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

See it in action.

如果出于某种原因您希望在此之后有连续的整数键,那么重新索引结果数组也很容易:

$array2 = array_values($array2);

【讨论】:

  • @lauthiamkok: 伟大的思想等等 ;) 无论如何,除了重写创建数组的代码(我没有检查过)之外,我认为没有更好的解决方案。
【解决方案2】:

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

它允许您定义如何使用您自己的排序函数对数组进行排序。您可以使用自定义函数对数组中的每个键执行搜索,然后按索引对其进行排序。

【讨论】:

    【解决方案3】:

    使用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));
    

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多