FlyingCat
FlyingCat

Reputation: 14270

php DB returned array issue

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!

Upvotes: 1

Views: 39

Answers (1)

Ray Paseur
Ray Paseur

Reputation: 2194

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

Upvotes: 2

Related Questions