从数组值中搜索特定单词

时间:2017-02-13 10:11:02

标签: php arrays

Array
(
    [hid] => 1
    [name] => Leonardo Royal Hotel Berlin Alexanderplatz
    [address] => Otto-Braunstrasse 90, D-10249 Berlin, Germany
    [price_from] => 5,180
    [main_image] => 
    [latitude] => 52.5271842
    [longitude] => 13.4214713,17
    [images] => Array
        (
            [0] => LeonardoRoyalHotelBerlinAlexanderplatz-800-600-1.jpg
            [1] => LeonardoRoyalHotelBerlinAlexanderplatz-800-600-2.jpg
            [2] => LeonardoRoyalHotelBerlinAlexanderplatz-800-600-3.jpg
            [3] => LeonardoRoyalHotelBerlinAlexanderplatz-800-600-4.jpg
        )

)

我有这样的数组。我怎样才能像"柏林"从数组值?

2 个答案:

答案 0 :(得分:1)

如果您不想知道某个键中存在berlin,则可以使用array_walk_recursive。如果您想了解更多信息,请发表评论。

$data = 'Your array here';
$str = 'Berlin';

array_walk_recursive($data, function($item, $key) use ($str) {
   if (stripos($item, $str) !== false) {
       echo $key; // or any action you want.
   }
});

如果印有某些东西,柏林就会存在一些关键。

答案 1 :(得分:-1)

希望此代码有帮助

<?php
$a=array("red folk","green book","blue");
 $match="book";
 $searchedValue="";
   foreach($a as $key=>$val){
   if(strpos($val,$match)) {
   $searchedValue=$val;
  }
  }
 echo $searchedValue;