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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:39:56+00:00 2026-05-29T04:39:56+00:00

This array_merge doesn’t seem to work for me. I am trying merge all the

  • 0

This array_merge doesn’t seem to work for me. I am trying merge all the arrays in one array like this:

foreach ($collection as $result) {
          $i++;
          if(empty($json)){
            $json = $result->getData();                                
          }else{
            $json = array_merge($json, $result->getData());                
          }
      }
      print_r($json);

I have 3 arrays in the collection. But when I do print_r($json); it only shows me the last array like this.

Array ( 
    [po_id]           => 3 
    [title]           => Test3 
    [geo_address]     => M1 2FF 
    [latitude]        => 53.449137 
    [longitude]       => -2.364551 
    [display_address] => testing 
    [url]             => http://testing.com 
    [phone]           => 0321654987 
    [status]          => 1 
    [type]            => 1 
    [created_time]    => 2012-01-26 11:07:05 
    [update_time]     => 2012-01-26 11:10:13 
    [distance]        => 3708.40724665926 
)

I am expecting this to merge all three arrays and print that out.

I’m kinda expecting it like this:

Array ( 
[po_id]           => 1 
[title]           => Test1 
[geo_address]     => M1 2FF
[po_id]           => 2 
[title]           => Test2 
[geo_address]     => M2 2FF  
[po_id]           => 3 
[title]           => Test3 
[geo_address]     => M3 2FF 

)

Means all the arrays should be merged in on array.

EDITTED

I have it working. In fact this what I was looking for:

$json = array();

    foreach ($collection as $key=>$result) {

         $data = $result->getData();

         $json[$key]['postorefinder_id'] = $data['postorefinder_id'];
         $json[$key]['title'] = $data['title'];
         $json[$key]['geo_address'] = $data['geo_address'];
         $json[$key]['latitude'] = $data['latitude'];
         $json[$key]['latitude'] = $data['latitude'];
         $json[$key]['longitude'] = $data['longitude'];
         $json[$key]['display_address'] = $data['display_address'];
         $json[$key]['url'] = $data['url'];
         $json[$key]['phone'] = $data['phone'];
         $json[$key]['status'] = $data['status'];
         $json[$key]['type'] = $data['type'];
         $json[$key]['created_time'] = $data['created_time'];
         $json[$key]['update_time'] = $data['update_time'];
         $json[$key]['distance'] = $data['distance'];
    }
    return json_encode($json);

Thanks @cillosis, your example really helped.

  • 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-29T04:39:57+00:00Added an answer on May 29, 2026 at 4:39 am

    According to the array_merge() function on php.net:

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

    This means, every time you try to merge them, because they are using the same key, it just gets overwritten. That’s why you only see the last array.

    [EDIT]

    So how would I concatenate multiple arrays with the same keys in one array?

    I don’t see how two elements can share the same key unless that key contained another array like this:

    foreach ($collection as $result) {
    
       // Get collection of data
       $data = $result->getData();
    
       // Assign each result to multi-dimensional array
       $json['po_id'][] = $data['po_id'];
       $json['title'][] = $data['title'];
       $json['geo_address'][] = $data['geo_address'];
       $json['latitude'][] = $data['latitude'];
       // ... the rest of them go here ...
    
    }
    

    That is untested, just threw it together real quick. It should output something like:

    Array(
       "po_id": Array(
          "first_po_id",
          "second_po_id",
          "third_po_id" 
       ),
       "title": Array(
          "first_title",
          "second_title",
          "third_title" 
       )
    )
    

    With more data than that of course.

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

Sidebar

Related Questions

This works $arr = array_merge(array_diff($words, array(the,an)); Why doesn't this work? $common consists of 40
I am trying to create a new array from two current arrays. Tried array_merge,
Given an array like this: Array => ( [0] => 1, [1] => 2,
Array starts like this: Array ( [SMART Board] => Array ( [0] => sb1
Trying to sort this array of objects according to (1) depth and (2) weight,
So basically I have these two arrays I want to merge... array(1) { [first]=>
I came across this thread: Is there a function like array_merge in PHP in
I have a dataset obtained from MySQL that goes like this: Array ( [0]
I have the following 2 arrays that I'm trying to merge recursively so that
Consider this array string[] presidents = { Adams, Arthur, Buchanan, Bush, Carter, Cleveland, Clinton,

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.