1

I'm quite new to PHP and I'm not really sure how to ask this question. My data is like this (displayed in JSON for easy viewing but i actually use array)

[{"_id":"1","title":"Month & Year","description":"To be used in Jurisprudence"},
 {"_id":"3","title":"Bible Version I","description":"Testament - Book - Chapter"}]

I know in PHP that i can access elements of associative array by using foreach as well as using the standard for loop.

e.g:

for($n=0;$n<sizeof($array_data);$n++){
     echo $array_data[$n]['title'];
}

Instead of using the index, what i want to do if possible is to access title using the _id. Something like, if have 3 as _id it should display the corresponding title in that array which is Bible Version I.

1
  • You have the tag associative-array, but your array is not associative. Commented Aug 5, 2017 at 18:44

2 Answers 2

2

Use an if statement.

foreach ($array_data as $item) {
    if ($item['_id'] == 3) {
        echo $item['title'];
        break;
    }
}

If you'll be doing this a lot, you should probably change your array to an associative array, so you can then just use $array_data[3]. E.g. it should be:

{
    "1": {"_id":"1","title":"Month & Year","description":"To be used in Jurisprudence"},
    "3": {"_id":"3","title":"Bible Version I","description":"Testament - Book - Chapter"}
}
Sign up to request clarification or add additional context in comments.

10 Comments

Hi, thank you for your answer. But can i do it without using a loop? The thing is, i'm saving the _id as value in an html element. Since i have the array already loaded in the browser i just want to call the particular title via the _id saved. I already used the loop for loading the data in the html elements thus i dont want to iterate again using another one.
No, you can't do it without a loop unless you change the array to an associative array first.
@ReubenJaMesAveñoGruta whatever you do, you need to loop. If you're searching an array, you have no choice
Looping every time will be expensive. That's why you should use the associative array, accessing these elements is very efficient. If you have that many items you should probably be using a database.
Or when you fetch the data fromthe database, you can do $array_data[$row['_id']] = $row; to create an associative array from the table.
|
1

A more efficient solution without a loop would be to use array_search with array_column :

$key = array_search("3", array_column($array_data, '_id'));
if ($myArray[$key]) {
  echo $myArray[$key]['_id'] . ' : ' . $myArray[$key]['title'];
}

Uses php 5.5.0 or above. Refer here

3 Comments

This is TWO loops, one loop to get all the column values, a second loop to search it for the ID you want.
The loops are hidden inside the array_column and array_search functions, but they're still there.
@Barmar Are you sure ? Can you point to any doc or ref if array_column or array_search are loops ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.