How to combine these two PHP arrays? -


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

weighted_images A multi-dimensional array is an associative array with keys for each sub array 'weight' (for order) and 'id' (image id). The array ('weight' = & gt; array ('weight' = & gt; 'id' => 156), 784 => array ('weight' = & Gt; -2, 'id' => 784),)

Image This array is user input. This is an array of image IDs.

  array (784, 346, 748)  

I want to sort them with the same array weight of the image if any image ends up to a weight Does not attach.

This is not a particularly difficult problem, though my solution is beautiful and can not help thinking that is a better way of doing it.

  $ t_images = array (); Foreign currency ($ weighted_images $ wi) {if (in_are ($ y ['id'], $ images)) {$ t_images [$ wi ['weight']] = $ wi ['id']; }} Foreach ($ Pictures as $ images) {if ($ $ weighted_images [$ image]) {$ t_images [] = $ image; }} $ Images = $ t_images;  

Question: Is there a better way to do this?

Squals are almost perfect, just have not missed the last step -

If an image is not a weight attachment until the end.

Here is the complete process.

  $ array = array_intersect_key ($ weighted_images, array_fill_keys ($ Picture, empty)); ($ A ['weight'] == $ b ['weight']) Return 0; Return ($ a ['weight']> gtc [$ b] 'weight'])? 1: -1;}); $ Array + = array_diff_key ($ Picture, $ weighted_images);  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -