skip to Main Content

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

4

Answers


  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']);`
    }
    
    Login or Signup to reply.
  2. PHP has a built-in function for that

    array_key_last($array);
    

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

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  4. You can use end().

    echo end($value['fulfillments'])["id"];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search