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 6776379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:17+00:00 2026-05-26T16:01:17+00:00

I have a large array. In this array I have got (among many other

  • 0

I have a large array.

In this array I have got (among many other things) a list of products:

$data['product_name_0'] = '';
$data['product_desc_0'] = '';
$data['product_name_1'] = '';
$data['product_desc_1'] = '';

This array is provided by a third party (so I have no control over this).

It is not known how many products there will be in the array.

What would be a clean way to loop though all the products?

I don’t want to use a foreach loop since it will also go through all the other items in the (large) array.

I cannot use a for loop cause I don’t know (yet) how many products the array contains.

I can do a while loop:

$i = 0;
while(true) { // doing this feels wrong, although it WILL end at some time (if there are no other products)
    if (!array_key_exists('product_name_'.$i, $data)) {
        break;
    }

    // do stuff with the current product

    $i++;
}

Is there a cleaner way of doing the above?

Doing a while(true) looks stupid to me or is there nothing wrong with this approach.

Or perhaps there is another approach?

  • 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-05-26T16:01:18+00:00Added an answer on May 26, 2026 at 4:01 pm

    Your method works, as long as the numeric portions are guaranteed to be sequential. If there’s gaps, it’ll miss anything that comes after the first gap.

    You could use something like:

    $names = preg_grep('/^product_name_\d+$/', array_keys($data));
    

    which’ll return all of the ‘name’ keys from your array. You’d extract the digit portion from the key name, and then can use that to refer to the ‘desc’ section as well.

    foreach($names as $name_field) {
       $id = substr($names, 12);
       $name_val = $data["product_name_{$id}"];
       $desc_val = $data["product_desc_{$id}"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large array of vehicle make and model data that I want
I've got a very large array of data that I'm writing to a range.
I have a large array in this format: var cars = [{brand:Honda,year:2002},{brand:Toyota,year:2000},{brand:Subaru,year:2009}]; The array
I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse
I have a large array in C (not C++ if that makes a difference).
I have a large array of vertices, some of them are edges, some are
Imagine you have a large array of floating point numbers, of all kinds of
I have a large 2d array that I serialize and base64_encode and throw into
I have a large int[] array and a much smaller int[] array. I want
So I have a large 2d array that i serialize, but when I attempt

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.