1
Array
(
    [0] => Array
        (
            [id] => 61
            [title] => werwer
        )

    [1] => Array
        (
            [id] => 63
            [title] => test
        )

    [2] => Array
        (
            [id] => 88
            [title] => test 2
        )
)

How can I get title which has id=63 in above type of array without looping.

Harish Singh
  • 3,359
  • 5
  • 24
  • 39
  • `$array[1][63]` - try this. And read this: http://php.net/manual/en/language.types.array.php – sectus Dec 12 '13 at 09:28
  • If you realy just want to answer his example (and only this exact array structure) it would still be $array[1]['title']. Your example would access an index that isn't set. – JustAPirate Dec 12 '13 at 09:33

3 Answers3

1

Actually, you can't do that without looping. That doesn't mean you have to use loop (foreach/while e t.c.) - but using array functions you will internally iterate array in any case.

For example, in PHP 5.5 that is:

$array = [
['id'=>63, 'title'=>'foo'],
['id'=>65, 'title'=>'bar']
];
//use this - if there are more than 2 connected to `id` fields:
$ids    = array_flip(array_column($array, 'id'));//here is iteration, gathering id column
$result = $array[$ids[63]]['title'];
//or else, if `title` is always the only field:
$result = array_column($array, 'title', 'id')[63];
//var_dump($result);

-and so on. array_search() with array_walk() (or similar ways) will hide iteration from you, but it will be done in any case.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • thanks, it can be done in ease with `$ids = array_column($records, 'title', 'id'); echo $result = $ids[63];` but i don't have PHP 5.5 – Harish Singh Dec 12 '13 at 09:44
  • You can use [this](http://stackoverflow.com/questions/18784897/how-can-i-get-the-array-of-a-single-type-from-a-multi-dimensional-array-without/18784941#18784941) for earlier versions – Alma Do Dec 12 '13 at 09:46
0

Well You can try some crazy stuff like this

array_search(61,
    array_combine(
            array_map(function ($a) {
                return $a['title'];
            }, $arr),
            array_map(function ($a) {
                return $a['id'];
            }, $arr)));

but it's highly uneffective (there are 4 loops 'hidden' in 2 array_map, array_combine, and array_search). I advise to use looping - just like @JustAPirate wrote.

$arr = array(array('id' => 61, 'title' => 'werwer'),
        array('id' => 62, 'title' => 'asdasd'),
        array('id' => 63, 'title' => 'qweqwe'),);

function f($arr, $id) {
    foreach ($arr as $a) {
        if ($a['id'] == $id)
            return $a['title'];
    }
}

var_dump(f($arr, 61));
tiriana
  • 668
  • 1
  • 7
  • 24
-1

No way to get it without a loop if you don't already know the index of the id 63 in the array.

foreach($array as $element) {
    if($element['id'] == 63) {
        echo $element['title'];
    }
}

You could change the structure of the array:

$array = array(61 => 'werwer', 63 => 'test', 88 => 'test 2');

That would result in:

Array
(
    [61] => 'werwer'
    [63] => 'test'
    [88] => 'test 2'
)

Then you could access/get it with $array[63]

JustAPirate
  • 164
  • 5