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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:23:56+00:00 2026-06-18T22:23:56+00:00

I have the following result from a MySQL query with two joins. Array (

  • 0

I have the following result from a MySQL query with two joins.

Array ( 
[0] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Firstschool [typ] => 0 [s_id] => 32 [fac] => history) 

[1] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => math)

[2] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => english)
)

The data is redundant at some points, I need it this way:

Array ( 
  [Berlin] => Array ( [lat] => 52.519 
                      [lon] => 13.406  
                      [schools] => Array([0]=> Firstschool [1]=>Secondschool)
  )

  [OtherCity] => Array ( ... )
)

First, is this okay or exists a better solution? =)
Second.. how to split it for the needed result.

I tried it with something like the following code snippet, but it doesn’t work as wished.

foreach($viewmodel as $item) { 
   $data[$item['place']][] = $item['pname'];
}

The result is:

Array ( [Berlin] => Array ( [0] => Firstschool [1] => Firstschool [2] => Firstschool ))

NOT so useful. 😉

I hope its understandable what I need. Maybe someone has an nice idea how to solve this problem.

Thanks for your time.

  • 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-18T22:23:57+00:00Added an answer on June 18, 2026 at 10:23 pm

    I think you are on a right path, just need to fill in a little more detail:

    $cities = Array (
         Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Firstschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history'),
         Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'math'),
         Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'english'),
    );
    
    // gather the transformed array in a new array 
    $out = array();
    foreach ($cities as $city) {
        // the first time we see the place
        if (!isset($out[$city['place']])) {
            // copy over what you want to keep
            $out[$city['place']] = array(
                'lat' => $city['lat'],
                'lon' => $city['lon'],
                'schools' => array($city['pname']),
            );
        } // only add $city['pname'] if we don't have it already
        elseif (!in_array($city['pname'], $out[$city['place']]['schools'])) {
            // we already seen this place, just add to the schools
            $out[$city['place']]['schools'][] = $city['pname'];
        }
    }
    

    For the gather faculties too question, use the school names as keys to arrays in the ‘schools’ key of the top level arrays, populate them like this: (still skipping duplicates):

    foreach ($a as $city) {
        if (!isset($out[$city['place']])) {
            $out[$city['place']] = array(
                'lat' => $city['lat'],
                'lon' => $city['lon'],
                'schools' => array($city['pname'] => array($city['fac'])),
            );
        } else {
            // for convenience and readability, introducing some variables
            $schools = &$out[$city['place']]['schools'];
            $pname = $city['pname'];
            $fac = $city['fac'];
    
            // if we didn't see this school yet, add it with it's faculty
            if (!isset($schools[$pname])) {
                $schools[$pname] = array($fac);
            } // if we did see this school before but the faculty is new, add it under the school's key
            else if (!in_array($fac, $schools[$pname])) { 
                $schools[$pname][] = $fac;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE
I have the following result from geoPlugin http://www.geoplugin.net/json.gp?ip=64.90.182.55 : geoPlugin({ geoplugin_request:64.90.182.55, geoplugin_status:200, geoplugin_city:New York,
I have the following table >anna2 name from to result 11 66607 66841 5
I have following select results: Select t1.ID, t1.Value1 from t1 where t1.ID=1 Result: ID
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
I have the following PHP code which runs a MySQL query and returns the
I have two tables in MySQL that I'm comparing with the following attributes: tbl_fac
I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
I have the following two mysql queries and I am trying to combine them
I have a following structered DOM result, getting after a POST event: <root> <config>

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.