0

I am creating APP for Shopify and I need the last fulfillment id of an order.

This is my code

if($value['fulfillment_status'] == 'fulfilled') {
 foreach($value['fulfillments'] as $id) {
  echo $id['id'].'<br>';
 }
}

The output is

3549057974422
3555100033174
3557460050070
3560109277334
3560268103830
3560845475990
3561173024918

I only want to save or get the last fulfillment id value which is

3561173024918

How to do this ?

Regards

Hannah James
  • 540
  • 3
  • 14
  • 2
    Does this answer your question? [What's the best way to get the last element of an array without deleting it?](https://stackoverflow.com/questions/3687358/whats-the-best-way-to-get-the-last-element-of-an-array-without-deleting-it) – DarkBee Aug 05 '21 at 10:18

4 Answers4

3

The end function returns the last value of an array and sets the pointer to the last value.

Based on your output, end($value['fulfillments']) would return 3561173024918 as required.

1

you can use this PHP function : end($value['fulfillment_status']) which will give you the last element in the array, your code will be like this :

if($value['fulfillment_status'] == 'fulfilled') {
    echo end($value['fulfillment_status']);`
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
soma
  • 176
  • 9
1

PHP has a built-in function for that

array_key_last($array);

https://www.php.net/manual/en/function.array-key-last.php

svin83
  • 239
  • 1
  • 4
  • 13
1

You can use end().

echo end($value['fulfillments'])["id"];