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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:57+00:00 2026-05-27T04:09:57+00:00

I am new to PHP and am looking at efficient ways to return data

  • 0

I am new to PHP and am looking at efficient ways to return data from the database. Lets say I have a UserProfile table that has a one to many relationship with UserInterest and UserContact:

Select p.Id, p.FirstName, p.LastName, i.Name as Interests, c.Email, c.Phone
from UserProfile p
left join UserInterest i on p.Id = i.UserProfileId
left join UserContact c on p.Id = c.UserProfileId
where p.Id = 1

An efficient way to retrieve data would be to create a multidimensional array such as:

$user = array(  "FirstName" => "John", 
                "LastName" => "Doe", 
                "Gender" => "Male", 
                "Interests" => array(
                    "Hiking", 
                    "Cooking"), 
                "Contact" => array(
                    "Email" => "john.doe@gmail.com", 
                    "Phone" => "(555) 555-5555"));

I can’t seem to get my head around how this would be constructed in PHP. For simple data like interests I could use group_concat(i.Name) as Interests in the query to return interests back as a comma separated list in a single row, however, for an associative array such as Contact, I’d like to be able to get a handle on each key in the array using $user[‘Contact’][‘Email’].

From a “Best Practices” standpoint, I would assume that constructing an array like this in one query is a lot better than hitting the database multiple times to retrieve this data.

Thanks!

Neil

  • 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-27T04:09:58+00:00Added an answer on May 27, 2026 at 4:09 am

    You can construct this array in one pass through the data returned by your query. In pseudo-code:

    for each row
         $user["FirstName"] = row->FirstName;
         $user["LastName"] = row->LastName;
         $user["Interests"][] = row->Interests;
         $user["Contact"]["Email"] = row->Email;
         $user["Contact"]["Phone"] = row->Phone;
    next
    

    The syntax $user["Interests"][] = $data is valid PHP code. It is equivalent to array_push($user["Interests"], $data).

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

Sidebar

Related Questions

We are in the startup build phase of a new PHP webapp that will
I am pretty new to php, but I am learning! I have a simple
I'm new to PHP and have installed on Linux to boot (also a newbie).
I'd like to write a new website from scratch. I'll be using PHP for
On my Apache server I have a PHP service that gets requests and generates
First I would like to say that I am entirely new to Magento so
I'm relatively new to PHP and have been writing a project using what I
I am new to PHP, so I am looking for some input on how
This is probably very easy but im also very new to php. Im looking
im looking for a way to add some docblocks to a new .php file,

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.