2013-01-17 105 views
3

我有一個數組AllUsers作爲PHP多維數組操作

Array AllUsers 
(
    [0] => Array 
     (
      [0] => Array 
        (
         [0] => Tim 
         [1] => [email protected] 
       ) 
      [1] => Array 
        (
         [0] => John 
         [1] => [email protected] 
       ) 
     ) 
    [1] => Array 
     (
      [0] => Array 
        (
         [0] => Mike 
         [1] => [email protected] 
       ) 
      [1] => Array 
        (
         [0] => Aron 
         [1] => [email protected] 
       ) 
     ) 
) 

我有另一個數組FilteredUsers作爲

Array FilteredUsers 
(
    [0] => Array 
     (
      [0] => John 
      [1] => [email protected] 
     ) 
    [1] => Array 
     (
      [0] => Mike 
      [1] => [email protected] 
     ) 
    [2] => Array 
     (
      [0] => Mike 
      [1] => [email protected] 
     ) 
) 

現在我想是的FilteredUsers[]每個元素添加AllUsers[]這樣 -

  1. FilteredUsers[0]應該得到廣告DED到批量AllUsers[1]因爲AllUsers[0]已經在它的元素名稱約翰陣列
  2. 同樣FilteredUsers[1]應該被添加到批量AllUsers[0]
  3. 任何(如AllUsers[0]AllUsers[1])不能有超過3個元素。如果全部批次已滿,則將創建新批次,但FilteredUsers[]中的每個元素都應該包含在某些批次中。

所以更新AllUsers陣列應該是這樣的 -

Array AllUsers 
(
    [0] => Array 
     (
      [0] => Array 
        (
         [0] => Tim 
         [1] => [email protected] 
       ) 
      [1] => Array 
        (
         [0] => John 
         [1] => [email protected] 
       ) 
      [2] => Array 
        (
         [0] => Mike 
         [1] => [email protected] 
       ) 
     ) 
    [1] => Array 
     (
      [0] => Array 
        (
         [0] => Mike 
         [1] => [email protected] 
       ) 
      [1] => Array 
        (
         [0] => Aron 
         [1] => [email protected] 
       ) 
      [2] => Array 
        (
         [0] => John 
         [1] => [email protected] 
       ) 
     ) 
    [2] => Array 
     (
      [0] => Array 
        (
         [0] => Mike 
         [1] => [email protected] 
       ) 
     ) 
) 
+1

只是好奇心,爲什麼你需要3批?你可以輕鬆地做一個'for $($ i = 0; $ i <= count($ AllUsers),$ i + = 3){} –

回答

2

這裏是工作代碼:

我在創建代碼引擎收錄還爲您提供:http://codepad.org/iyZUpYxc

<?php 

//PHP Multidimensional Array Manipulation 

$allUsers = array(); 
$allUsers[] = array(array('name'=>'Tim', 'email'=>'[email protected]'), array('name'=>'John','email'=>'[email protected]')); 
$allUsers[] = array(array('name'=>'Mike', 'email'=>'[email protected]'), array('name'=>'Aron','email'=>'[email protected]')); 

$filteredUsers = array(); 
$filteredUsers[] = array('name'=>'John', 'email'=>'[email protected]'); 
$filteredUsers[] = array('name'=>'Mike', 'email'=>'[email protected]'); 
$filteredUsers[] = array('name'=>'Mike', 'email'=>'[email protected]'); 

//RULE: one batch cannot have duplicate user names 

//print_r($allUsers); 
//print_r($filteredUsers); 

foreach ($filteredUsers as $filteredUser) { 
    //$filteredUser is a single dimensional arrray. 

    $filteredUserAdded = 0; 
    foreach ($allUsers as &$allUser) { 
    //$allUser is a muldimentional array. 

    //Check whether the current batch contains $filteredUser['name'] value 
    $intersect = array_uintersect(array($filteredUser), $allUser, 'compareName'); 
    if (empty($intersect) && count($allUser) < 3) { 
     //We can add filtereduser here 
     $allUser[] = $filteredUser; 
     $filteredUserAdded = 1; 
    } 

    } // end foreach $allUsers 

    if ($filteredUserAdded == 0) { 
     //This filtered user was not added in any existing batch. 
     //Hence add it in a new batch 
     $allUsers[] = array($filteredUser);  
    } 
} // end foreach $filteredUsers 


//function to compare the 'name' column value of two arrays 
function compareName($array1, $array2) 
{ 
    return strcmp($array1['name'], $array2['name']); 
} 

//Note: http://in1.php.net/array_uintersect is the key used here for comparison 

print_r($allUsers); 
print_r($filteredUsers); 

?> 

注意:您錯過了陣列 ( [0] =>約翰 [1] => [email protected] )。

但我的代碼輸出也正確。

謝謝

+0

@Sachyn請讓我知道這段代碼的成功:) – OMG

+0

OMG it作品..非常感謝:) – skos

+0

感謝@Sachyn的投票 – OMG

1

挑戰接受!!!

##### BORROWED FROM OMG 
//PHP Multidimensional Array Manipulation 

$allUsers = array(); 
$allUsers[] = array(array('name'=>'Tim', 'email'=>'[email protected]'), array('name'=>'John','email'=>'[email protected]')); 
$allUsers[] = array(array('name'=>'Mike', 'email'=>'[email protected]'), array('name'=>'Aron','email'=>'[email protected]')); 

$filteredUsers = array(); 
$filteredUsers[] = array('name'=>'John', 'email'=>'[email protected]'); 
$filteredUsers[] = array('name'=>'Mike', 'email'=>'[email protected]'); 
$filteredUsers[] = array('name'=>'Mike', 'email'=>'[email protected]'); 

# using PHP's unique continue statement!  

# loop through filtered users 
foreach($filteredUsers as $fUser) { 
    #loop through all users batches 
    foreach($allUsers as $key=>$aUsers) { 
     # is it full? 
     if(isset($aUsers[2]))continue; 
     # any user with same name? 
     foreach($aUsers as $aUser) 
      if($aUser['name']==$fUser['name'])continue 2; 
     # push in current batch 
     $allUsers[$key][]=$fUser; 
     continue 2; 
    } 
    # new batch needed 
    $allUsers[]=$fUser; 
} 
var_dump($allUsers); 

如果你不知道,continue接受一個「參數」表示控制流的數量到氣泡爲止。

而且可以找到工作代碼here