I'm trying to take parts number from two different tables and put them in the same array, then rearrange them.
Strangely it gives parts number twice, but when I run the same query in phpmyadmin it gives each part number once. I spend whole day but could not correct this.
//first query
$finalData = array(); $sql="SELECT
jobc_parts_p.part_no,
SUM(jobc_parts_p.issued_qty) AS sale_qty FROM
`jobc_parts_p` WHERE DATE_FORMAT(jobc_parts_p.date_time,'%Y-%m-%d') BETWEEN '".$f."' AND '".$t."'
GROUP BY jobc_parts_p.part_no";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$finalData[$row['part_no']][] = $row;
}
//second query
$sql2="SELECT
jobc_consumble_p.part_no,
SUM(jobc_consumble_p.issued_qty) AS csale_qty
FROM
`jobc_consumble_p`
WHERE DATE_FORMAT(jobc_consumble_p.date_time,'%Y-%m-%d') BETWEEN '".$f."' AND '".$t."'
GROUP BY
jobc_consumble_p.part_no";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$finalData[$row['part_no']][] = $row;
}
/// rearanging data......
$rearrangedFinalData = array();
foreach($finalData AS $first) {
foreach($first AS $data) {
$temp = array();
$temp['part_no'] = $data['part_no'];
$temp['sale_qty'] = isset($data['sale_qty']) ? $data['sale_qty'] : $data['csale_qty'];
$rearrangedFinalData[] = $temp;
}
}
//output result
foreach($rearrangedFinalData AS $row) {
$sr++;
echo "<tr><td>$sr</td>
<td colspan='2' >",$row["part_no"],"</td>
<td align='center'>",$row["sale_qty"],"</td>
</tr>";
}
RESULT
1 10R46 2
2 10R46 2
3 10R91 1
4 10R91 1
5 10M95 3
6 10M95 3
What i want:
1 10R46 2
2 10R91 1
3 10M95 3
First query print_r($finalData);
Array (
[10R46 ] => Array ( [0] => Array ( [part_no] =>
10R46 [sale_qty] => 1 ) )
[10R91 ] => Array ( [0] => Array ( [part_no] =>
10R91 [sale_qty] => 3 ) ))
Because, you are looping twice:
Modify
$rearrangedFinalData = array();
foreach($finalData AS $first) {
foreach($first AS $data) { // <-- Remove this extra loop.
$temp = array();
$temp['part_no'] = $data['part_no'];
$temp['sale_qty'] = isset($data['sale_qty']) ? $data['sale_qty'] : $data['csale_qty'];
$rearrangedFinalData[] = $temp;
}
}
To:
$rearrangedFinalData = array();
foreach($finalData AS $first) {
$temp = array();
$temp['part_no'] = $first['part_no'];
$temp['sale_qty'] = isset($first['sale_qty']) ? $first['sale_qty'] : $data['csale_qty'];
$rearrangedFinalData[] = $temp;
}
I think your problem lies within the $finalData where you store same results twice. (ofcourse from different tables though).
the first query creates a result of
$finalData = array(
'10R46' => array(0 => array('part_no' => 10R46, 'sale_qty' => 2)),
'10R91' => array(0 => array('part_no' => 10R91, 'sale_qty' => 1)),
'10M95' => array(0 => array('part_no' => 10M95, 'sale_qty' => 2))
);
then you run the second query and ADD to the finaldata again with this $finalData[$row['part_no']][] = $row;
so the $finalData is now something like
$finalData = array(
'10R46' => array(0 => array('part_no' => 10R46, 'sale_qty' => 2), 1 => array('part_no' => 10R46, 'csale_qty' => 2)),
'10R91' => array(0 => array('part_no' => 10R91, 'sale_qty' => 1), 1 => array('part_no' => 10R91, 'csale_qty' => 1)),
'10M95' => array(0 => array('part_no' => 10M95, 'sale_qty' => 3), 1 => array('part_no' => 10M95, 'csale_qty' => 3))
);
That is the reason why you double loop it and also get double results.
So i would combine the queries.
$sql="SELECT
jobc_parts_p.part_no,
SUM(jobc_parts_p.issued_qty) AS sale_qty,
SUM(jobc_consumble_p.issued_qty) AS csale_qty ## this part was added
FROM `jobc_parts_p`
LEFT JOIN `jobc_consumable_p` ON (jobc_consumable_p.part_no = jobc_parts_p.part_no) ## this part was added
WHERE DATE_FORMAT(jobc_parts_p.date_time,'%Y-%m-%d') BETWEEN '".$f."' AND '".$t."'
GROUP BY jobc_parts_p.part_no";
and now your result should look something like this without the secondquery
$finalData = array(
'10R46' => array(0 => array('part_no' => 10R46, 'sale_qty' => 2, 'csale_qty' => 2)),
'10R91' => array(0 => array('part_no' => 10R91, 'sale_qty' => 1, 'csale_qty' => 1)),
'10M95' => array(0 => array('part_no' => 10M95, 'sale_qty' => 2, 'csale_qty' => 3))
);
also if you don't expect there would be two rows with same part_no you can change
$finalData[$row['part_no']][] = $row; -> $finalData[$row['part_no']] = $row;
So you don't need to double loop it.
$new_array = array_values(array_unique($rearrangedFinalData));
Remove the duplicates and rearrange the keys (if needed) before you put your array in the foreach.
Then you can use the $new_array to generate your html
Related
I have been stuck for days on this 'little' problem.
I have one array which contains specific data:
$data = array(
0 => array('id' => 8, 'month' => 1, 'cost' => 12500),
1 => array('id' => 8, 'month' => 2, 'cost' => 14200),
2 => array('id' => 9, 'month' => 1, 'cost' => 23000),
3 => array('id' => 9, 'month' => 2, 'cost' => 18000),
);
And this is the html table results i need to get:
Id Jan Feb Mar Apr May
8 12,500 14,200 10,200 10,300 11,000
9 23,000 18,000 21,320 10,642 14,636
How i can sort array to display this data in html table on view.ctp ?
I have tried using foreach loops but i really don't know how to put unique months and unique id like it is displayed above. I'm using CakePHP 2.x technology.
I appreciate every help. Thank you
Loop over your array and group into a new array based on the ID. Assign month=cost key-value pairs, using IDs as the grouping keys of your multidimensional array.
$by_id = [];
foreach($data as $x) {
$by_id[$x['id']][$x['month']] = $x['cost'];
// e.g. $by_id[8][2] = 14200;
}
This will turn your sample data into the following array:
array(2) {
[8] · array(2) {
[1] · int(12500)
[2] · int(14200)
}
[9] · array(2) {
[1] · int(23000)
[2] · int(18000)
}
}
That should be much easier to turn into a HTML table. For example like this:
$html = [];
$html[] = '<table>';
$html[] = '<tr><th>ID</th><th>Jan</th><th>Feb</th></tr>';
foreach($by_id as $id => $months) {
$html[] = '<tr>';
$html[] = "<td>{$id}</td>";
foreach($months as $month => $cost) {
$html[] = "<td>{$cost}</td>";
}
$html[] = '</tr>';
}
$html[] = '</table>';
echo implode("\n", $html);
I trust the code is clear enough without elaborate comments. Add months to the table header as necessary. Use e.g. number_format for $cost to format your monies. Be aware that the sample code above doesn't account for possible missing months for a given ID, it assumes symmetric data. Add checks and remedies if necessary. See demo at https://3v4l.org/7CFRf.
You should use a 2D array to display that kind of table.
Then the $data array might be like this:
$data = array(
0 => array('id' => 8, 'costData' => array(
0 => array('month' => 1, 'cost' => 12500),
1 => array('monnt' => 2, 'cost' => 14200)
),
1 => array('id' => 9, 'costData' => array(
0 => array('month' => 1, 'cost' => 23000),
1 => array('month' => 2, 'cost' => 18000)
)
);
To revert the $data array like this, the code below will be needed.
<?php
$newData = array();
for($i = 0; $i < count($data); $i++)
{
$newItem = array('id' => $data[$i]['id'], 'costData' => array(
'month' => $data[$i]['month'],
'cost' => $data[$i]['cost']
));
for($j = $i + 1; $j < count($data); $j++)
{
if($data[$j]['id'] == $data[$i]['id']) {
$newItem['costData'][] = array('month' => $data[$j]['month'], 'cost' => $data[$j]['cost']);
array_splice($data, $j, 1);
$j--;
}
}
$newData[] = $newItem;
}
Using new data Array, you can make your html page like this.
$html = '';
for($i = 0; $i < count($newData); $i++)
{
$html .= '<tr>';
$html .= '<td>'.($newData[$i]['id']).'</td>';
for($j = 0; $j < count($newData[$i]['costData']); $j++) {
$html .= '<td>'.($newData[$i]['costData'][$j]).'</td>';
// you can do some operations if there are missing months to leave blank td tags..
}
// fill the missing months to fill the td tags.
}
You could use a combination of the array_column() and array_sum() functions:
$data = array(
array('id' => 8, 'month' => 1, 'cost' => 12500),
array('id' => 8, 'month' => 2, 'cost' => 14200),
array('id' => 9, 'month' => 1, 'cost' => 23000),
array('id' => 9, 'month' => 2, 'cost' => 18000),
);
$result = array();
foreach ($data as $row) {
$result[$row['id']][$row['month']] = $row['cost'];
}
// Sum the costs for each month
$months = array_column($data, 'month');
$result = array_map(function($v) use ($months) {
return array_sum(array_intersect_key($v, array_flip($months)));
}, $result);
print_r($result);
This will output:
Array
(
[8] => Array
(
[1] => 12500
[2] => 14200
)
[9] => Array
(
[1] => 23000
[2] => 18000
)
)
As an example I have an array like this:
$Batches = array(
$Batch_1= array(
$A_Row= array(1,1,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1)),
$Batch_2= array(
$A_Row= array(1,0,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1))
);
I want to sum per each $n_Row:
So I should get a result like this:
$Sum_Array = array(
$A_row=array(2,1,0,0),
$B_row= array(0,0,0,2),
$C_row = array(0,0,0,2)
);
But I'm having difficulty trying to get this result and need some tips/advice from someone else.
This is my code so far:
//count rows and columns
$count_array= count($Batches);
$count_row = count($Batches[0]);
$count_column = count($Batches[0][0]);
$Array_sum = array();
for($array=0;$array<$count_array;$array++){
for($row=0;$row<$count_row;$row++){
for($col=0;$col<$count_column;$col++){
//echo $Batches[$array][$row][$col] . "\n";
if($Batches[$array][$row][$col]==1){
$Batches[$array][$row][$col]++;
}
$Array_sum[]=$Batches[$array][$row][$col];
}
}
}
Anyone who can help me in the right direction?
Thanks in advance.
I think your "array"
$Batches = array(
$Batch_1= array(
$A_Row= array(1,1,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1)),
$Batch_2= array(
$A_Row= array(1,0,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1))
);
is a product of the imagination. But it is also valid PHP code. I assume you want to work with keys and the array looks like this:
$Batches = array(
'Batch_1' => array(
'A_Row' => array(1,1,0,0),
'B_Row' => array(0,0,0,1),
'C_Row' => array(0,0,0,1)),
'Batch_2' => array(
'A_Row' => array(1,0,0,0),
'B_Row' => array(0,0,0,1),
'C_Row' => array(0,0,0,1))
);
The Summation works better with foreach.
$sum_arr = [];
foreach($Batches as $ib => $batch){
foreach($batch as $ir => $row){
foreach($row as $ic => $col){
$sum_arr[$ir][$ic] ??= 0;
$sum_arr[$ir][$ic] += $col;
}
}
}
var_export($sum_arr);
The Output:
array (
'A_Row' =>
array (
0 => 2,
1 => 1,
2 => 0,
3 => 0,
),
'B_Row' =>
array (
0 => 0,
1 => 0,
2 => 0,
3 => 2,
),
'C_Row' =>
array (
0 => 0,
1 => 0,
2 => 0,
3 => 2,
),
)
The summation also works with the original array. However, this array has only numeric keys.
I have this array :
(
[id] => block_5df755210d30a
[name] => acf/floorplans
[data] => Array
(
[floorplans_0_valid_for_export] => 0
[floorplans_0_title] => title 1
[floorplans_0_house_area] => 40m²
[floorplans_0_bedrooms] => 1
[floorplans_1_valid_for_export] => 1
[floorplans_1_title] => title xx
[floorplans_1_house_area] => 90m²
[floorplans_1_bedrooms] => 2
[floorplans_2_valid_for_export] => 1
[floorplans_2_title] => title 2
[floorplans_2_house_area] => 50m²
[floorplans_2_bedrooms] => 1
[floorplans] => 3
)
)
As we can see in the data, we have fields (floorplans_X_valid_for_export).
What I want to do is to get the data only when this field equal to 1.
So from the given example, I want to keep only these fields:
[floorplans_1_valid_for_export] => 1
[floorplans_1_title] => title xx
[floorplans_1_house_area] => 90m²
[floorplans_1_bedrooms] => 2
[floorplans_2_valid_for_export] => 1
[floorplans_2_title] => title 2
[floorplans_2_house_area] => 50m²
[floorplans_2_bedrooms] => 1
This is an odd schema, but it can be done by iterating through the array and searching for keys where "valid_for_export" equals 1, and then using another array of field "stubs" to get the associated items by a unique identifier of X in floorplans_X_valid_for_export
$array = [
'floorplans_0_valid_for_export' => 0,
'floorplans_0_title' => 'title 1',
'floorplans_0_house_area' => '40m²',
'floorplans_0_bedrooms' => 1,
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
'floorplans' => 3
];
$stubs = [
'floorplans_%s_valid_for_export',
'floorplans_%s_title',
'floorplans_%s_house_area',
'floorplans_%s_bedrooms'
];
$newArr = [];
foreach ($array as $key => $value) {
if (strpos($key, 'valid_for_export') && $array[$key] == 1) {
$intVal = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
foreach ($stubs as $stub) {
$search = sprintf($stub, $intVal);
if (isset($array[$search])) {
$newArr[$search] = $array[$search];
} else {
// key can't be found, generate one with null
$newArr[$search] = null;
}
}
}
}
echo '<pre>';
print_r($newArr);
Working: http://sandbox.onlinephpfunctions.com/code/23a225e3cefa2dc9cc97f53f1cbae0ea291672c0
Use a parent loop to check that the number-specific valid_for_export value is non-empty -- since it is either 0 or non-zero.
If so, then just push all of the associated elements into the result array.
Some reasons that this answer is superior to the #Alex's answer are:
Alex's parent loop makes 13 iterations (and the same number of strpos() calls); mine makes just 3 (and only 3 calls of empty()).
$array[$key] is more simply written as $value.
Sanitizing the $key to extract the index/counter is more overhead than necessary as demonstrated in my answer.
Code (Demo)
$array = [
'floorplans_0_valid_for_export' => 0,
'floorplans_0_title' => 'title 1',
'floorplans_0_house_area' => '40m²',
'floorplans_0_bedrooms' => 1,
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
'floorplans' => 3
];
$labels = ['valid_for_export', 'title', 'house_area', 'bedrooms'];
$result = [];
for ($i = 0; $i < $array['floorplans']; ++$i) {
if (!empty($array['floorplans_' . $i . '_valid_for_export'])) {
foreach ($labels as $label) {
$key = sprintf('floorplans_%s_%s', $i, $label);
$result[$key] = $array[$key];
}
}
}
var_export($result);
Output:
array (
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
)
With that constructed data it might be hard (not impossble tho), hovewer i would suggest to change it to multidimensional arrays so you have something like:
[floorplans][0][valid_for_export] => 0
[floorplans][0][title] => title 1
[floorplans][0][house_area] => 40m²
[floorplans][0][bedrooms] => 1
[floorplans][1][valid_for_export] => 1
[floorplans][1][title] => title xx
[floorplans][1][house_area] => 90m²
Rought sollution
It is not the best approach, but it should work if you dont need anything fancy, and know that structure of data wont change in future
$keys = [];
$for($i=0;$i<$array['floorplans'];++$i) {
if(isset($array['floorplans_'.$i.'_valid_for_export']) && $array['floorplans_'.$i.'_valid_for_export']===1) {
$keys[] = $i;
}
}
print_r($keys);
I want to count all the values (The name of the cities) in this array who have the character t before the last character in the string.
ARRAY
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
The results should be: 3 (Paris_t1 - Madrid_t1 - Amsterdam_t1)
I believe i have to combine:
array_count_values($cities_array)
and
substr($value, -2, 1) == "t"
I have tried, but I get only errors.
This will give you what you want.
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
$count = 0;
$city_text = '';
foreach($cities_array as $city){
if(substr($city, -2, 1) == "t"){
$count++;
$city_text .= $city . '-';
}
}
echo $count. "(".rtrim($city_text,'-').")";
Try below solution:
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
$filtered_array = array_filter($cities_array, function($val){
return (strpos($val, 't', (strlen($val)-2)) !== false);
});
print_r($filtered_array);
output: - (you can implode array by - to get desired result)
Array
(
[city1] => Paris_t1
[city2] => Madrid_t1
[city3] => Amsterdam_t1
)
In above solution in strpos third parameter strlen($val)-2) i.e. position will be searched from second last character of $val
public function getCheckoutForm(){
$arr = array(
'cmd' => '_cart',
'business' => 'some#mail',
'no_shipping' => '1',
'upload' => '1',
'return' => 'url',
'cancel_return' => 'url1',
'no_note' => '1',
'currency_code' => 'url2',
'bn' => 'PP-BuyNowBF');
$cpt=1;
foreach($this->items as $item){
$arr1[] = array(
'item_number_'.$cpt.'' => $item['item_id'],
'item_name_'.$cpt.'' => $item['item_name'],
'quantity_'.$cpt.'' => $item['item_q'],
'amount_'.$cpt.'' => $item['item_price']
);
$cpt++;
}
return array_merge($arr,$arr1[0],$arr1[1]);
}
This returns array like that:
Array
(
[cmd] => _cart
[business] => some#mail
[no_shipping] => 1
[upload] => 1
[return] => url1
[cancel_return] =>url2
[no_note] => 1
[currency_code] => EUR
[bn] => PP-BuyNowBF
[item_number_1] => 28
[item_name_1] => item_name_1
[quantity_1] => 1
[amount_1] => 5
[item_number_2] => 27
[item_name_2] => item_name_2
[quantity_2] => 1
[amount_2] => 30
)
The problem is that in return $arr1[0] and $arr1[1] are hardcoded. And if in loop i have more than 2 arrays, lets say 0,1,2,3 ans so on, this code won't work. Any idea? Maybe my logic is compleatly wrong...
There's no need to create arrays in your loop - just add new keys directly to the first array:
public function getCheckoutForm(){
$arr = array(
'cmd' => '_cart',
'business' => 'some#mail',
'no_shipping' => '1',
'upload' => '1',
'return' => 'url',
'cancel_return' => 'url1',
'no_note' => '1',
'currency_code' => 'url2',
'bn' => 'PP-BuyNowBF'
);
$cpt=1;
foreach($this->items as $item){
$arr['item_number_'.$cpt] = $item['item_id'];
$arr['item_name_'.$cpt] = $item['item_name'];
$arr['quantity_'.$cpt] = $item['item_q'];
$arr['amount_'.$cpt] = $item['item_price'];
$cpt++;
}
return $arr;
}
I would probably do something like
$count = count($arr1);
for($i=0;$i<$count;$i++){
$arr = array_merge($arr,$arr1[$i]);
}
return $arr;
I hope, I understood, what you mean ^^
foreach ($i = 0, $n = count($arr1); $i < $n; $i++) {
$arr = array_merge($arr, $arr1[$i]);
}
return $arr;
You could do the merge in every iteration:
foreach($this->items as $item){
$temp_arr = array(
'item_number_'.$cpt.'' => $item['item_id'],
'item_name_'.$cpt.'' => $item['item_name'],
'quantity_'.$cpt.'' => $item['item_q'],
'amount_'.$cpt.'' => $item['item_price']
);
$arr = array_merge($arr,$temp_arr)
$cpt++;
}
which has the advantage that you could possibly get $temp_arr from a function,
or just add all the elements to one array:
foreach($this->items as $item){
$arr['item_number_'.$cpt.''] => $item['item_id'];
$arr['item_name_'.$cpt.''] => $item['item_name'];
$arr['quantity_'.$cpt.''] => $item['item_q'];
$arr['amount_'.$cpt.''] => $item['item_price'];
$cpt++;
}
do this
$count = count($data);
$sum = 1;
$arr = [];
for($i=0;$i<$count;$i++){
$temp = $arr;
if($i == $count - 1){
$sum = 0;
}
$arr = array_merge($temp,$data[$i + $sum]);
}
return $arr;