How to combine these two PHP arrays?

I have two arrays in php that are part of an image management system.

weighted_images A multidimensional array. Each auxiliary array is an associative array with the keys "weight" (for ordering) and "id" (image identifier).

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

image This array is entered by the user. This is an array of image identifiers.

array(784, 346, 748)

I want to combine them into one array of identifiers, ordered by image weight. If the image has no weight added to the end.

This is not a particularly difficult problem, but my solution is far from elegant and cannot help but think that there should be a better way to do this.

$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;

Question: Is there a better way to do this?

+3
5

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);
+1
<?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) );
0

:

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

array_fill_keys $images null . (array_intersect_key), , , .

, :

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

Then just do some sorting and foreach to make sure you have all the images in the array.

// 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;
  }
}

What is it.

0
source

Source: https://habr.com/ru/post/1749938/


All Articles