2017-02-23 65 views
1

我有一個三維數組。我試圖通過關鍵字在三維數組中搜索

$Platinum = array 
     (
     array(
      "Name"  => "Art of Life", 
      "description" => "a description", 
      "keyword"  => array(
       "designing", 
       "interior design websites", 
      ), 
     ), 
     array(
      "Name"  => "Golden Art", 
      "description" => "a description", 
      "keyword"  => array(
       "designing", 
       "design company", 
       "design services", 
      ), 
     ), 
    ); 

搜索公司我想這print_r(array_search("designing", array_column($Platinum[0], 'keyword'))); ,但沒有奏效。
如何查找關鍵字並獲取公司名稱,如果我找到關鍵字?

回答

1

使用此代碼,

$arr = array_column($Platinum, 'keyword'); 
$current_key = ''; 
foreach($arr as $k => $v){ 
    if(in_array('designing', $v)){ 
     $current_key = $k; 
     break; 
    }  
} 
$company_name = $Platinum[$current_key]['Name']; 
echo $company_name;die; 

試試看吧,這會工作。

+0

感謝。它適用於我 –

+0

你是最受歡迎的! – rahulsm

0

此功能會爲你做的工作:

<?php 
function find_company_by_keyword($companies, $search_keyword) 
{ 
    $search_keyword = trim(strtolower($search_keyword)); 

    $results = []; 

    foreach ($companies as $company) { 
     $keyword_found = false; 

     foreach ($companies['keyword'] as $keyword) { 
      if ($keyword_found) continue; 

      $keyword = trim(strtolower($keyword)); 
      if ($keyword == $search_keyword) $results[] = $company; 
     } 
    } 
    return $results 
} 

用法:

<?php 
$results = find_company_by_keyword($Platinum,'designing');