php - Associative Arrays -
i'm needing rearrange array 1 order of how [id] (oper-%%%%%%%%%%) appears within array 2, , i'm not quite sure how approach this.
array 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 ) )
array 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 illustration "name" here code far (and know isn't clean because isn't done pdo, i'm trying work @ moment):
$result = mysql_query("select * tbloperatorgoals monthlygoal '$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 * tbluserpayments chargeamount not null , paymentstatus='ok' , paymentdate '$currentdate%'"); while ($row = mysql_fetch_array($result)) { $operearnedarray[] = array( 'amount' => $row['chargeamount'], 'id' => $row['operatorid']); } foreach ($operearnedarray $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 $id => $value) { if (in_array($id,$operidarray)) { $opersums[] = array( 'id' => $id, 'amount' => $value); } }
any help appreciated. associative arrays not cup of tea.
basically i'm looking array looks this:
array ( [name] => bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
crude should work:
$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 $entry): foreach($raw $rawentry): if ($rawentry['id'] == $entry[1]): $temp = array( 'name' => $entry[0], 'oper' => $rawentry['id'], 'amount' => $rawentry['amount'], ); $returnarray[] = $temp; endif; endforeach; endforeach; homecoming $returnarray; }
php mysql
No comments:
Post a Comment