Accessing two-dimentional array by element

Hi Experts,

Is it possible to access a element within a 2-D array by its content, without using a loop/if?

For example, can I specify date 2014-07-06, run A, and get capacity 56 vehicle 37? But if I specify 2014-07-06 run C, then I would get capacity of null?  My array is
Array
(
    [0] => Array
        (
            [id] => 6955
            [date] => 2014-07-06
            [run] => A
            [capacity] => 56
            [vehicle_id] => 37
        )
    [1] => Array
        (
            [id] => 6956
            [date] => 2014-07-06
            [run] => B
            [capacity] => 60
            [vehicle_id] => 370
        )
    [2] => Array
        (
            [id] => 6960
            [date] => 2014-07-07
            [run] => A
            [capacity] => 65
            [vehicle_id] => 37
        )
)

Open in new window


I know that I can use foreach and a nested if, but I want to prevent looping through the array over and over again because I may have 1 year range, and some days can have 1 run some up to 6, so I thought if I can access that array elements directly, it would be more efficient.
APD TorontoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
How (and where) was the array created?  If you have control over the creation of the array, you can create another array at the same time for the specific purpose:
Array
(
    [2014-07-06] => Array
        (
            [A] => Array
                (
                    [capacity] => 56
                    [vehicle_id] => 37
                )
            [B] => Array
                (
                    [capacity] => 60
                    [vehicle_id] => 370
                )
        )
    [2014-07-07] => Array
        (
            [A] => Array
                (
                    [capacity] => 65
                    [vehicle_id] => 37
                )
        )
)

Open in new window

And then you can:
- check existence for "C" by using isset($anotherArray["2014-07-06"]["C"])
- access capacity for "A" by $anotherArray["2014-07-06"]["A"]["capacity"]
- access vehicle_id for "A" by $anotherArray["2014-07-06"]["A"]["vehicle_id"]

All without using a loop.
0
Julian HansenCommented:
The answer to your question is no - you cannot access array elements by their value.

If I read your question correctly - you are potentially wanting to do different retrievals based on values in the array.

In this case you should consider storing your data in a database and then using database queries to return the results you want - that would be the correct way to solve the problem.

What is the source of your array data currently?
0
APD TorontoAuthor Commented:
To answer both, this is coming from a database, but I dont want to query the database every time, because it will be slower than looping through the array.
0
Julian HansenCommented:
Well those are your only two options.

1. Query the database for the data you want
2. Build global array and loop through checking values.

Your original question: Can an array be accessed by value - answer is no - only by keys.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.