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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:03:35+00:00 2026-05-29T06:03:35+00:00

I have a bunch of php arrays that look like this (please don’t ask

  • 0

I have a bunch of php arrays that look like this (please don’t ask why, I’m just doing my job…All I will say is EAV…):

$firstNames = ([accountId] => 100, [firstName] => 'John'
               [accountId] => 101, [firstName] => 'Fred');


$lastNames =  ([accountId] => 100, [lastName] => 'Doe'
               [accountId] => 101, [lastName] => 'Bloggs');


$city      =  ([accountId] => 100, [city] => 'New York'
               [accountId] => 101, [city] => 'Cambridge');


$country   =  ([accountId] => 100, [country] => 'USA'
               [accountId] => 101, [country] => 'UK');

etc etc.

I have to combine them into one array:

$userDetails =  ([accountId] => 100, [firstName] => "John", [lastName] => "Doe", 
                 [city] => "New York", [country] => "USA");

My feeling is the correct answer would be to break these attributes out of EAV and model them correctly. But I can’t. It would also be possible to do self-join upon self-join in the db, but I have simplified the example and this is not really possible – and I’ve been told to do it this way…There could be a bunch of additional fields tacked on as well, later.

So what is the best way to produce one associative array, merging on accountId in PHP? Is there a function, or do I need to loop round and round etc.

  • 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-29T06:03:36+00:00Added an answer on May 29, 2026 at 6:03 am

    This nested foreach should do it:

    $result = array();
    
    foreach (array('firstNames' => 'firstName', 'lastNames' => 'lastName', 'city' => 'city', 'country' => 'country') as $srcArr => $arrKey) {
      foreach ($$srcArr as $item) {
        if (!isset($result[$item['accountId']])) {
          $result[$item['accountId']] = $item;
        } else {
          $result[$item['accountId']][$arrKey] = $item[$arrKey];
        }
      }
    }
    
    var_dump($result);
    

    See it working

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

Sidebar

Related Questions

Let's say that I have a bunch of records in MySQL, something like this:
I have a php array that has a bunch of data that I need
I have a bunch of PHP code that is auto-generated and it puts all
I have an application that is mainly a bunch of PHP files included in
I have a php page that takes in a bunch of url parameters and
I have bunch of lists like this: out[3] [[3]] [1] forum=28&mid=11883 [2] forum=29&mid=11884 out[4]
I have a bunch of name-parentname pairs, that I'd like to turn into as
I have this code in cake php but it generates a bunch of labels
I have a PHP script (news-generator.php) which, when I include it, grabs a bunch
I have a real basic form (code below) with a bunch of back-panel PhP.

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.