-11

I have an array,

array
(
    [0] => array
    (
        [id]       => 2
        [title]    => Test
        [alt_text] => 'This is test',

    )

    [1] => array
    (
        [id]       => 3
        [title]    => Test1
        [alt_text] => 'This is test1',

    )

    [2] => array
    (
        [id]       => 7
        [title]    => Test2
        [alt_text] => "This is test2",

    ),

)

I want the value of the title for dynamic id.

E.g. If I pass 2, then it should return value of title as Test

I can do this with foreach loop no issue. But is there any core function to achieve this or a combination of core functions or a one-liner snippet?

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • 2
    No built-in way to do this, but the foreach loop should be quite straighforward and you could wrap it yourself in a function. – laurent Feb 28 '17 at 12:18
  • Do you mean you want to speed up the fetch? – Haotian Liu Feb 28 '17 at 12:19
  • Can you not use an associative array with the `id`s as key? This way, the lookup would be as easy as `$array[$id]['title']`. Are your `id`s unique? – domsson Feb 28 '17 at 12:19
  • In addition, if you worry about performance, add a `break` statement once you've found the title. – ValLeNain Feb 28 '17 at 12:20
  • You can look at some of the functions such as array_filter or array_reduce, that take a custom callback function - those can be used to achieve that. But they will of course loop over the data internally as well … so you might as well write code that is easily understandable by having one look at it yourself, using a “normal” loop. – CBroe Feb 28 '17 at 12:20
  • If you are worrying about the performance, you can use the search trees. – Haotian Liu Feb 28 '17 at 12:22
  • if it's the last element, the `break` won't help the performance. But given your structure, you don't really have a choice. – ValLeNain Feb 28 '17 at 12:23
  • You are worrying about the wrong thing. You could iterate over thousands of elements in no time. Bottlenecks in applications these days aren't on doing array lookups, it's on network connections, database fetch, etc. If really you think there's could be a performance issue, then try benchmarking it and you'll see there's problem no performance issue. – laurent Feb 28 '17 at 12:40

6 Answers6

1

Assuming that your ids are unique (!), you could simply use an associative array instead:

$yourArrray = array(
    2 => array('title' => 'Test', 'alt_text' => 'This is test'),
    3 => array('title' => 'Test1', 'alt_text' => 'This is test1'),
    7 => array('title' => 'Test2', 'alt_text' => 'This is test2')
);

This way, your lookup would be as easy as:

$yourArray[$id]['title']

For example:

$title = $yourArray[2]['title'] // $title Now contains "Test"
domsson
  • 4,553
  • 2
  • 22
  • 40
0

Using loop you can do this:

function getTitle($arr,$key){//$arr is your array
    foreach($arr as $keys=>$values){
        if($values['id'] == $key){
            return $values['title'];
        }
    }
    return null;
}

echo getTitle($arr,2);
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • @rahul_m in that case you should see this:http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php. and i don't think there is any direct function to get this done. – Suchit kumar Feb 28 '17 at 12:28
  • 2
    or construct your data like @domdom answer this will become very easy. – Suchit kumar Feb 28 '17 at 12:30
0

This function will help.

public function getValue($array, $id)
{
    foreach ($array as $value) {
        if($value['id'] == $id) {

          return $value['title'];
        }
    }

    return null;
}
Maulik Savaliya
  • 1,262
  • 7
  • 17
Ajay Korat
  • 716
  • 4
  • 14
0

Hope this function will help

public function getTitle($array, $id) {
    foreach ($array as $value) {
       if($value['id'] == $id) {
           return $value['title'];
        }
     }
   return null;
}
sandip kakade
  • 1,346
  • 5
  • 23
  • 49
0

This is the solution I ended up using:

echo $arr[array_search(7, array_column($arr, 'id'))]['title'];
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • You still haven't answered my question on whether or not your ids are unique. If so, you can simplify the whole lookup (see my answer). – domsson Feb 28 '17 at 12:37
  • Its just, that consider a set of categories which will be unique, I didn't mentioned that either. So if you had considered the case of what input array I post. You may get my concern – Rahul Feb 28 '17 at 12:38
0

There is one functionality I came across, Considering that id will be unique

$temp = array_column($arr,'title','id');
echo $temp[2]; // will result Test

Working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60