I'm building an image gallery and want to throw some promo banners in at random points to promote certain offers to users. Given the following two arrays have been filtered from a database query:
Media images array:
Array
(
[0] => Array
(
[insertDate] => 2014-11-10 11:22:58
[keyword] => standard
[mediaClass] => image
[mediaURL] => http://image1.jpg
[promoURL] =>
)
[2] => Array
(
[insertDate] => 2014-11-10 11:23:18
[keyword] => standard
[mediaClass] => image
[mediaURL] => http://image3.jpg
[promoURL] =>
)
[3] => Array
(
[insertDate] => 2014-11-10 11:23:28
[keyword] => standard
[mediaClass] => image
[mediaURL] => http://image4.jpg
[promoURL] =>
)
[5] => Array
(
[insertDate] => 2014-11-10 11:23:48
[keyword] => standard
[mediaClass] => image
[mediaURL] => http://image6.jpg
[promoURL] =>
)
)
Promo images array:
Array
(
[1] => Array
(
[insertDate] => 2014-11-10 11:23:08
[keyword] => promo
[mediaClass] => image
[mediaURL] => http://image2.jpg
[promoURL] => http://www.google.com
)
[4] => Array
(
[insertDate] => 2014-11-10 11:23:38
[keyword] => promo
[mediaClass] => image
[mediaURL] => http://image5.jpg
[promoURL] => http://www.google.com
)
)
How can I insert the promo images into the media images array at random indexes while maintaining the sort order by insertDate of the media images?
i.e Adding promo banners into a timeline of images.
This is what I would do:
Only thing to note is that both my media and promo arrays are numeric index based starting at zero and contiguous which is default for an array and not like the arrays you posted where they
1
and4
keys are removed from the media array.If you have no tons of images (say, max. 100), feel free to create arrays "by hand". First, you should create an array, which contains the positions of the promo items. You know the number of items in the content array, the number of the items in the promo array; the sum of them will be the final array's size. Figure out some smart math, which calculates the positions of promo items (e.g. don't put promo in the first and last place, don't put two promo items in neighbour positions, maybe you have to
unset()
some promo items, if there're more promo items than content items).If you have the promo positions, you may walk trhough on the content items, copying them into the final array. Before content copy, you should check whether the current position is reserved for a promo item, if yes, promo item should copied first. Voila, you have a final item list, promo items interleaved.
PHP is a good choice for these kind of tasks, thanks to autovivification (you don't have to create parent nodes for a deep-placed element). The programming, especially web pages are:
Similar tasks appears in job interview questions (scan through an array, and count/sum/merge something).