1

I am trying to combine 2 db results together to have an array like the following:

array(
    0=>array( 
      'name'=>'John',
      'title'=>'manager',
      'email'=>'test',
      'permission'=>'1',
      'depart'=>'human resource'
      ),
    1=>array(
      'name'=>'Ted',
      'title'=>'employee',
      'email'=>'test2',
      'permission'=>'2',
      'depart'=>'human resource'
   )
)

The first 3 elements are from 1 returned DB result and the last two elements are from another DB results.

     $firstResults = DBCall::call(getName);  //get name,title,email or John and Ted as an array
     $secondResults = DBCall::call(getPermission);  //get permission and depart as an array

     //I then use array merge
     $userResults=array_merge($firstResults ,$secondResults);

     //but it will become

array(
    0=>array( 
      'name'=>'John',
      'title'=>'manager',
      'email'=>'test'

      ),
    1=>array(
      'name'=>'Ted',
      'title'=>'employee',
      'email'=>'test2'

   ),
    2=>array(
      'permission'=>'1',
      'depart'=>'human resource'
   ),
    3=>array(
      'permission'=>'2',
      'depart'=>'human resource'   
   )
)

Are there anyways to archieve the outcome I need? Thanks a lot!

4
  • Maybe you can get the combined results from your SQL statement instead. Commented Dec 28, 2012 at 20:38
  • I think the most appropriate approach is to create a new DBCall::call(getNamePermission) that will use a new query. Commented Dec 28, 2012 at 20:39
  • unfortunately, I won't be able to change the db call. I have to use the current db results. Commented Dec 28, 2012 at 20:41
  • I think the only way is to actually define a new array. I had some problems with that some time ago and the only way to fixit was creating a new array using a loop. Commented Dec 28, 2012 at 20:41

1 Answer 1

2

This seems to test out OK. http://www.laprbass.com/RAY_temp_flyingcat.php

<?php // RAY_temp_flyingcat.php
error_reporting(E_ALL);
echo "<pre>";

// SIMULATE DB RESULTS SETS
$getname = array(
    0=>array(
      'name'=>'John',
      'title'=>'manager',
      'email'=>'test',
      ),
    1=>array(
      'name'=>'Ted',
      'title'=>'employee',
      'email'=>'test2',
   )
)
;

$getperm = array(
    0=>array(
      'permission'=>'1',
      'depart'=>'human resource'
      ),
    1=>array(
      'permission'=>'2',
      'depart'=>'human resource'
   )
)
;

// MERGE THE ARRAYS SENSIBLY
foreach ($getname as $key => $arr)
{
    $getboth[$key] = array_merge($getname[$key], $getperm[$key]);
}

// SHOW THE WORK PRODUCT
print_r($getname);
print_r($getperm);
print_r($getboth);

Best regards, ~Ray

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.