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?