How can i compare and merge two array - php

I have following two arrays which comes from the same database use the same query to get both. both contains information about a single sales order having two different line items(Bill of materials) the first one has Line ItemItemID= 600271 and the second one has LineItem ItemID=600274 but as you can see below they both have the same Sales Order Number [CustomerSONo] => [7] => **15020**
so How can i compare or union these two array's. ?
//query
$result= --select statement--;
while($row = mysqli_fetch_array($result)) {
print_r($row);
}
Array 1
Array (
[0] => XXX001
[CustomerId] => XXX001
[1] => XXX Company Name.*
[Customer_Bill_Name] => XXX Company Name.*
[2] => DHE
[WhichShipVia] => DHE [3] =>
[INV_POSOOrderNumber] => [4] => 2014-12-19
[ShipByDate] => 2014-12-19 [5] =>
[GoodThruDate] => [6] =>
[CustomerSONo] => [7] => 15020
[Reference] => 15020 [8] => 2014-11-25
[TransactionDate] => 2014-11-25 [9] => 1
[DistNumber] => 1
[10] => 70.0000000000000000000 //here is the difference 1
[Quantity] => 70.0000000000000000000 //here is the difference 2
[11] => 600271 //here is the difference 3
[ItemId] => 600271 //here is the difference 4
[12] => ASSY7.60-15SL/8 FRM I1 15X6 6-6, BLK (GWT-761508 (24) //here is the difference 5
[SalesDescription] => ASSY7.60-15SL/8 FRM I1 15X6 6-6, BLK (GWT-761508)(24)//here is the difference 1 //here is the difference 6
[13] => AS1577 //here is the difference 7
[PartNumber] => AS1577 //here is the difference 8
[14] => ASSY7.60-15 8PLY W/WHL15X6 BLK //here is the difference 9
[ItemDescription] => ASSY7.60-15 8PLY W/WHL15X6 BLK )
Array 2:
Array (
[0] => XXX001
[CustomerId] => XXX001
[1] => XXX Company Name.*
[Customer_Bill_Name] => XXX Company Name.*
[2] => DHE [WhichShipVia] => DHE [3] =>
[INV_POSOOrderNumber] => [4] => 2014-12-19
[ShipByDate] => 2014-12-19 [5] =>
[GoodThruDate] => [6] =>
[CustomerSONo] => [7] => 15020
[Reference] => 15020 [8] => 2014-11-25
[TransactionDate] => 2014-11-25 [9] => 2
[DistNumber] => 2
[10] => 6.0000000000000000000 //here is the difference 1
[Quantity] => 6.0000000000000000000 //here is the difference 2
[11] => 600274 //here is the difference 3
[ItemId] => 600274 //here is the difference 4
[12] => ASSY9.5L-15SL/8 FLT I1 15X8 6-6, BLK (GWT-951508)(16)
[SalesDescription] => ASSY9.5L-15SL/8 FLT I1 15X8 6-6, BLK (GWT-951508)(16) //here is the difference 5
[13] => AS1601 //here is the difference 6
[PartNumber] => AS1601 //here is the difference 7
[14] => ASSY9.5L-15 W/WHL15X8 6/6 BLK //here is the difference 8
[ItemDescription] => ASSY9.5L-15 W/WHL15X8 6/6 BLK ) //here is the difference 9

Option 1
$orders = array();
while($row = mysqli_fetch_array($result)) {
$orders[$row['CustomerSONo'][] = $row;
}
This will add all rows that share the same CustomerSONo together in one array. If you have multiple orders, you can get all the seperate orders using
foreach($orders as $orderNo => $order) {
foreach($order as $key => $orderRow) {
}
}
Option 2
If you however only want to extract the ItemIDs from each order, you can do the following:
$orderItems = array();
while($row = mysqli_fetch_array($result)) {
$orderItems[$row['CustomerSONo']][] = $row['ItemID'];
}
This will create an array called $orderItems that only stores the order numbers in the array, rather than all the information about the order.
Option 3
If you want to echo the lines, you first have to "sort" them like in Option 2. Once you have gained all ItemIDs that belong to one CustomerSONo, you do another foreach loop to echo them into a single line.
foreach($orders as $orderNo => $itemIds) {
echo "Order #" . $orderNo . ": " . implode(", ", $itemIds);
}
This will create the following:
Order #1: 18340, 1837, 13
Order #2: 183, 868, 285, 860

Related

Update MySQL table with foreach passing multiple list values

I'm actually running into an issue when trying to put this into practice. So, I have a list of goal ids and a current amount. I need to pass all these values which I am by doing goalidlist[] and current[]. The issue is when I'm running the foreach and doing the SQL Update per goalid. Here's my code below. Hope someone can point out what I'm doing wrong.
$goalidlist = $_POST['goalidlist'];
$currentnums = $_POST['current'];
if (isset($_POST['goalidlist'])) {
foreach($goalidlist as $key => $glist) {
// Update key goals
$updGoals = "UPDATE okr_3519031jkl.key_goals SET current=? WHERE goalid=? AND userid=?";
if ($stmt = mysqli_prepare($conn, $updGoals)) {
// Bind Params
mysqli_stmt_bind_param($stmt, "sii", $currentnums[$key], $glist, $userid);
mysqli_stmt_execute($stmt);
$stmt->close();
}
}
}
goalidlist[] = Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 6 [6] => 9 [7] => 7 [8] => 8 [9] => 10 [10] => 11 [11] => 12 [12] => 13 )
current[] = Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 6 [4] => 2 [5] => 12 [6] => 3 [7] => 1 [8] => 7 [9] => 9 [10] => 2 [11] => 6 [12] => 3 )

Swiss Tournament. Team confrontation multiple rounds without repeat and ordered

I try to make a tournament based on Swiss method.
I have for example 12 teams, and in each round, every team has points (0 to 100) and wins, loses or draws.
I want to find which teams play against each other, with these conditions:
Ordered by wins, draws, and points.
Not to be played previously.
In each round, I got foreach team the possible teams to play against in an array like this: (The key indicates the team id, and the values indicate ll possible teams to play separated by ",")
[2] => 4,11,6,10,3,8,7,12,
[5] => 4,11,9,10,3,8,1,12,
[4] => 2,5,6,10,8,7,12,
[11] => 5,9,10,3,8,7,
[9] => 5,11,6,3,8,7,12,
[6] => 2,4,9,10,3,7,12,
[10] => 2,5,4,11,6,8,7,12,
[3] => 5,11,9,6,8,7,
[8] => 2,5,4,11,9,10,3,1,12,
[7] => 2,4,11,9,6,10,3,1,12,
[1] => 5,4,11,9,6,3,8,7,
[12] => 2,5,4,9,6,10,8,7,
First I have all teams played before in a array: (key indicates team id)
Array (
[1] => 2,10,12,
[2] => 1,9,5,
[3] => 4,12,10,
[4] => 3,11,9,
[5] => 6,7,2,
[6] => 5,8,11,
[7] => 8,5,8,
[8] => 7,6,7,
[9] => 10,2,4,
[10] => 9,1,3,
[11] => 12,4,6,
[12] => 11,3,1, )
Then, I get all teams ordered by wins, loses and points in a array: (Keys indicates also team id)
Array
(
[1] => 2
[2] => 5
[3] => 4
[4] => 11
[5] => 9
[6] => 6
[7] => 10
[8] => 3
[9] => 8
[10] => 7
[11] => 1
[12] => 12
)
Finaly I try to find the possible match against two teams.
$checks = array();
$pairs = array();
for ($i = 1; $i <= count($list); $i++) {
for ($j = 1; $j <= count($list); $j++) {
if(strpos($plays[$list[$i]], $list[$j]) !== false || $list[$i] == $list[$j] ) {
}else{
if(!in_array($list[$i],$checks) && !in_array($list[$j],$checks)){
$pairs[] = $list[$i].",".$list[$j];
$checks[] = $list[$i];
$checks[] = $list[$j];
}
}
}
}
And finaly I print the array "$pairs". It shows:
Array
(
[0] => 2,4
[1] => 5,11
[2] => 9,6
[3] => 10,8
[4] => 3,7
)
Thats not correct because the team_id 1 and team_id 12 can not play in this round because the played before:
I don't know how to solve this.
Thanks again!

Compare Array elements and add based on key and value

I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z

2 arrays count how many times id = date

Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo

Sort php associative array [duplicate]

This question already has answers here:
Sorting an associative array in PHP [duplicate]
(5 answers)
Closed 8 years ago.
I am trying to sort an array by miles.
$i = 0;
foreach($results as $key => $value)
{
echo $results['miles'][$i] . "<br />";
echo $results['postcode'][$i] . "<br />";
echo $results['id'][$i] . "<br />";
echo $results['description'][$i] . "<br />";
echo $results['title'][$i] . "<br />";
echo $results['thumbnail'][$i] . "<br />";
$i++;
}
AS you can see I'm having 6 different keys here. I want to order everything by $results['miles'] in ASCENDING order.
This is my array structure:
Array ( [miles] => Array ( [0] => 0 [1] => 0 [2] => 14 [3] => 8 [4] => 8 [5] => 0 [6] => 8 [7] => 14 ) [title] => Array ( [0] => Stunning 1 Bedroom Apartment With Concierge [1] => Big Double Inc Bills+ Balcony+Free Parking [2] => Large, Sunny Flat In Willesden [3] => Brewhouse Yard EC1V [4] => Stunning 2 Double Bed Flat - City [5] => Room To Let £575 Pm Bills Included [6] => All Bills Inclusive | Gorgeous 1 Bed In The City [7] => Large Double Room In Zone 2 (Kensal Green 2 Mins) ) [id] => Array ( [0] => 187 [1] => 176 [2] => 186 [3] => 178 [4] => 179 [5] => 177 [6] => 183 [7] => 182 ) [thumbnail] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => ) [postcode] => Array ( [0] => IG11 [1] => ig11 [2] => NW10 [3] => EC1 [4] => ec1 [5] => ig11 [6] => ec1 [7] => NW10 ) )
Any help would be appreciated!
If you can, the best way is to sort the data before it comes in to your script (if it's coming from SQL then order it there).
If you need to do it in PHP though you can use the uasort function, which allows you to define your own comparison:
function compareMiles($a, $b) {
if ($a['miles'] == $b['miles']) {
return 0;
}
return ($a['miles'] < $b['miles']) ? -1 : 1;
}
uasort($results, 'compareMiles');
You can use usort() function to sort it.
Use asort() to sort an array by maintain index association.
Refer this link for details and example

Categories