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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:32:35+00:00 2026-06-02T08:32:35+00:00

having some issues with getting the output from an array. I’ve looked around and

  • 0

having some issues with getting the output from an array. I’ve looked around and can’t seem to find out why my array isn’t assigning an index to each portion of an array. I’ve looked around and even tried adding a counter variable to the piece of the array but that doesn’t seem to create an index. I’m pretty new to PHP so any help that anyone can give me would be appreciated.

foreach($dbh->query('SELECT n_productID, t_productName, t_categoryName FROM v_prodcatintersect') as $row) {
    $prodID=$row["n_productID"]; 
    $prodName=$row["t_productName"]; 
    $prodCategor=$row["t_categoryName"];
    $products=array(
        array(
            "prodID" => $prodID,
            "prodName" => $prodName,
            "prodCategor" => $prodCategor
        ),
    print_r($products);
}
for($i = 0, $size = sizeof($products); $i < $size; ++$i){
    echo "The product ID is ".$products[$i]["prodID"];
    echo " The product name is ".$products[$i]["prodName"];
    echo " Product Category ".$products[$i]["prodCategor"];
}

Right now the output from the array (from the print_r) is “Array ( [0] => Array ( [prodID] => 1 [prodName] => iPhone 4 [prodCategor] => Smartphones ) ) Array ( [0] => Array ( [prodID] => 2 [prodName] => Droid 3 [prodCategor] => Smartphones ) )”. And the echo’s only print the second item out of two since they both have the same index. Appreciate any help with creating an index here. Thanks.

  • 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-02T08:32:37+00:00Added an answer on June 2, 2026 at 8:32 am

    To add a new item to an existing array, you can use the following syntax:

    $products[] = array(
                    'prodID'   => $prodID,
                    'prodName' => $prodName,
                    'prodCategor' => $prodCategor);
    

    This will append a new entry into the end of an existing array. So, $products at the end of the foreach loop will give you the following instead of just overwriting the contents of $products. This allows you to increment through $products in your second for loop correctly.

    array(
      [0] => array(
               [prodID] => 1
               [prodName] => iPhone
               [prodCategor] => Smartphones)
      [1] => array(
               [prodID] => 2
               [prodName] => Droid
               [prodCategor] => Smartphones))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
Deploying a Rails3 app, and am having some issues getting rake to find the
We are having some issues getting SharePoint to work as we want it to.
I am using Fluent NHibernate and having some issues getting a many to many
I'm having some issues with getting cookies to work when using a ProxyPass to
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
Originally, I was having some issues getting this code to function, but after a
I'm having some issues getting phpdoc to run correctly. The docs are being generated
I'm having some issues filling a list in Python. I keep getting a list

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.