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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:13:28+00:00 2026-06-13T16:13:28+00:00

I am generating an array from a while loop, and I would like to

  • 0

I am generating an array from a while loop, and I would like to eventually use this array to display data.

while($row = $database->fetch(PDO::FETCH_ASSOC)
{

     $value_1 = $row['value_1'];
     $value_2 = $row['value_2'];

     $data[] = array("value_1"=>$value_1,"value_2"=>$value_2);

}

so the $data[] would display using print_r I get something like this:

Array
(
    [0] => Array
        (
            [value_1] => Hello World
            [value_2] => venus
        )

    [1] => Array
        (
            [value_1] => Hello World
            [value_2] => pluto
        )

    [2] => Array
        (
            [value_1] => Hello Moon
            [value_2] => Halloween
        )

)

My question is, how would I do a foreach loop or display such a way where if I wanted to get all data, but consolidate the same values?

Ex:

Hello World to venus pluto
Hello Moon to Halloween

I know those sentences doesn’t make sense, but as you can see Hello World would be consolidated and I would need to add an ” to” in between value_1 and value_2

Hope my question is understandable.

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-13T16:13:30+00:00Added an answer on June 13, 2026 at 4:13 pm

    Try something like this. (Not Tested)

    $match = array();
    foreach($data as $d) {
        if (isset($match[$d['value_1']])) {
            $match[$d['value_1']] = $match[$d['value_1']].' '.$d['value_2'] ;
        } else {
            $match[$d['value_1']] = $d['value_1']. ' to '. $d['value_2'];
        }
    }
    
    print_r($match);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this simple PHP loop that is generating html table data with
This question came into my mind while generating sample data for a SO-answer. I
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
I am generating a byte array of size about 250MB. This is then being
When generating VS2010 targets with CMake, I would like the /LTCG flag turned on
I have the following code (taken from actual script): $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $auth_token=$row['oauth_token']; $auth_token_secret=$row['oauth_token_secret'];
I am trying to dynamically fetch the data from a PhP module, load it
I have this code which echoes me information from the database: <?php include('db_connect.php'); mysql_query(SET
Generating normal columnar data in excel file is quite easy but does any one
While generating an XML content, I get an empty node <node/> , and I

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.