Been working on this for 4 hours. My eyes are starting to hurt and my brain is weary from paradox.
So here is the setup. 3 arrays:
An array of posts that are advertisements
An array of normal posts
An array of taken positions which is pulled from array 1
The ad array has predefined spots.
I have merged the 2 arrays because the ads are basically integrated into the normal flow of posts. I have even been able to assign every post a spot based on the natural order of the posts, and the assigned spot for the ad. So if a post's natural spot has an ad in it, then that posts's position needs to bump forward one. This is easy if there is only 1 ad in the stream. But the thing is set up for multiple ads of arbitrary number.
So here is how I bump the position of each non-ad element in the array. I could run this again a few times I think and that should cover when there are additional ads but it seems wrong and unholy.
Wondering if anyone can solve this problem because I am sick of the taste of my own tears.
if(in_array($newarray[$key]['position'],$taken)){
$newkey = ($newarray[$key]['position'] + 1);
$newarray[$key]['position'] = $newkey;
$taken[] = $newkey;
In the end I need the merged array with all positions correctly assigned and unique.
You could write a recursive function to pull the next available position number, but if you're working with a small number of posts + ads, something like below would be quick and easy. Note that this assumes you need to preserve the original keys for the ads and posts arrays. If you don't need to do that then you can simplify it even further.
// Sample array of ads
$ads = array('ad1' => array('position' => 1),
'ad2' => array('position' => 4),
'ad3' => array('position' => 6),
'ad4' => array('position' => 10));
// Identify ad positions
foreach ( $ads as $k => $v ) {
$adPositions[$v['position']] = $v['position'];
}
// Identify available positions
$availablePositions = array_combine(range(1,100), range(1,100));
$availablePositions = array_diff_key($availablePositions, $adPositions);
// Sample posts
$posts = array('post1' => array('position' => false),
'post2' => array('position' => false),
'post3' => array('position' => false),
'post4' => array('position' => false),
'post5' => array('position' => false),
'post6' => array('position' => false));
// Use first (lowest number) available position as post position
foreach ( $posts as $k => $v ) {
$posts[$k]['position'] = array_shift($availablePositions);
}
// Merge arrays
$merged = array_merge($ads, $posts);
// Create list of positions for use with array_multisort
foreach ( $merged as $k => $v ) {
$position[$k] = $v['position'];
}
// Sort $merged by ['position']
array_multisort($position, SORT_ASC, $merged);
Gives you:
Array
(
[ad1] => Array
(
[position] => 1
)
[post1] => Array
(
[position] => 2
)
[post2] => Array
(
[position] => 3
)
[ad2] => Array
(
[position] => 4
)
[post3] => Array
(
[position] => 5
)
[ad3] => Array
(
[position] => 6
)
[post4] => Array
(
[position] => 7
)
[post5] => Array
(
[position] => 8
)
[post6] => Array
(
[position] => 9
)
[ad4] => Array
(
[position] => 10
)
)