Is there a way to access a multidimensional array from any position? example:
$arr = array (
1 => array ( 1, 'some-url-one', 'Some Title One' ),
2 => array ( 2, 'some-url-two', 'Some Title Two' ),
3 => array ( 2, 'some-url-three', 'Some Title Three' ),
);
Then, if we have the id of an item, for example 2 and want to get the title of that item we simply use:
$title = $arr[2][2]; // which outputs Some Title Two
And if we want to get the url of that same item we simply use:
$title = $arr[2][1]; // which outputs some-url-two
BUT what if we have the URL (some-url-two) and want to get the id or title of that item?
There are 2 obligations:
1- loop through the array and check if that URL exists
2- get the id and the title
How to do that the best way? I already have 3 arrays:
1- id to url
2- url to id
3- id to title
$id2url = array (
1 => 'some-url-one',
2 => 'some-url-two',
3 => 'some-url-three'
);
And the two others are the same procedure. So I wanted to optimize my code by combining the 3 arrays. so how to access the id and the title with a given url?
Thanks.
You might look for
array_flip()PHP Manual, it will make your lookup arrays work with their values as keys: