I’m working with some data which is being retrieved from my database. It’s placed into a 2-dimensional* array and manually sets the index based upon the ID of the data in the database.
* It’s more than two-dimensional, but I only need to work with 2 dimensions of it.
This is the Array in question:
Array
(
[error] => Boolean(false)
[results] => Array
(
[1] => Array
(
[0] => title1
[4] => title2
)
[3] => Array
(
[0] => title3
[1] => title4
[2] => title5
)
[4] => Array
(
[0] => title6
[1] => title7
[3] => title8
)
)
)
Within this Array, I only need to work with the “results” part of it, hence making the problem Array 2-dimensional.
Some voodoo-magic happens in my script and I’m told the index for the first array and the index for the second array, and then I pull the value from the second array.
Let’s say that I wanted to get the value “title5”. My script would tell me that I need to use index 3 and then index 2.
I’ve done all of that, but it’s this next part of the task that I’m stuck on, and have no-idea how to even attempt.
After retrieving “title5” I want to retrieve the indexes for the next and previous item, so in this case for both “title4” and “title6”. In the event of there not being a previous value (say we originally pulled “title1”) then I need some way of identifying this, like with a false boolean. And likewise for if there is no “next” value either.
By retrieve the indexes I mean, for example: title4 = 3 & 1 and title6 = 4 & 0
Does anybody have any ideas how I should go about doing this? I’m not looking for someone to do the work for me necessarily, but some pointers would be greatly appreciated. In the mean time I’ll keep experimenting. Thanks!
Note:
- Titles aren’t actually called
title1,title2, etc. So I can’t just decrement a number and do a search.
I edited @Jonathan Rich’s answer a little bit to obtain my actual solution. Where
$firstIndexand$secondIndexare the two index that I already know.