2014-12-19 62 views
-1

分離我具有以下陣列搜索值在PHP

Array 
(
    [0] => Array 
     (
      [data] => PHP 
      [attribs] => Array 
       (
       ) 

      [xml_base] => 
      [xml_base_explicit] => 
      [xml_lang] => 
     ) 

    [1] => Array 
     (
      [data] => Wordpress 
      [attribs] => Array 
       (
       ) 

      [xml_base] => 
      [xml_base_explicit] => 
      [xml_lang] => 
     ) 
) 

一個varialbe像是$ var = 'PHP,的Joomla' 值;

我曾嘗試以下,但不工作

$key = in_multiarray('PHP', $array,"data"); 
function in_multiarray($elem, $array,$field) 
    { 
     $top = sizeof($array) - 1; 
     $bottom = 0; 
     while($bottom <= $top) 
     { 
      if($array[$bottom][$field] == $elem) 
       return true; 
      else 
       if(is_array($array[$bottom][$field])) 
        if(in_multiarray($elem, ($array[$bottom][$field]))) 
         return true; 

      $bottom++; 
     }   
     return false; 
    } 

所以要檢查如果是$ var任何價值存在於陣列(不區分大小寫)

我如何能做到這一點不循環?

+0

循環通過它。你做了什麼。顯示一些努力 – putvande 2014-12-19 09:13:39

+0

foreach()爆炸()和buisnesslogic是你的朋友 – donald123 2014-12-19 09:13:55

+0

@Bhumi Shah,這是你真正的數組,可以將數組深度超過這個嗎? – 2014-12-19 09:16:32

回答

1

這應該爲你工作:

(把代碼中的一些註釋說明什麼goning上)

<?php 

    //Array to search in 
    $array = array(
       array(
        "data" => "PHP", 
        "attribs" => array(), 
        "xml_base" => "", 
        "xml_base_explicit" => "", 
        "xml_lang" => "" 
       ), 
       array(
       "data" => "Wordpress", 
       "attribs" => array(), 
       "xml_base" => "", 
       "xml_base_explicit" => "", 
       "xml_lang" => "Joomla" 
       ) 
      ); 

    //Values to search 
    $var = "Php, Joomla"; 

    //trim and strtolower all search values and put them in a array 
    $search = array_map(function($value) { 
        return trim(strtolower($value)); 
    }, explode(",", $var)); 

    //function to put all non array values into lowercase 
    function tolower($value) { 

     if(is_array($value)) 
      return array_map("tolower", $value); 
     else 
      return strtolower($value); 

    } 

    //Search needle in haystack 
    function in_array_r($needle, $haystack, $strict = false) { 
     foreach ($haystack as $item) { 
      if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { 
       return true; 
      } 
     } 

     return false; 
    } 

    //Search ever value in array 
    foreach($search as $value) { 

     if(in_array_r($value, array_map("tolower", array_values($array)))) 
      echo $value . " found<br />"; 
    } 

?> 

輸出:

php found 
joomla found 
+0

謝謝。它正在爲固定值工作,但如果我有$ var ='Php 5.4,Joomla';那麼還工作? – 2014-12-19 10:09:27

+0

@BhumiShah如果你在數組中也有值,例如:''xml_base「=>」PHP 5.4「,並且你有:'$ var ='pHp 5.4,Joomla';'那麼它將起作用是的 – Rizier123 2014-12-19 10:11:46

+0

值不是在數組中,但PHP = PHP 5.4所以它必須給出值 – 2014-12-19 10:15:23

0

根據我的理解,您試圖傳遞字符串ex:'php'和元素的關鍵字'data'。 所以你的密鑰可以容納一個值或一個數組。

$key = in_multiarray("php", $array,"data"); 
    var_dump($key); 
    function in_multiarray($elem, $array,$field) 
    { 
      $top = sizeof($array) - 1; 
      $bottom = 0; 
      while($bottom <= $top) 
      { 

       if(is_array($array[$bottom][$field])) 
       { 
        foreach($array[$bottom][$field] as $value) 
        { 
         if(strtolower(trim($value)) == strtolower(trim($elem))) 
         { 
          return true; 
         } 
        } 
        } 
        else if(strtolower(trim($array[$bottom][$field])) == strtolower(trim($elem))) 
        { 
        return true; 
        } 

       $bottom++; 
      }   

     return false; 
    } 
+0

它不工作逗號分隔的字符串$ var ='Php,Joomla'; – 2014-12-19 09:52:26

+0

分解字符串並創建一個數組,嘗試循環使用該數組,嘗試:) – 2014-12-19 10:07:15