1

我正在尝试将 2 个 db 结果组合在一起以获得如下所示的数组:

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'
   )
)

前 3 个元素来自 1 个返回的 DB 结果,后两个元素来自另一个 DB 结果。

     $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'   
   )
)

有什么办法可以归档我需要的结果吗?非常感谢!

4

1 回答 1

2

这似乎测试正常。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);

最好的问候,~雷

于 2012-12-28T20:45:31.377 回答