Find in array smarty

3.8k Views Asked by At

I have an array

Array
(
[0] => Array
    (
        [id] => 32
        [type] => 4           
    )

[1] => Array
    (
        [id] => 51
        [type] => 9            
    )
    ..
    ..
    ..
    ..
    ..
 [84] => Array
    (
        [id] => 51
        [type] => 9 
        [cnt] => 1180                       
    )   
 ) 

And i need to find value of [cnt] in my array, which lies only in one index of my entire array. The index of [cnt] is not constant.

2

There are 2 best solutions below

1
On BEST ANSWER

You need to loop over while array.

{foreach from=$data key='k' item='i'}
    {if $i.cnt == 1180}
        {assign var='key' value=$k}
    {/if}
{/foreach}

Value 1180 is in key {$key}.

{$data[$key]|print_r}

If smarty has something like break, you can add it into if condition.

EDIT:
If you need to find value of cnt, it should be:

{foreach from=$data key='k' item='i'}
    {if isset($i.cnt)}
        CNT value is: {$i.cnt}
        Key of item with CNT: {$k}
    {/if}
{/foreach}
0
On

You can simply use array_column function of PHP as

$arr = array(  
           array('id'=>1,'value'=>'asdas'),     
           array('id'=>1,'value'=>'asdas'),  
           array('id'=>1,'value'=>'asdas'),  
           array('id'=>1,'value'=>'asdas'),  
           array('id'=>1,'value'=>'asdas','cnt'=>1185)  
        );  

print_r(array_column($arr, 'cnt'));