我一直在试图获得我的综合汇率的值,但遇到了一些麻烦,简单地获得值:
Array ( [0] => Array ( [ZipCode] => 01014 [CombinedRate] => 0.0625 ) ) <--- this is the array that is printed.
下面是完整的代码:
<?php
function csv_to_array($filename= '', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
$zipsearch = csv_to_array($_SERVER['DOCUMENT_ROOT'].'/textfiles/taxes.csv');
function search($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, search($subarray, $key, $value));
}
}
return $results;
}
$valueofzip = search( $zipsearch, 'ZipCode', '01014');
print_r($valueofzip)
/*
Need Code To print Just value of that array
*/
?>
我只需要从打印的数组中找到邮政编码后的组合比率的值,但无论出于何种原因,我只是错过了它,
如何从打印的数组中获得"组合速率"的值:array ([0] => array ([ZipCode] => 01014 [CombinedRate] => 0.0625))
基本上这个页面会打印出一个单一的税率
看起来您想要检索的值很简单:
$valueofzip[0]['CombinedRate']
。$valueofzip
的第0个元素中键为"combinerate"的值。
如果您期望search
函数返回多个数组,那么foreach
循环将更合适:
foreach($valueofzip as $zip) {
if (isset($zip['CombinedRate'])) echo $zip['CombinedRate'];
}
根据您想对该值做什么,您可能还需要查看文档中的array_walk
、array_map
等。它们可用于遍历数组并对每个元素应用回调函数。例如:
$valueofzip = array(array("ZipCode" => 01014, "CombinedRate" => 0.0625),
array("ZipCode" => 02025, "CombinedRate" => 0.125 ));
$combined_rates = array_map(function($x) { if (isset($x['CombinedRate'])) return $x['CombinedRate']; }, $valueofzip));
print_r($combined_rates);
输出:Array
(
[0] => 0.0625
[1] => 0.125
)