2016-09-11 134 views
2
$arr = "Array 
(
    [industry] => Array 
     (
      [0] => Automotive_industry 
      [1] => Automotive_industry 
      [2] => Restaurant 
      [3] => Restaurant 
      [4] => Restaurant 
      [5] => Restaurant 
      [6] => Automotive_industry 
      [7] => Automotive_industry 
      [8] => Restaurant 
      [9] => Role-playing_game 
      [10] => Bicycle 
      [11] => Automotive_industry 
      [12] => Automotive_industry 
      [13] => Automotive_industry 
      [14] => Automotive_industry 
      [15] => Restaurant 
      [16] => Restaurant 
      [17] => Restaurant 
      [18] => Restaurant 
      [19] => Automotive_industry 
      [20] => Automotive_industry 
      [21] => Restaurant 
      [22] => Role-playing_game 
      [23] => Bicycle 
      [24] => Automotive_industry 
      [25] => Automotive_industry 
     ) 

    [product] => Array 
     (
      [0] => Motorcycle 
      [1] => Motorcycle 
      [2] => French_fries 
      [3] => Poutine 
      [4] => Sandwich 
      [5] => Milkshake 
      [6] => Motorcycle 
      [7] => Scooter_(motorcycle) 
      [8] => Hamburger 
      [9] => Champions_(role-playing_game) 
      [10] => Bicycle 
      [11] => Scooter_(motorcycle) 
      [12] => Scooter_(motorcycle) 
      [13] => Motorcycle 
      [14] => Motorcycle 
      [15] => French_fries 
      [16] => Poutine 
      [17] => Sandwich 
      [18] => Milkshake 
      [19] => Motorcycle 
      [20] => Scooter_(motorcycle) 
      [21] => Hamburger 
      [22] => Champions_(role-playing_game) 
      [23] => Bicycle 
      [24] => Scooter_(motorcycle) 
      [25] => Scooter_(motorcycle) 
     ) 

    [iri] => Array 
     (
      [0] => http://dbpedia.org/resource/Hero_MotoCorp 
      [1] => http://dbpedia.org/resource/Hero_MotoCorp 
      [2] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [3] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [4] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [5] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [6] => http://dbpedia.org/resource/Hero_MotoCorp 
      [7] => http://dbpedia.org/resource/Hero_MotoCorp 
      [8] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [9] => http://dbpedia.org/resource/Hero_Games 
      [10] => http://dbpedia.org/resource/Hero_Cycles 
      [11] => http://dbpedia.org/resource/Hero_MotoCorp 
      [12] => http://dbpedia.org/resource/Hero_MotoCorp 
      [13] => http://dbpedia.org/resource/Hero_MotoCorp 
      [14] => http://dbpedia.org/resource/Hero_MotoCorp 
      [15] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [16] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [17] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [18] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [19] => http://dbpedia.org/resource/Hero_MotoCorp 
      [20] => http://dbpedia.org/resource/Hero_MotoCorp 
      [21] => http://dbpedia.org/resource/Hero_Certified_Burgers 
      [22] => http://dbpedia.org/resource/Hero_Games 
      [23] => http://dbpedia.org/resource/Hero_Cycles 
      [24] => http://dbpedia.org/resource/Hero_MotoCorp 
      [25] => http://dbpedia.org/resource/Hero_MotoCorp 
     ) 

)"; 

我想從上面陣列 字「摩托車」搜索wrod並獲得IRI HTTP路徑。PHP數組搜索值

任何幫助?

回答

0

你沒有說如果你想要他們全部或只是第一個。

$val = "Motorcycle"; 


// only first 
$ind = array_search ($val, $arr ["product"], true); 
if ($ind >= 0) { 
    echo $arr ["iri"] [$ind] . "<br>"; 
} 

echo "<hr>" ; 

// all values 
foreach (array_keys ($arr ["product"], $val, true) as $ind) { 
    echo $arr ["iri"] [$ind] . "<br>"; 
} 
0

我已經採取了看看array你給了,並想指出幾個事實:

  • "Motorcycle"裏面"product"
  • "Motorcycle"是重複的,所以我們需要能夠決定哪個"Motorcycle"的以前我們正在搜索
  • "Motorcycle"在幾個位置以不區分大小寫的方式顯示爲substring s

因此,首先讓我們實現一個函數,該函數將找到string的相應iri。請注意,我們需要決定他們是否重複我們是否更多iri元素搜索,我們是否希望它是區分大小寫的:

function getIri($input, $str, $duplicates = false, $caseInsensitive = false) { 
    $output = array(); 
    foreach ($input["iri"] as $element) { 
     if (($caseInsensitive && (stripos($element, $str) >= 0)) || 
      (strpos($element, $str) >= 0)) { 
       $output[]=$element; 
       if (!$duplicates) { 
        return $output; 
       } 
     } 
    } 
    return $output; 
} 

然後,你需要做同樣的product

function getPreviousProductOf($input, $str, $duplicates = false, $caseInsensitive = false, $substring = false) { 
    $output = array(); 
    //We start from 1, as the 0'th element does not have a previous 
    for ($index = 1; $index < count($input["product"]); $index++) { 
     if (($substring && $caseInsensitive && (stripos($input["product"][$index], $str) >= 0)) || 
      ($substring && !$caseInsensitive && (strpos($input["product"][$index], $str) >= 0)) || 
      (!$substring && $caseInsensitive && (strcasecmp($input["product"][$index], $str) === 0)) || 
      (!$substring && !$caseInsensitive && ($input["product"][$index] === $str))) { 
      $output = array_merge($output, getIri($input, $input["product"][$index]), $duplicate, $caseInsensitive); 
      if (!$duplicate) { 
       return $output; 
      } 
     } 
    } 
    return $output; 
} 

最後,你需要調用第二function,像這樣:

var $result = getPreviousProductOf($input, "Motorcycle");