php数组搜索值

时间:2016-09-11 11:21:44

标签: php arrays search

$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 Word"摩托车"并获得iri http路径。

任何帮助??

2 个答案:

答案 0 :(得分: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>";
}

答案 1 :(得分:0)

我已经看过你提供的array,并想指出一些事实:

  • "Motorcycle"位于"product"
  • "Motorcycle"重复,因此我们需要能够决定我们之前搜索的"Motorcycle"
  • "Motorcycle"在多个位置以不区分大小写的方式显示为substring

首先,让我们实现一个函数,该函数将找到iri的相应string。请注意,我们需要确定我们是否在搜索更多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");