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

  • Home
  • SEARCH
  • 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 6174123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:41:55+00:00 2026-05-23T23:41:55+00:00

I’ve got a data schema like this: cp id te su 1 7 2

  • 0

I’ve got a data schema like this:

cp
    id    te    su
    1     7     2
    2     7     1
    3     6     8

cl
    id    cp    st
    1     2     5
    2     2     6
    3     1     6

us
   id    na
   ..    ..
   5     Joe
   6     Mike
   7     Tina
   ..    ..

I want to run a php function foo($te) with foo(7) to output data like this from mySQL

[0]
    su: 1
    st_list:
        [0]
            id:6
            na:Mike
[1]
    su: 2
    st_list:
        [0]
            id:5
            na:Joe
        [1]
            id:6
            na:Mike

I want to know how best to do this. Right now I’m able to use JOINs, but the result looks like this

[0]
    su: 1
    st_id:6
    st_na:Mike
[1]
    su: 2
    st_id:5
    st_na:Joe
[3]
    su: 2
    id:6
    na:Mike

The su:2 is repeated… obviously that isn’t a problem in this example, the problem is that in the real thing that su represents a lot more data, that i’ll be getting through “this” (whatever answer I choose) method.

EDIT:
I hope you understand what I’m asking… I know a lot of you guys are way more knowledgable in mySQL and development in general than me, so that’s why i’m asking in such a plain (I HAVE THS—-> WANT THIS) way, because I think if I asked any more specifically I would end up making assumptions about the way it should run). I want an effecient solution, because this data is being used to populate search results.

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-05-23T23:41:56+00:00Added an answer on May 23, 2026 at 11:41 pm

    You will need to loop over the results yourself and build the array in the format you want. MySQL can’t give you the results in the nested format you want. Here’s a rough draft (untested):

    $su = 0;
    $st_list = array();
    $nested = array();
    foreach ($results as $row) {
        if ($su != 0 && $row['su'] != $su) {
            $nested[] = compact('su', 'st_list');
        }
    
        if ($su != $row['su']) {
            $su = $row['su'];
            $st_list = array();
        }
    
        $st_list[] = array(
            'id' => $row['st_id'],
            'na' => $row['st_na'],
        );
    }
    
    $nested[] = compact('su', 'st_list');
    

    Edit: Usually it’s best to simply fetch all the results you want using a single SQL query. That is almost always the most efficient way to do it. But if it really matters to you (and this is a performance critical part of your application), the only way to know for sure is benchmark both methods. A lot depends on your database layout, number of rows, SQL indexes, etcetera.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.