Update MySQL table with foreach passing multiple list values - php

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 )

Related

How to store 3 key by index per row PHP [duplicate]

This question already has an answer here:
Multi dimensional loops from 4 existing arrays [duplicate]
(1 answer)
Closed 10 months ago.
i have problem with array, i have 3 array and i wont to merge into one by row index
$tahun = array(2010,2011,2012,2013,2014);
$status = array("akademi","instansi","umum","pending","pass");
$total = array(2,1,3,4,5);
i want this array into want like this
array(
0 => array(2010,"akademi",2),
1 => array(2011,"instansi",1),
2 => array(2012,"umum",3),
3 => array(2013,"pemding",4),
4 => array(2014,"pass",5),
);
but when i use array_merge_recursive() the output like this
Array
(
[0] => 2015
[1] => 2016
[2] => 2017
[3] => 2018
[4] => 2019
[5] => 2019
[6] => 2019
[7] => 1
[8] => 1
[9] => 1
[10] => 1
[11] => 3
[12] => 2
[13] => 1
[14] => akademisi
[15] => instansi pemerintah
[16] => umum
[17] => umum
[18] => akademisi
[19] => instansi pemerintah
[20] => umum
)
You can use array_map() with null as the callback...
$combined = array_map(null, $tahun, $status, $total);
from the manual...
NULL can be passed as a value to callback to perform a zip operation
on multiple arrays. If only array1 is provided, array_map() will
return the input array.
Use simple foreach loop:
$res = [];
foreach($tahun as $ind=>$val){
$res[$ind] = [$val, $status[$ind], $total[$ind]];
}
Demo

PHP Working with an array to find ranges and missing numbers

I have an array that looks like this:
[0] => Array
(
[1] => 5
[2] => 4
[3] => 3
[5] => 1
[7] => 1
[8] => 2
[9] => 3
[10] => 4
[11] => 5
)
[1] => Array
(
[1] => 6
[2] => 5
[4] => 3
[5] => 2
[6] => 1
[8] => 3
[9] => 4
[10] => 5
[11] => 6
)
[2] => Array
(
[1] => 7
[2] => 6
[3] => 5
[4] => 4
[5] => 3
[6] => 2
[7] => 3
[8] => 4
[11] => 7
)
I have an order of operations I'm trying to go through and I really don't know where to go from here. Any suggestions would be of great help.
First I give my class the number of items I want to return. For example here we'll use 4.
I want to loop through and find the item in the array that has the lowest value.
I want to look at the keys to the items around (it included) and be sure they're not missing a number if they are..reject it..
In this example the first one you would come to would be:
[5] => 1
Now looking around it you see that the keys are missing some numbers. So no combination of 4 will get that to match any 4 in the proper order.
[1] => 5
[2] => 4
[3] => 3
[5] => 1 //this one
[7] => 1
[8] => 2
[9] => 3
In this situation I want it to move onto the next case.
[7] => 1
Notice this one will work due to the keys being 7,8,9,10.
[7] => 1
[8] => 2
[9] => 3
[10] => 4
This is what I would like returned first..but I don't even know how to begin to get there.
Further more there are situations like this..Say for example there are no 1's at all in the dataset..and only this lone 2 in the last one.
[0] => Array
(
[1] => 5
[2] => 4
[3] => 3
[5] => 3
[7] => 3
[8] => 3
[9] => 3
[10] => 4
[11] => 5
)
[1] => Array
(
[1] => 6
[2] => 5
[4] => 3
[5] => 3
[6] => 3
[8] => 3
[9] => 4
[10] => 5
[11] => 6
)
[2] => Array
(
[1] => 7
[2] => 6
[5] => 3
[6] => 2 // this one
[7] => 3
[8] => 4
[11] => 7
)
The following wont work:
[6] => 2 // this one
[7] => 3
[8] => 4
[11] => 7
but this one will:
[5] => 3
[6] => 2 // this one
[7] => 3
[8] => 4
I have no idea on how to approach this.. If someone could offer some advice it would be GREATLY appreciated. Thanks so much in advance.
The following assumes your data is in an array called $data. I'll describe it in steps, then pull it all together as a function.
Step 1 find the min value:
$minValue=min($data);
Step 2 loop through the array looking for all values that are that value:
foreach($data as $index => $value){
if($value == $minValue){
// $index is a candidate!
}
}
Step 3 Check if $valuesToReturn entries exist after index:
$success=true;
for($i=1;$i<=$valuesToReturn;$i++){
if(!array_key_exists($index + $i,$data)){
// Candidate failed.
$success=false;
break;
}
}
Step 4 If the candidate was successful, return it.
if($success){
return $index;
}
Putting that all together, we get this:
function findSuitableIndex($data,$valuesToReturn){
// Min:
$minValue=min($data);
foreach($data as $index => $value){
if($value == $minValue){
// $index is a candidate!
// test if index is actually suitable:
$success=true;
for($i=1;$i<=$valuesToReturn;$i++){
if(!array_key_exists($index + $i,$data)){
// Candidate failed.
$success=false;
break;
}
}
if($success){
return $index;
}
}
}
// If we fell down here, we failed to find any successful results.
return -1;
}
Working sample:
Code on eval.in
Here are some suggestions. Your exact implementation would depend on your particular situation.
To loop through each element in the array, you could use a foreach loop.
foreach ($arr[0] as $index => $value) {
// Here, $arr[0][$index] == $value;
}
To check if a key exists or not, you could use array_key_exists.
if ( !array_key_exists($index - 1, $arr[0]) ) {
// The previous index is missing in the array.
}
A simple (but inefficient) way to find a contiguous sequence of k indices with the smallest value at the first index would be to find the smallest element and check if the contiguous sequence exists; if not, find the next smallest element and recursively check until you have completed processing the largest element.
You could also try finding all contiguous sequences of at least length k and then selecting the sequence with the smallest value at the first index.
Hope this helps!

How can i compare and merge two array

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

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

Re-order PHP array by middle key as start (Circular Sorting)

very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.

Categories