2016-04-26 54 views
0

我有一個很大的關聯數組,其中有許多鍵和值,看起來像這樣。我想搜索「經常」的特定值並檢索具有該值的所有密鑰。如何搜索值以獲取密鑰並使用該密鑰獲取另一個陣列中的另一個密鑰?

$data = array(
'token' => 'token', 
'content' => 'record', 
'format' => 'json', 
'type' => 'flat', 
'records' => array($record), 
'fields' => array(), 
'returnFormat' => 'json' 
); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'url'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&')); 
$output = curl_exec($ch); 
$row = json_decode($output, true); 

foreach ($row[0] as $key => $value) { 

       $first_array=array($variable_name=>$value); 

       $often_variable=array_search("Often <br> (3)", $first_array);     
      } 

然後,我想使用我的第二個數組,它具有該鍵作爲其值,並將該數組的鍵打印到表中。

$objPHPExcel = PHPExcel_IOFactory::load("123.csv"); 

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { 
$worksheetTitle  = $worksheet->getTitle(); 
$highestRow   = $worksheet->getHighestRow(); // e.g. 10 
$highestColumn  = $worksheet->getHighestColumn(); // e.g 'F' 
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
$nrColumns = ord($highestColumn) - 64; 

for ($row2 = 2; $row2 <= $highestRow; ++ $row2) { 
    $variable_name = $worksheet->getCellByColumnAndRow(0, $row2)->getValue(); 
    $field_label = $worksheet->getCellByColumnAndRow(4, $row2)->getValue(); 

    $second_array=array($field_label=>$variable_name) 
} 

我已經使用了array_search()但不確定這是否是這個用例的最佳選項。

+0

發表您的完整系列... –

+0

參見:http://stackoverflow.com/a/2960083/3933332然後就通過鍵陣列和循環在第二個 – Rizier123

+0

發佈您的代碼搜索請爲能夠幫助你 – Basti

回答

2
foreach ($first_array as $var_name => $val){ 
    if ($value == "Often"){ 
     foreach ($second_array as $field_label => $variable_name){ 
     if ($variable_name == $var_name){ 
      echo "field_label: $field_label\n"; 
     }; 
     } 
    } 
    }