2016-05-12 13 views
2

Ich habe ein Array, das dynamisch ist. Ich möchte suchen, wenn es einen Wert wie "Gravur" gibt, dann sollte es wahr zurückgeben. Wie kann ich wie Array-Suche in mehrdimensionales Array implementieren unterSo suchen Sie nach einem Wert im mehrdimensionalen Array

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 
     ) 

) 

Antwort

2

Sie eine rekursive Funktion verwenden können.

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); 

Die obige Funktion wird nur ganze Werte übereinstimmen. Wenn Sie Teile von Zeichenfolgen abgleichen müssen, müssen Sie eine strpos-Prüfung hinzufügen (Danke, 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

Dies kehrt immer 'bool (false)' – baig772

+0

ändern '$ Nadel == $ value' auf' strpos ($ value, $ needle)! == false' – AbraCadaver

+0

Oh ja, dies nur Streichhölzer komplette Werte . Ich habe übersehen, dass der Beispieltext ein Teilwert war. @AbraCadaver Wird es beim Vergleich mit Nicht-String-Werten nicht zu Problemen kommen? –

Verwandte Themen