Hi there folks!
TL;DR for anyone that doesn't need my verbosity:
array_splice( $sim_array, 0, 0, $match_array);
seems to be ruining the multidimensional nature of the inserted element($match_array).
------------------Full meal deal follows---------------------
I'm trying to build a "similar content" feature on my site and the way I've decided to handle it is to store the top 10 id's and scores for a given row and when it's time to check for new and better matching content, it does the following:
1) Store the current top 10 results in an array
2) loop through new content, score it and then insert into the current array in it's proper location (if it beats the score of the current 3rd slot, place it in that location, shifting 4 - 10th down).
3) Trim the array back to 10 results so I can create an update clause.
Here's what my array looks like before I fiddle with it:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
... shortened
All the 0's are because my system isn't working yet.
Next, I create an array for the new content's ID and score for later check and insertion:
Array
(
[0] => 1
[1] => 9
)
I then have an elseif dance to see where it needs to place the new string into the array:
if($match_score > $sim1score){
array_splice( $sim_array, 0, 0, $match_array);
}elseif($match_score > $sim2score){
array_splice( $sim_array, 1, 0, $match_array);
}elseif($match_score > $sim3score){
array_splice( $sim_array, 2, 0, $match_array);
}elseif($match_score > $sim4score){
array_splice( $sim_array, 3, 0, $match_array);
}elseif($match_score > $sim5score){
array_splice( $sim_array, 4, 0, $match_array);
}elseif($match_score > $sim6score){
array_splice( $sim_array, 5, 0, $match_array);
}elseif($match_score > $sim7score){
array_splice( $sim_array, 6, 0, $match_array);
}elseif($match_score > $sim8score){
array_splice( $sim_array, 7, 0, $match_array);
}elseif($match_score > $sim9score){
array_splice( $sim_array, 8, 0, $match_array);
}elseif($match_score > $sim10score){
array_splice( $sim_array, 9, 0, $match_array);
}
Then trim it back to 10 results:
$sim_array = array_slice($sim_array, 0, 10);
So, my hope with array_splice in this example would have been to have it insert the multidimensional string in the first place and it did, kind of. The problem seems to be that it ruined it's multidimensional nature. Here's what my array looks like after my splice attempt:
Array
(
[0] => 1
[1] => 9
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
... shortened
Could someone help me figure out how to insert new elements into an array in the proper location without losing the proper multidimensional nature?
array_splice( $sim_array, 0, 0, $match_array);
seems to be ruining the inserted element's format.