-1

We saved address as below image in Database :

enter image description here

Here 803110 is zip code of customer, i tried to fetch this zip code and pass it in array as below :

$sqlh="SELECT order_id, address  FROM do_order where order_id='".$order_id."'";
$resulth = $db_handle->runSelectQuery($sqlh);

$address=explode(",",$resulth['address']);
$countadd=count($address);
$pincode=$address[$countadd-1];

$data = 
array (
'OrderNo' => $order_id,
'ZipCode' => $pincode,
);

I got Notice: Undefined index: address & i tried this link before posting question, but that not worked for me....

here is full code in pastebin

  • Are you sure the zip code will always be at last? – Vidhyut Pandya Nov 27 '18 at 05:58
  • @VidhyutPandya Thanks for reply , yes..... –  Nov 27 '18 at 05:58
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Eaten by a Grue Nov 27 '18 at 06:00
  • @billynoah Thanks , i already tried that link, but it did't helped me..... –  Nov 27 '18 at 06:00
  • `$resulth` almost certainly contains an array or object of rows. You are accessing it as if it was a single row. dump the output, get your answer – Eaten by a Grue Nov 27 '18 at 06:01
  • @billynoah sorry, please post your comment as answer...... `$address = explode(",",$resulth[0]['address']);` –  Nov 27 '18 at 06:03
  • it's ok - looks like you've figured it out. normally any query is going to return an array of results but it really depends how `runSelectQuery()` is written and I was only making an educated guess. – Eaten by a Grue Nov 27 '18 at 06:07
  • @billynoah `so need experience guys like you always` , thanks again...... –  Nov 27 '18 at 06:09

1 Answers1

0

You can get the last element of an array using the end() function.

Try this

$address = explode(",",$resulth['address']); 
//or can try explode(",",$resulth[0]['address']) if $resulth is multidimensional array
$pincode = end($address);

Demo

Vidhyut Pandya
  • 1,559
  • 1
  • 13
  • 27