如何搜索值以获取键并使用该键获取不同数组中的另一个键?
How can I search for value to get key and use that key to get another key in a different array?
我有一个大型关联数组,其中包含许多键和值,如下所示。我想搜索特定值 "Often" 并检索所有具有该值的键。
$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);
}
然后,我想使用我的第二个数组将该键作为其值并将该数组的键打印到 table.
$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() 但不确定这是否是这个用例的最佳选择。
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";
};
}
}
}
我有一个大型关联数组,其中包含许多键和值,如下所示。我想搜索特定值 "Often" 并检索所有具有该值的键。
$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);
}
然后,我想使用我的第二个数组将该键作为其值并将该数组的键打印到 table.
$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() 但不确定这是否是这个用例的最佳选择。
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";
};
}
}
}