如何合并这两个PHP数组?

3

我有两个 PHP 数组,它们是图像管理系统的一部分。

weighted_images 是一个多维数组。每个子数组都是一个关联数组,包含 'weight'(用于排序)和 'id'(图像的 ID)两个键。

array(
    156 => array('weight'=>1, 'id'=>156),
    784 => array('weight'=>-2, 'id'=>784),
)

图片 这个数组是用户输入的。它包含了图片ID的数组。

array(784, 346, 748)

我希望将它们合并成一个按照图像权重排序的ID数组。如果图像没有权重,则将其附加到末尾。
这不是特别难的问题,但我的解决方案远非优雅,我认为肯定有更好的方法来解决。
$t_images = array();
foreach ($weighted_images as $wi) {
  if ( in_array($wi['id'], $images) ) {
    $t_images[$wi['weight']] = $wi['id'];
  }
}
foreach ($images as $image) {
  if ( !$weighted_images[$image] ) {
    $t_images[] = $image;
  }
}
$images = $t_images;

问题:有更好的方法吗?

(涉及IT技术)

请详细说明您的代码示例。当前$ai和$images未定义,而且不清楚$weighted_images和$images包含什么内容。 - Sjoerd
抱歉,那个是笔误。$ai 是我整理时打错的,应该是 $wi(现已更正)。$weighted_images 和 $images 在代码上面有定义。 - AnnanFay
5个回答

1

Schmalls 差不多说对了,只是漏掉了最后一步 -

如果一个图片没有权重值 就在末尾添加。

以下是完整的流程。

$array = array_intersect_key($weighted_images, array_fill_keys($images, null));

uasort($array, function($a, $b) {
    if($a['weight'] == $b['weight']) return 0;
    return ($a['weight'] > $b['weight']) ? 1 : -1;
});

$array += array_diff_key($images, $weighted_images);

0
<?php
$weights = array(
    156 => array('weight'=>1, 'id'=>156),
    784 => array('weight'=>-2, 'id'=>784),
);

$selected = array(784, 346, 748);

$selectedWeights = array();
foreach ($selected as $id)
{
    $weight = 0;
    if (isset($weights[$id]))
    {
        $weight = $weights[$id]['weight'];
    }
    $selectedWeights[$id] = $weight;
}
asort($selectedWeights);

print_r($selectedWeights);
?>

0
如果我理解得正确:
$data = array(
156 => array('weight'=>1, 'id'=>156),
784 => array('weight'=>-2, 'id'=>784),
);
$ids = array(156, 784, 431);


function compare_weight($item1, $item2) {
    return $item1['weight'] > $item2['weight'] ? 1 : -1;
}

uashort($data, 'compare_weight');

foreach($ids as $id)
    $data += array($id => array('weight'=>null, 'id'=>$id) );

最终数组需要仅包含images数组中的图像,但如果它们包含在weighted_images中,则还需包含这些图像的权重。 - AnnanFay

0

你可以很容易地获取数组的交集:

$selected_images = array_intersect_key($weighted_images, array_fill_keys($images, null))

array_fill_keys函数使用$images数组作为键,null作为每个键的值。因为我们使用键(array_intersect_key)来交集数组,所以除了第一个数组之外,任何数组的值都无关紧要。

然后,您可以使用回调函数按权重排序,就像Skirmantas建议的那样:

function cmp_weight($a, $b)
{
    if ($a['weight'] == $b['weight']) {
        return 0;
    }

    return (($a['weight'] > $b['weight']) ? 1 : -1;
}

$images = uasort($selected_images, 'cmp_weight');

如果您正在使用PHP 5.3,您可以使用匿名函数:

$images = uasort($selected_images, function($a, $b)
{
    if ($a['weight'] == $b['weight']) {
        return 0;
    }

    return (($a['weight'] > $b['weight']) ? 1 : -1;
})

0

我会开始重新思考$weighted_images数组。像这样的东西,其中键是ID,值是WEIGHT,就足够了:

$weighted_images = array(
  156 => 1,
  784 => -2,
);
$images = array(156, 784, 431);

然后只需进行一些排序,以及使用foreach确保您在数组中拥有所有图像。
// Images (with weight) ordered
asort($weighted_images);

// Check all images and add at the end the ones with no weight, with null value
foreach ($images as $id) {
  if (!array_key_exists($id, $weighted_images)) {
    $weighted_images[$id] = null;
  }
}

就是这样。


这意味着-2权重比1“更高”。如果不是,您应该使用arsort()而不是asort()。 - eillarra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接