我有一个数组,我想从中选择一个特定值。
我有一个函数可以检索一些api数据并放入这个数组中。
功能如下;
function get_patronapi_data($id) {
$apiurl = APISERVER . "/PATRONAPI/$id/dump";
$api_contents = get_api_contents($apiurl); //get_api_contents() is a seperate function
$api_array_lines = explode("\n", $api_contents);
foreach ($api_array_lines as $key => $api_line) {
$api_line_arr = explode("=", $api_line);
$regex_match = array("/\[(.*?)\]/","/\s/","/#/");
$regex_replace = array('','','NUM');
$key = trim(preg_replace($regex_match, $regex_replace, $api_line_arr[0]));
$api_data[$key] = trim($api_line_arr[1]);
}
return $api_data;
}
$return_value = get_patronapi_data($id);
当我var_dump($api_array_lines)
时,我得到以下内容;
array(38) {
[0]=>
string(14) "REC INFO[p!]=p"
[1]=>
string(22) "EXP DATE[p43]=31-12-19"
[2]=>
string(13) "PCODE1[p44]=-"
[3]=>
string(13) "PCODE2[p45]=-"
[4]=>
string(14) "PCODE3[p46]=58"
[5]=>
string(14) "P TYPE[p47]=10"
[6]=>
string(17) "TOT CHKOUT[p48]=0"
[7]=>
string(17) "TOT RENWAL[p49]=0"
[8]=>
string(17) "CUR CHKOUT[p50]=0"
[9]=>
string(26) "BIRTH DATE[p51]= - -19 "
[10]=>
string(20) "HOME LIBR[p53]=qm "
[11]=>
string(15) "PMESSAGE[p54]=-"
[12]=>
string(13) "MBLOCK[p56]=-"
[13]=>
string(15) "REC TYPE[p80]=p"
[14]=>
string(21) "RECORD #[p81]=1083177"
[15]=>
string(17) "REC LENG[p82]=300"
[16]=>
string(21) "CREATED[p83]=01-10-12"
[17]=>
string(21) "UPDATED[p84]=08-10-15"
[18]=>
string(19) "REVISIONS[p85]=1091"
[19]=>
string(13) "AGENCY[p86]=1"
[20]=>
string(15) "CL RTRND[p95]=0"
[21]=>
string(22) "MONEY OWED[p96]=£0.00"
[22]=>
string(17) "CUR ITEMA[p102]=0"
[23]=>
string(17) "CUR ITEMB[p103]=0"
[24]=>
string(18) "ILL REQUES[p122]=0"
[25]=>
string(17) "CUR ITEMC[p124]=0"
[26]=>
string(17) "CUR ITEMD[p125]=0"
[27]=>
string(25) "CIRCACTIVE[p163]=08-10-15"
[28]=>
string(19) "NOTICE PREF[p268]=-"
[29]=>
string(34) "PATRN NAME[pn]=Smith, Mr Jo"
[30]=>
string(31) "PATRN NAME[pn]=Smith, Jo"
[31]=>
string(69) "ADDRESS[pa]=IT"
[32]=>
string(18) "TELEPHONE[pt]=6167865"
[33]=>
string(21) "UNIQUE ID[pu]=678678678"
[34]=>
string(21) "P BARCODE[pb]=456456"
[35]=>
string(22) "P BARCODE[pb]=345345"
[36]=>
string(33) "EMAIL ADDR[pz]=sdfsdF@sdfsdf.com"
[37]=>
string(12) "TITLE[pe]=Mr"
}
我需要从数组中获得的唯一值是EXP DATE
(=
符号后面的所有内容) - 此值不始终位于[1]
位置 - 如何我可以选择仅这个值吗?
是否需要额外的正则表达式或我的功能是否需要更改?
我必须说我没有写上面的代码from here。
这里非常新的php和自学,所以任何帮助都非常感谢。
答案 0 :(得分:1)
笨拙但可行:
foreach($api_array_lines as $line){ // go through each item in the array
if(strpos($line, "EXP DATE") !== false){ //if the string "EXP DATE" DOES NOT exist in the string "$line", it will return false
$data = explode("=", $line); //split the string into an array, split by the character "="
echo $data[1]; //return the second element in array ($data[0] would be "EXP DATE"
}
}
或作为一项功能:
function getLineData($array, $keyword){
foreach($array as $line){
if(strpos($line, $keyword) !== false){
$data = explode("=", $line);
return $data[1];
}
}
}
$data_you_require = getLineData($api_array_lines, "EXP DATE");
echo $data_you_require; //outputs 31-12-19
答案 1 :(得分:1)
我做了一个很大的假设,即每一行都有" ="括号中的值" [p43]"钥匙总是一样的。
将简单数组移动到索引数组:
$new_array = array();
foreach ($api_array_lines as $line) {
list($key, $value) = explode('=',$line);
$new_array[$key]=$value;
}
然后引用你需要的密钥:
$key_to_find = 'EXP DATE[p43]';
echo $new_array[$key_to_find];