looping through multidimensional array from post and save to mysql - php

This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.

Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise

Related

Finding matching keys in a multidimensional array and storing them in new array

I want to parse a multidimensional array and store all the matching keys into their own array.
Input Array:
array (
[Items] => Array (
[0] => AS1585.AGG
[1] => AS1585.AGG
[2] => ZBRK-1776
[3] => ZBRK-2076
[4] => ZBRK-3001
)
[Qty2] => Array (
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 6
)
[Cost] => Array (
[0] => $351.00
[1] => $351.00
[2] => $437.00
[3] => $380.00
[4] => $358.00
)
[TotalCubes2] => Array (
[0] => 168.40
[1] => 168.40
[2] => 186.05
[3] => 161.25
[4] => 140.70
)
[TotalCost2] => Array (
[0] => $1755.00
[1] => $1755.00
[2] => $2185.00
[3] => $1900.00
[4] => $2148.00
)
)
Desired output:
Array (
[Items] => AS1585.AGG
[Qty2] => 5
[Cost] => $351.00
[TotalCubes2] => 168.40
[TotalCost2] => $1755.00
)

How to access 2D array fields in PHP

-----problem solved, see the update 2 below----
I put all MySQL query results into a 2D array:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
But now I cannot access the $allSuppliersInfo fields.
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
What am I doing wrong?
------- UPDATE-----
print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------ UPDATE 2-----
Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.
I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.
This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

how to loop through an array within an array in php

Here's the array structure (only the first element in the array):
Array
(
[1] => Array
(
[pageid] => 1
[step_order] => 1
[pageurl] => http://www.domain.com/
[in_links] => Array
(
[domains] => Array
(
[Direct Entry] => 1520
[www.google.com] => 387
[www.google.co.in] => 14
[search.yahoo.com] => 10
[All other] => 27
)
[impressions] => Array
(
[Direct Entry] => Array
(
[0] => 10654
[1] => 10728
[2] => 10772
)
[www.google.com] => Array
(
[0] => 10991
[1] => 12455
[2] => 12466
[3] => 10757
)
[www.google.co.in] => Array
(
[0] => 9839
[1] => 9837
[2] => 9845
)
[search.yahoo.com] => Array
(
[0] => 12087
[1] => 10864
)
)
)
[out_links] => Array
(
[domain] => Array
(
[Left site] => 1752
[http://www.domain.com/#] => 102
[http://www.domain.com/contact] => 102
[http://www.domain.com/#basic_inline_div] => 2
)
[impressions] => Array
(
[Left site] => Array
(
[0] => 7680
[1] => 9728
[2] => 10496
)
[http://www.domain.com/#] => Array
(
[0] => 259
[1] => 11013
)
[http://www.domain.com/contact] => Array
(
[0] => 12802
[1] => 10757
)
[http://www.domain.com/#basic_inline_div] => Array
(
[0] => 11
[1] => 51
)
)
)
[visitors] => 1958
)
)
I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}

How to sum values via the same key and group by other key

I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values?
My array looks like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 4
[1] => 762182Z
)
[2] => Array
(
[0] => 2
[1] => 072182X
)
[3] => Array
(
[0] => 4
[1] => 660122Y
)
[4] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[5] => Array
(
[0] => 1
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 1
[1] => 225160Y
)
[8] => Array
(
[0] => 8
[1] => 954120Y
)
[9] => Array
(
[0] => 3
[1] => 072182X
)
)
I'd like to something like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[2] => Array
(
[0] => 4
[1] => 762182Z
)
[3] => Array
(
[0] => 5
[1] => 072182X
)
[4] => Array
(
[0] => 4
[1] => 660122Y
)
[5] => Array
(
[0] => 2
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 8
[1] => 954120Y
)
)
The array in short version:
Array => TO => Array
( (
[0] => 2:790180X:26mm [0] => 2:790180X:26mm
[1] => 4:762182Z [1] => 2:790180X:24mm
[2] => 2:072182X [2] => 4:762182Z
[3] => 4:660122Y [3] => 5:072182X
[4] => 2:790180X:24mm [4] => 4:660122Y
[5] => 1:225160Y [5] => 2:225160Y
[6] => 1:244160Y [6] => 1:244160Y
[7] => 1:225160Y [7] => 8:954120Y
[8] => 8:954120Y )
[9] => 3:072182X
)
Just sum into a new array, using the various "grouping" fields as keys in the new array:
$sums = array();
foreach ($yourarray as $element) {
$sums[$element['dimension1']][$element['dimension2']]++;
}
For every dimension you need to 'group' by, you add a key to the $sums array.

Is this the correct syntax for 'array_unique()' in PHP?

I want to get rid of duplicates in my array but I'm still printing duplicates with this:
$getuser = ltrim($top10['url'], " users/" ); //trim url to get user id
$array[$i++] = $getuser;
$dirty = $array;
$clean = array_unique($dirty);
print_r($clean)."<br />";
Input print_r($array)
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Output print_r($clean);
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Yes, it seems to be correct. Basically you specify an array to array_unique and it gives you unique items out of the array.
$unique_array = array_unique($your_array);

Categories