在多维数组中搜索

时间:2013-03-22 00:55:05

标签: php arrays opencart

这是一个多维数组:

[0] => Array (   
    [order_total_id] => 8160  
    [order_id] => 2048 [code] => sub_total  
    [title] => Medzi-súčet  
    [text] => 5,75€  
    [value] => 5.7500  
    [sort_order] => 1 )   
[1] => Array (   
    [order_total_id] => 8161  
    [order_id] => 2048  
    [code] => shipping  
    [title] => Doporučený list  
    [text] => 2,00€  
    [value] => 2.0000  
    [sort_order] => 3 )  
[2] => Array (  
    [order_total_id] => 8162  
    [order_id] => 2048  
    [code] => tax  
    [title] => DPH 20%  
    [text] => 1,15€  
    [value] => 1.1500  
    [sort_order] => 5 )  
[3] => Array (  
    [order_total_id] => 8163  
    [order_id] => 2048  
    [code] => total  
    [title] => Celkom  
    [text] => 8,90€  
    [value] => 8.9000  
    [sort_order] => 6 )  

我的问题是:有可能在m.d.数组中获得[value] => 8.9000[value]可以多次)?我认为正确的方法是首先找到[code] => total来识别正确的数组,然后在同一个数组中找到[total]键。为什么?因为[code] => total可以在子数组[0],[1],[2],[3]中,有时只有[0],[1]子数组。

如果有效,最好找到[code] => shipping[value] => 2.0000

谢谢你们。

1 个答案:

答案 0 :(得分:1)

http://www.php.net/manual/en/function.array-search.php#69965

<?php    

function myMulti_Array_Search($theNeedle, $theHaystack, $keyToSearch) 
        { 
        foreach($theHaystack as $theKey => $theValue) 
            { 
            $intCurrentKey = $theKey;    

            if($theValue[$keyToSearch] == $theNeedle) 
                { 

                return $intCurrentKey ; 
                } 
            else 
                { 
                return 0; 
                } 
            } 
        } 

?>