I need help to get only the paired values of this arrays:
Array $requestedFields:
Array
(
[0] => id
[1] => first-name
[2] => last-name
[3] => username
[4] => email
[5] => truntt
[6] => fiff
)
Array $apiFields:
Array
(
[0] => Array
(
[0] => id
[1] => id
[2] => accounts.profile.basic
)
[1] => Array
(
[0] => first-name
[1] => fname
[2] => accounts.profile.basic
)
[2] => Array
(
[0] => last-name
[1] => lname
[2] => accounts.profile.basic
)
[3] => Array
(
[0] => username
[1] => username
[2] => accounts.profile.basic
)
[4] => Array
(
[0] => email
[1] => email
[2] => accounts.profile.emailaddress
)
[5] => Array
(
[0] => test-test
[1] => test
[2] => accounts.profile.test
)
)
I need to get the $apiFields elements matched with $requestedFields in this case only 0-4 elements values.
Any help is appreciated.
Use the below code:
foreach($apiFields as $key=>$arr){
if(!in_array($arr[0], $requestedFields)){
unset($apiFields[$key]);
}
}
It will give you the expected result in $apiFields array.
Related
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
-----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);
}
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
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.