Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:08:33+00:00 2026-06-07T07:08:33+00:00

I have a script that is looping through an array of orders with an

  • 0

I have a script that is looping through an array of orders with an inner loop going through all the items that were in that particular order. At the end of the script I need to concatenate a string with all the item data with an unique identifier for each. My problem arises when I get past the first order.

We will assume this loop will iterate through 3 orders. Here is my example:

foreach ($_orders as $order)
{
$oid = $order['id'];
$i = 1;
foreach ($order['items'] as $item)
         {
            $cjItemStr .= '&ITEM'. $i . '=' . $item['sku'] . '&AMT' . $i . '=' . $item['price'] . '&QTY' . $i . '=' . $item['qty'];
            $i++;
         }
}

This would correctly output what I need for 1 order. The string would read:

&ITEM1=TT-5555&AMT1=5.00&QTY1=2&ITEM2=TT-3333&AMT2=10.00&QTY2=1&ITEM3=TT-2222&AMT3=15.00&QTY3=1

This works great for one order but once I move onto the next order I need the incrementer to continue from where the other one left off. It would need to move onto ITEM4,AMT4,QTY4,ITEM5,AMT5,QTY5 etc. As it stands now it just goes back to 1. Anyone have any idea on how I could fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:08:34+00:00Added an answer on June 7, 2026 at 7:08 am

    Just move your initialization of $i outside the outer loop.

    // Initialize $i outside the first loop:
    $i = 1;
    foreach ($_orders as $order)
    {
      $oid = $order['id'];
      foreach ($order['items'] as $item)
      {
         $cjItemStr .= '&ITEM'. $i . '=' . $item['sku'] . '&AMT' . $i . '=' . $item['price'] . '&QTY' . $i . '=' . $item['qty'];
         // And increment $i for each order item
         $i++;
      }
    }
    // Assuming count($_orders) == 3 and each has count($order['items'] == 3)
    // $i is now 10 after all loops complete.
    

    However, if you need the value of $i to persist beyond each run of this script, you’ll need to store it somewhere, such as writing it to a text file or sticking it into a database. As it is now, any time you run this script, you start back at $i = 0

    Finally, I’m not sure how Magento handles array elements, but if this query string is to be parsed by PHP, you can format it such that each element is an array. Then when received, access as $_GET['ITEM'][] for example.

     // Surrounding each with [] will allow PHP to process them as arrays in the script that receives this
     $cjItemStr .= '&ITEM['. $i . ']=' . $item['sku'] . '&AMT[' . $i . ']=' . $item['price'] . '&QTY[' . $i . ']=' . $item['qty'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that recursively loops through all the sub directories and compresses
I have a phpmailer that attaches files by looping through an array of files
I have following script that executes all the .reg files in the current directory.
I have a script that uses a ton of redirects. About half way through
I have a PHP script now looping through combinations of a set of arrays.
I have a script that scans all my servers in my domains and outputs
So I have this script that parses through a file and inserts data into
I have script that reads remote file content and writes it to local server.
I have this script that run to fix my menu bar to the browser
I have a script that submits data via an ajax POST. It is called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.