2014-10-30 138 views
0

我需要重新排列陣列1中陣列2中[id](oper - %%%%%%%%%%)的顯示順序,而且我不太確定如何解決這個問題。關聯陣列

陣列1

Array ([0] => Array ([id] => oper-4c597644-402490ee [amount] => 17498.5) [1] => Array ([id] => oper-4f30019a-27f27473 [amount] => 10383.5) [2] => Array ([id] => oper-4bceffd1-21e0af5b [amount] => 6277) [3] => Array ([id] => oper-4f300d33-de9592e3 [amount] => 11382) [4] => Array ([id] => oper-4c236420-0b11e945 [amount] => 14759) [5] => Array ([id] => oper-50f6e4ad-9effbec7 [amount] => 3058) [6] => Array ([id] => oper-4f05a90b-03b379f9 [amount] => 12112.5) [7] => Array ([id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023) [8] => Array ([id] => oper-52657816-3d6516e2 [amount] => 3495)) 

陣列2

Array ([0] => Array ([0] => Bob [1] => oper-4bceffd1-21e0af5b) [1] => Array ([0] => Bob [1] => oper-4c236420-0b11e945) [2] => Array ([0] => Bob [1] => oper-4c597644-402490ee) [3] => Array ([0] => Bob [1] => oper-4f05a90b-03b379f9) [4] => Array ([0] => Bob [1] => oper-4f30019a-27f27473) [5] => Array ([0] => Bob [1] => oper-4f300d33-de9592e3) [6] => Array ([0] => Bob [1] => oper-50f6e4ad-9effbec7) [7] => Array ([0] => Bob [1] => oper-52657816-3d6516e2) [8] => Array ([0] => Bob [1] => oper-qtgjvw8y-1uqtw058) [9] => Array ([0] => Empty [1] =>) [10] => Array ([0] => Upg. [1] =>) [11] => Array ([0] => Ren. [1] =>)) 

鮑勃只是一個例子「名」 這是我到目前爲止的代碼(我知道這是不是乾淨,因爲這ISN」我只是試圖讓這個工作在這一刻):

$result = mysql_query("SELECT * FROM tblOperatorGoals WHERE MonthlyGoal LIKE '$currentDate%'"); 

while ($row = mysql_fetch_array($result)) { 
    $opernameArray[] = $row['OperatorName']; 
    $operIDArray[] = $row['OperatorID']; 
    $monthlyGoal[] = substr($row['MonthlyGoal'], 8); 
    $operArray[] = array('name' => $row['OperatorName'], 'id' => $row['OperatorID']); 
} 

$result = mysql_query("SELECT * FROM tblUserPayments WHERE ChargeAmount IS NOT NULL AND PaymentStatus='OK' AND PaymentDate LIKE '$currentDate%'"); 
while ($row = mysql_fetch_array($result)) { 
    $operearnedArray[] = array(
     'amount' => $row['ChargeAmount'], 
     'id' => $row['OperatorID']); 
} 

foreach ($operearnedArray as $value) { 
    if($value['id'] == '' || $value['id'] == null) { 
    continue; 
    } 
    if(array_key_exists($value['id'], $operSums1)) { 
    $operSums1[$value['id']] += $value['amount']; 
    } else { 
    $operSums1[$value['id']] = $value['amount']; 
    } 
} 

foreach ($operSums1 as $id => $value) { 
    if (in_array($id,$operIDArray)) { 
    $operSums[] = array(
    'id' => $id, 
    'amount' => $value); 
    } 
} 

任何幫助是極大的ppreciated。關聯數組只是不是我的一杯茶。

基本上我正在尋找,看起來像這樣的數組:

Array ([name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$)...ect...ect... 

回答

0

原油,但應該工作:

$toSort = array(
    array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,), 
    array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,), 
    array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,), 
    array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,), 
    array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,), 
    array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,), 
    array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,), 
    array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,), 
    array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,), 
); 

$assigned = array (
    array ('Bob','oper-4bceffd1-21e0af5b'), 
    array ('Bob','oper-4c236420-0b11e945'), 
    array ('Bob','oper-4c597644-402490ee'), 
    array ('Bob','oper-4f05a90b-03b379f9'), 
    array ('Bob','oper-4f30019a-27f27473'), 
    array ('Bob','oper-4f300d33-de9592e3'), 
    array ('Bob','oper-50f6e4ad-9effbec7'), 
    array ('Bob','oper-52657816-3d6516e2'), 
    array ('Bob','oper-qtgjvw8y-1uqtw058'), 
); 

$sorted = assignData($toSort, $assigned); 

var_dump($sorted); 

function assignData($raw, $order) 
{ 
    $returnArray = array(); 
    foreach($order as $entry): 
     foreach($raw as $rawEntry): 
      if ($rawEntry['id'] == $entry[1]): 
       $temp = array(
        'name' => $entry[0], 
        'oper' => $rawEntry['id'], 
        'amount' => $rawEntry['amount'], 
       ); 
       $returnArray[] = $temp; 
      endif; 
     endforeach; 
    endforeach; 
    return $returnArray; 
} 
+0

我已經編輯我的職務,我仍然無法使其正常工作。 – Juan 2014-10-31 14:25:20

0

試試這個。

<?php 

$data = [ 
    ['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5], 
    ['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5], 
    ['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277], 
    ['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382], 
    ['id' => 'oper-4c236420-0b11e945', 'amount' => 14759], 
    ['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058], 
    ['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5], 
    ['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023], 
    ['id' => 'oper-52657816-3d6516e2', 'amount' => 3495] 
]; 

$names = [ 
    ['BobA', 'oper-4bceffd1-21e0af5b'], 
    ['BobB', 'oper-4c236420-0b11e945'], 
    ['BobC', 'oper-4c597644-402490ee'], 
    ['BobD', 'oper-4f05a90b-03b379f9'], 
    ['BobE', 'oper-4f30019a-27f27473'], 
    ['BobF', 'oper-4f300d33-de9592e3'], 
    ['BobG', 'oper-50f6e4ad-9effbec7'], 
    ['BobH', 'oper-52657816-3d6516e2'], 
    ['BobI', 'oper-qtgjvw8y-1uqtw058'], 
    ['Empty', ''], 
    ['Upg.', ''], 
    ['Ren.', ''] 
]; 

$idMappedNames = array_column($names, 0, 1); 

$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) { 
    if (!array_key_exists($item['id'], $carry)) { 
     $carry[$item['id']] = [ 
      'name' => $idMappedNames[$item['id']], 
      'amount' => 0 
     ]; 
    } 

    $carry[$item['id']]['amount'] += $item['amount']; 

    return $carry; 
}, []); 
+0

我剛剛編輯我的帖子,我仍然無法讓它正常工作。 – Juan 2014-10-31 14:24:58

0

您可以將這些信息在短短一個SQL查詢

$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal 
    FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID 
    WHERE a.ChargeAmount IS NOT NULL 
    AND a.PaymentStatus='OK' 
    AND a.PaymentDate LIKE '$currentDate%' 
    AND u.MonthlyGoal LIKE '$currentDate%' "); 
while ($row = mysql_fetch_assoc($result)) { 
    $wantedArray[] = array(
     'OperatorName' => $row['OperatorName'], 
     'MonthlyGoal'=>$row['MonthlyGoal'], 
     'ChargeAmount' => $row['ChargeAmount'], 
     'OperatorID' => $row['OperatorID']); 
} 

但最初提出的問題有云:

$desiredArray = array(); 
$id_to_amount = array(); 
foreach($array1 as $x) 
    $id_to_amount[$x['id']] = $x['amount']; 
foreach($array2 as $x) 
{ 
    $id = $x[1]; 
    $name = $x[0]; 
    $amount = -1; 
    if(isset($id_to_amount[$id])) 
     $amount = $id_to_amount[$id]; 
    $desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount); 
} 
+0

我剛剛編輯我的帖子,以包括一個額外的表,我需要拉動信息。 – Juan 2014-10-31 14:49:38

+0

操作員名稱是從tblOperatorGoals中提取的。對不起,我把它留下了。 – Juan 2014-10-31 14:56:42

+0

@Juan好的,我用tblOperatorGoals表和月度目錄字段編輯了我的加入表格解決方案 – David162795 2014-10-31 14:57:19