2016-05-12 34 views
2

我有一個動態數組。我想搜索是否有任何值「雕刻」,然後它應該返回true。 我如何能實現像下面如何搜索多維數組中的值

Array 
(
    [0] => Array 
     (
      [product_option_id] => 2222 
      [product_option_value] => Array 
       (
        [0] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8934 
          [option_value_id] => 229 
          [name] => Cherokee 
          [image] => 
          [price] => $12.95 
          [price_prefix] => + 
         ) 

        [1] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8935 
          [option_value_id] => 228 
          [name] => Narkisim 
          [image] => 
          [price] => $12.95 
          [price_prefix] => + 
         ) 

        [2] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8933 
          [option_value_id] => 230 
          [name] => Segoe Script 
          [image] => 
          [price] => $12.95 
          [price_prefix] => + 
         ) 

        [3] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8932 
          [option_value_id] => 227 
          [name] => No Engraving 
          [image] => 
          [price] => 
          [price_prefix] => + 
         ) 

       ) 

      [option_id] => 57 
      [name] => Engraving Fonts 
      [type] => select 
      [value] => 
      [required] => 0 
     ) 

    [1] => Array 
     (
      [product_option_id] => 2221 
      [product_option_value] => Array 
       (
       ) 

      [option_id] => 56 
      [name] => Engraving Text 
      [type] => text 
      [value] => 
      [required] => 0 
     ) 

    [2] => Array 
     (
      [product_option_id] => 2187 
      [product_option_value] => Array 
       (
        [0] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8855 
          [option_value_id] => 223 
          [name] => Black 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [1] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8856 
          [option_value_id] => 220 
          [name] => Blue 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [2] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8857 
          [option_value_id] => 221 
          [name] => Green 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [3] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8858 
          [option_value_id] => 224 
          [name] => Pink 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [4] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8859 
          [option_value_id] => 222 
          [name] => Purple 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [5] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8860 
          [option_value_id] => 225 
          [name] => White 
          [image] => 
          [price] => $4.95 
          [price_prefix] => + 
         ) 

        [6] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8854 
          [option_value_id] => 226 
          [name] => No Clipboard Booklight 
          [image] => 
          [price] => 
          [price_prefix] => + 
         ) 

       ) 

      [option_id] => 55 
      [name] => Add a Clipboard Booklight 
      [type] => select 
      [value] => 
      [required] => 0 
     ) 

    [3] => Array 
     (
      [product_option_id] => 1792 
      [product_option_value] => Array 
       (
        [0] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 7682 
          [option_value_id] => 71 
          [name] => Blue 
          [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Calculator_Clips/Calculator_Clip_4-50x50.jpg 
          [price] => $3.75 
          [price_prefix] => + 
         ) 

        [1] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 7683 
          [option_value_id] => 70 
          [name] => Green 
          [image] => 
          [price] => $3.75 
          [price_prefix] => + 
         ) 

        [2] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 11178 
          [option_value_id] => 219 
          [name] => Red 
          [image] => 
          [price] => $3.75 
          [price_prefix] => + 
         ) 

        [3] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 11179 
          [option_value_id] => 218 
          [name] => Purple 
          [image] => 
          [price] => $3.75 
          [price_prefix] => + 
         ) 

        [4] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 11180 
          [option_value_id] => 254 
          [name] => Clear 
          [image] => 
          [price] => $3.75 
          [price_prefix] => + 
         ) 

        [5] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 7681 
          [option_value_id] => 84 
          [name] => No Calculator Clip 
          [image] => 
          [price] => 
          [price_prefix] => + 
         ) 

       ) 

      [option_id] => 19 
      [name] => Add a Calculator Clip 
      [type] => select 
      [value] => 
      [required] => 0 
     ) 

    [4] => Array 
     (
      [product_option_id] => 2328 
      [product_option_value] => Array 
       (
        [0] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 9291 
          [option_value_id] => 231 
          [name] => Add 1 Notepad 
          [image] => 
          [price] => $1.99 
          [price_prefix] => + 
         ) 

        [1] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 9292 
          [option_value_id] => 232 
          [name] => Add 2 Notepads 
          [image] => 
          [price] => $2.99 
          [price_prefix] => + 
         ) 

        [2] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 9293 
          [option_value_id] => 234 
          [name] => No Notepad 
          [image] => 
          [price] => 
          [price_prefix] => + 
         ) 

       ) 

      [option_id] => 58 
      [name] => Add a 3.5" H x 10.25" W Notepad 
      [type] => select 
      [value] => 
      [required] => 0 
     ) 

    [5] => Array 
     (
      [product_option_id] => 2186 
      [product_option_value] => Array 
       (
        [0] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8851 
          [option_value_id] => 67 
          [name] => Black 
          [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg 
          [price] => $9.99 
          [price_prefix] => + 
         ) 

        [1] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8852 
          [option_value_id] => 68 
          [name] => Blue 
          [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg 
          [price] => $9.99 
          [price_prefix] => + 
         ) 

        [2] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8853 
          [option_value_id] => 69 
          [name] => Red 
          [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg 
          [price] => $9.99 
          [price_prefix] => + 
         ) 

        [3] => Array 
         (
          [poip_image] => 
          [product_option_value_id] => 8850 
          [option_value_id] => 79 
          [name] => No Stand 
          [image] => 
          [price] => 
          [price_prefix] => + 
         ) 

       ) 

      [option_id] => 18 
      [name] => Add a Clipboard Stand 
      [type] => select 
      [value] => 
      [required] => 0 
     ) 

) 

回答

2

您可以使用一個遞歸函數在多維數組陣列搜索。

function in_array_recursive($needle, $haystack) { 
    foreach ($haystack as $value) { 
     if ($needle == $value) return true; 
     if (is_array($value) && in_array_recursive($needle, $value)) return true; 
    } 
    return false; 
} 

$found = in_array_recursive('Engraving', $your_array); 

上述功能將只匹配整個值。如果您需要匹配部分字符串,則需要添加strpos檢查(謝謝,AbraCadaver)

function in_array_recursive($needle, $haystack) { 
    foreach ($haystack as $value) { 
     if (is_array($value)) { 
      if (in_array_recursive($needle, $value)) return true; 
     } else { 
      if (strpos($value, $needle) !== false) return true; 
     } 
    } 
    return false; 
} 

$found = in_array_recursive('Engraving', $your_array); 
+0

這總是返回'布爾(假)' – baig772

+0

改變'$針== $ value'到'strpos($值,$針)!== FALSE' – AbraCadaver

+0

噢,這只是比賽的完整價值。我錯過了示例文本是部分值。 @AbraCadaver與非字符串值比較時不會造成問題嗎? –