关联数组 [英] Associative Arrays
问题描述
我需要按照 [id] (oper-%%%%%%%%%%%) 在数组 2 中出现的顺序重新排列数组 1,我不太确定如何接近这个.
I'm needing to rearrange Array 1 by the order of how the [id] (oper-%%%%%%%%%%) appears within Array 2, and I'm not quite sure as to how to approach this.
数组 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] => ) )
Bob 只是一个例子名字"到目前为止,这是我的代码(我知道这并不干净,因为这不是用 PDO 完成的,我现在只是想让它工作):
Bob is just an example "name" Here is my code so far (And I know this isn't clean because this isn't done with PDO, I'm just trying to get this to work at the moment):
$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);
}
}
非常感谢任何帮助.关联数组不是我的菜.
Any help is greatly appreciated. Associative arrays are just not my cup of tea.
基本上我正在寻找一个看起来像这样的数组:
Basically I'm looking for an Array that looks like this:
Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
推荐答案
你可以在一个 SQL Query 中连接这些信息
You can connect these information it in just single SQL Query
$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']);
}
<小时>
但正如最初提出的问题:
But as the question originally asked goes:
$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 );
}
这篇关于关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!