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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:06:28+00:00 2026-05-20T02:06:28+00:00

I have a database table that contains the following: id user_id plant_id date value

  • 0

I have a database table that contains the following:

id  user_id     plant_id        date            value
68  68          109             2011, 04, 02    300
67  68          109             2011, 02, 16    300
66  68          109             2011, 06, 11    120
65  68          109             2011, 02, 04    120
64  68          109             2010, 12, 19    55
63  68          109             2011, 01, 22    456

62  68          108             2011, 01, 22    888
61  68          108             2011, 01, 15    123

I need to create an array for user_id 68 that is keyed to plant_id and looks like this:

Array
(
    [109] => Array
        (
            [2011, 04, 02] => 300
            [2011, 02, 16] => 300
            [2011, 06, 11] => 120
            [2011, 02, 04] => 120
            [2010, 12, 19] => 55
            [2011, 01, 22] => 456

        )

    [108] => Array
        (
            [2011, 01, 22] => 888
            [2011, 01, 15] => 123
        )

)

Unfortunately I am screwing it up with the following code:

$result = mysql_query("SELECT * FROM data WHERE user_id='$user_id'");

$value_array = array();
while ($row = mysql_fetch_array($result)) {
    $value_array[$row['date']] = $row['value'];
}

$result = mysql_query("SELECT * FROM data WHERE user_id='$user_id'");

$plant_array = array();
while ($row = mysql_fetch_array($result)) {
    $plant_array[$row['plant_id']] = $value_array;
}

which gives me the same arrays for each plant_id:

Array
(
    [109] => Array
        (
            [2011, 04, 02] => 300
            [2011, 02, 16] => 300
            [2011, 06, 11] => 120
            [2011, 02, 04] => 120
            [2010, 12, 19] => 55
            [2011, 01, 22] => 888 // skips value '456' because of repeated date?
            [2011, 01, 15] => 123
        )

    [108] => Array
        (
            [2011, 04, 02] => 300
            [2011, 02, 16] => 300
            [2011, 06, 11] => 120
            [2011, 02, 04] => 120
            [2010, 12, 19] => 55
            [2011, 01, 22] => 888 // skips value '456' because of repeated date?
            [2011, 01, 15] => 123
        )

)

How do I create the array loops keyed to plant_id, containing data only from that specific plant_id?

Any help is greatly appreciated!

  • 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-20T02:06:29+00:00Added an answer on May 20, 2026 at 2:06 am
    $result = mysql_query("SELECT * FROM data WHERE user_id='$user_id'");
    
    $output_array = array();
    while ($row = mysql_fetch_array($result)) {
        if(!isset($output_array[$row['plant_id']]) || !is_array($output_array[$row['plant_id']])){
            $output_array[$row['plant_id']] = array();
        }
    
        $output_array[$row['plant_id']][$row['date']] = $row['value'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I have a database that contains a table that looks a bit like this:
I have two simple tables in my database. A card table that contains Id,
I'm trying to normalize a mysql database.... I currently have a table that contains
I have a table in a sql server 2008 database that contains bunch of
I have a database structure that has a Person table which contains fields such
I have the following table in my database: user_id | p1 | p2 |
I have a table that contains the following datetime and customer example : data1
I have a Billing table in my database that contains daily accruals of amounts
I have a database that contains a table for storing URL Images (since storing

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.