0

I have an array, and I would like to select one specif value from it.

I have a function that retrieves some api data and puts in in an this array.

The function is as follows;

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);

When I var_dump($api_array_lines) I get the following;

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][email protected]"
  [37]=>
  string(12) "TITLE[pe]=Mr"
}

The only value I require from the array is EXP DATE (everything after the = sign) - this value is not always at position [1] - how can I select only this value?

Does it require additional regex or does my function need changed?

I must say I didn't write the above code I got it from here.

Quite new to php here and teaching myself so any help is greatly appreciated.

2 Answers 2

1

Clunky but would work:

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"
    }
}

Or as a function:

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
Sign up to request clarification or add additional context in comments.

3 Comments

That works! Can you briefly explain what I was doing wrong or how you corrected it as I would like to learn! Thanks again. Decided to use your first suggestion not the function.
@johnny_s Don't forget to mark it as the answer if it worked!
Was just about to - perfect answer and thanks for the explanation! :)
1

I'm making a big assumption that every line has "=" and that the values in the brackets "[p43]" are always the same for the key.

Move the simple array to an indexed array:

$new_array = array();    
foreach ($api_array_lines as $line) {
    list($key, $value) = explode('=',$line);
    $new_array[$key]=$value;
}

Then reference the key you need:

$key_to_find = 'EXP DATE[p43]';
echo $new_array[$key_to_find];

1 Comment

Thanks James. I have already accepted an answer but thanks for the alternative suggestion! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.