php array_diff not working on this compare - php

I have two arrays:
(
[lineCode] => C
[serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
[skillCode] => 90SB
[opCode] => 90SB
[jobType] => CUSTOMER
[techNo] =>
[lineStatus] => C
)
(
[id] => 755350
[rid] => 252178
[lineCode] => C
[serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
[skillCode] => 90SB
[opCode] => 90SB
[jobType] => CUSTOMER
[techNo] =>
[lineStatus] => W
[timeA] => 1575497139
[timeC] => 0
)
When i perform $diff = array_diff($arry1, $arry2);, it does not find the lineStatus to be different. Could it be because of service Request line with special characters? Although as a test, I set both lines to blanks, and it still did not see the difference.
Any help would be great. Stumped for the day.
UPDATE w/ MORE INFO
As this is part of a larger loop through mulitple arrays, the diff check before this one above is below:
$arry1= Array
(
[lineCode] => B
[serviceRequest] =>
[skillCode] => 15
[opCode] => 15
[jobType] => CUSTOMER
[techNo] => A05
[lineStatus] => C
)
$arry2= Array
(
[id] => 755362
[rid] => 252184
[lineCode] => B
[serviceRequest] =>
[skillCode] => 15
[opCode] => 15
[jobType] => CUSTOMER
[techNo] => A05
[lineStatus] => W
[timeA] => 1575504138
[timeC] => 0
)
$diff= Array
(
[lineStatus] => C
)
the code for the diff is $diff=array_diff($arry1,$arry2);
If it works correctly for this one, why would it not for the next.

Why does it not find it the way you originally did it...?
array_diff() does not care about key-value combinations, it only compares the values in the different arrays that are compared.
To better understand what's happening: if in your updated data you introduce an array element [dummy] => "C" into $arry2 you will no longer get [lineStatus] => "C" returned into $diff. As the value C is now found in both $arry1 and $arry2.
Have a look at this demo

Related

How to add sum values of array according to keys in php

I have an array of php like this
Array
(
[0] => Array
(
[Sr_No] => 1
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Ankle
[Asan_Sub_Cat_Val] => 35
[Asan_Name] => General Ankle Warm up
[Asan_Name_Val] => 447
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Actual_Steps] => 12
[Actual_Ratio] => 0
)
[1] => Array
(
[Sr_No] => 2
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Knee
[Asan_Sub_Cat_Val] => 111
[Asan_Name] => General knee warm up
[Asan_Name_Val] => 464
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Actual_Steps] => 14
[Actual_Ratio] => 0
)
[2] => Array
(
[Sr_No] => 1
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Ankle
[Asan_Sub_Cat_Val] => 35
[Asan_Name] => General Ankle Warm up
[Asan_Name_Val] => 447
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Actual_Steps] => 10
[Actual_Ratio] => 0
)
[3] => Array
(
[Sr_No] => 2
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Knee
[Asan_Sub_Cat_Val] => 111
[Asan_Name] => General knee warm up
[Asan_Name_Val] => 464
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Actual_Steps] => 9
[Actual_Ratio] => 0
)
)
The desired output I want
Array
(
[0] => Array
(
[Asan_Id] => 447
[Asan_Category] => Warm-Up
[Asan_Sub_Category] => Ankle
[Asan_Name] => General Ankle Warm up
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Total_Steps] => 22
)
[1] => Array
(
[Asan_Id] => 464
[Asan_Category] => Warm-Up
[Asan_Sub_Category] => Knee
[Asan_Name] => General knee warm up
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Total_Steps] => 23
)
)
I want those data who are repeating become one but their different actual steps become total steps with their sum.
Please help me because I have tried some code but did not success like this
$asan=[];
$total_steps=0;
foreach ($aasan_details as $key) {
$total_steps += $key['Actual_Steps'];
if(!in_array($key['Asan_Name_Val'],$asan))
{
$asan[] = $key['Asan_Name_Val'];
$lookup[] = array("Asan_Id"=>$key['Asan_Name_Val'],
"Asan_Category"=>$key['Asan_Category'],
"Asan_Sub_Category"=>$key['Asan_Sub_Category'],
"Asan_Name"=>$key['Asan_Name'],
"Prescribed_Steps"=>$key['Prescribed_Steps'],
"Prescribed_Ratio"=>$key['Prescribed_Ratio'],
'Total_Steps'=>$total_steps);
}
}
It is not working , what should I do guys , please help me out
where $aasan_details is the array which I showed above and the in lookup array I am getting uniqe values but not getting their total
The best solution would be to move this into a database and use a query to perform that exact operation for you.
But if you have to do it in code, you could use a keyed array to basically group them up by however many fields need to match for them to be conjoined:
e.g.
foreach ..
$consolidated[$item['Asan_Cat_Val']][$item['Asan_Sub_Cat_Val']][] = $item;
Then make a couple of nested loops to go across this and sum everything up.
There a few useful techniques to explain here...
You can reduce eye-strain in your looped array declarations by establishing a collection of keys that you know you want to retain in (copy to) the output array. This spares you having to type out each $keyName => $row[$keyName], like this (but ultimately make yourself happy).
Declare temporary associative keys (using $id) while building your output array to allow isset() to make super fast checks for a pre-existing group.
If a group is not yet set, then store the data with the new keys along with the data with the original keys. No arithmetic is necessary. The + symbols in my snippet are not "addition operators", they are "array union operators" -- they are merging the three arrays without a function call (otherwise array_merge() would do the same).
When finished iterating the array, you may choose to re-index the result (remove the temporary associative keys) by calling array_values(). If you are not bothered by the existence of these temporary keys, you can omit the function call.
If a group has been encountered before, you only need to modify the Total_steps. Use an addition-assignment operator (+=) for the briefest syntax.
Code: (Demo)
$keysToKeep = array_flip(['Asan_Category', 'Asan_Sub_Category', 'Asan_Name', 'Prescribed_Steps', 'Prescribed_Ratio']);
foreach ($array as $row) {
$id = $row['Asan_Name_Val'];
if (!isset($result[$id])) {
$result[$id] = ['Asan_Id' => $id] + array_intersect_key($row, $keysToKeep) + ['Total_Steps' => $row['Actual_Steps']];
} else {
$result[$id]['Total_Steps'] += $row['Actual_Steps'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'Asan_Id' => '447',
'Asan_Category' => 'Warm-Up',
'Asan_Sub_Category' => 'Ankle',
'Asan_Name' => 'General Ankle Warm up',
'Prescribed_Steps' => '40',
'Prescribed_Ratio' => '00',
'Total_Steps' => 22,
),
1 =>
array (
'Asan_Id' => '464',
'Asan_Category' => 'Warm-Up',
'Asan_Sub_Category' => 'Knee',
'Asan_Name' => 'General knee warm up',
'Prescribed_Steps' => '20',
'Prescribed_Ratio' => '00',
'Total_Steps' => 23,
),
)

Post array incomplete when receiving a post

I'm on a Drupal7 site, and I'm not used to Drupal. When I edit a node (standard page), and try to save it, the menu disappears. Not all node's are like this, only the ones that uses a field group of heatmaps, probably a custom field group (legacy).
System specs are:
CentOS 6.6
Apache 2.2
Mysql 5.5
Php 7
At first, I thought it was a bug in Drupal 7, and I tried solutions as Menu items disappearing in Drupal 7. But the suggested solutions didn't work. So I started to suspect post_max_size or memory_limit, because the form with the custom field grows very large when it uses the field or the field group. So I've maxed the memory settings and it looks good but is still not working.
The field group array tends to be very large and I tried to find some info about nestling level too big in a post but found no hints.
The post size is:
post_max_size in bytes = 536870912
post CONTENT_LENGTH = 1020347
The field group contains Heatmaps with Geolocations and endless of data:
[field_heatmap_data] => Array
(
[und] => Array
(
[0] => Array
(
[tablefield] => Array
(
[cell_0_0] => X
[cell_0_1] => Y
[cell_0_2] => Plastic
[cell_0_3] => Paper
[cell_0_4] => Glass
[cell_0_5] => Metal
[cell_0_6] => Organiskt
[cell_0_7] =>
[cell_0_8] =>
[cell_0_9] => Other
[cell_1_0] => 14.1741233638
[cell_1_1] => 57.7797089972
[cell_1_2] => 0
[cell_1_3] => 0
[cell_1_4] =>
[cell_1_5] =>
[cell_1_6] =>
[cell_1_7] => 1
[cell_1_8] =>
[cell_1_9] => 2
[cell_2_0] => 14.1784435935
[cell_2_1] => 57.7797106709
[cell_2_2] => 0
[cell_2_3] => 0
[cell_2_4] =>
[cell_2_5] =>
[cell_2_6] =>
[cell_2_7] =>
[cell_2_8] =>
[cell_2_9] =>
[cell_3_0] => 14.1656472109
[cell_3_1] => 57.7831198751
[cell_3_2] => 1
[cell_3_3] => 2
[cell_3_4] => 1
[cell_3_5] => 1
[cell_3_6] =>
[cell_3_7] =>
[cell_3_8] =>
[cell_3_9] =>
[cell_4_0] => 14.1753179083
[cell_4_1] => 57.7826699822
[cell_4_2] => 0
[cell_4_3] => 5
[cell_4_4] =>
[cell_4_5] => 3
[cell_4_6] =>
[cell_4_7] => 9
[cell_4_8] => 4
[cell_4_9] =>
[cell_5_0] => 14.1602465906
[cell_5_1] => 57.7824661754
[cell_5_2] => 2
[cell_5_3] => 0
[cell_5_4] => 1
[cell_5_5] =>
[cell_5_6] =>
[cell_5_7] => 4
[cell_5_8] =>
[cell_5_9] => 1
[cell_6_0] => 14.1552312791
[cell_6_1] => 57.7788985858
[cell_6_2] => 0
[cell_6_3] => 1
[cell_6_4] =>
[cell_6_5] => 1
[cell_6_6] =>
[cell_6_7] => 4
[cell_6_8] =>
[cell_6_9] =>
[cell_7_0] => 14.1631063952
[cell_7_1] => 57.7813178687
[cell_7_2] => 1
[cell_7_3] => 0
[cell_7_4] =>
[cell_7_5] =>
[cell_7_6] =>
[cell_7_7] => 2
[cell_7_8] => 3
[cell_7_9] =>
[cell_8_0] => 14.1742044644
[cell_8_1] => 57.7827544419
[cell_8_2] => 0
[cell_8_3] => 0
[cell_8_4] =>
[cell_8_5] =>
[cell_8_6] =>
[cell_8_7] => 4
[cell_8_8] => 1
[cell_8_9] =>
[cell_9_0] => 14.157952438
[cell_9_1] => 57.7818974962
[cell_9_2] => 2
[cell_9_3] => 4
[cell_9_4] => 5
[cell_9_5] => 1
[cell_9_6] =>
[cell_9_7] => 8
[cell_9_8] => 2
[cell_9_9] =>
[cell_10_0] => 14.1706946744
[cell_10_1] => 57.7815507326
[cell_10_2] => 0
[cell_10_3] => 0
[cell_10_4] =>
And so on....
So I've figured out that there is a flaw in the architecture of the node because it cant clearly not handle that much data in a field group and should been handled as an separate node, but since this is a legacy project I don't want to mess things up.
If I var_dump the $_POST variable on different pages when editing, I can clearly see that the $_POST variable stops after the $_POST['field_heatmap'] element where there is data, while the pages that doesn't contain data in that field group the $_POST array continues after the $_POST['field_heatmap'] element.
So my question is, should I continue to try to find a bug in Drupal or should I investigate further some php settings (Or maybe apache). I've tried debugging with cachegrind but can't find any unusual. Or any hints are greatly appreciated!
Finally! The max_input_vars was set to 1000
Changed it to max_input_vars = 10000 and it worked!

Fox Pro Error on getting values using MAX(date)

I'm in a bind with a deadline and I cannot seem to figure this out.
I am trying to query the table to get the values for the corresponding max shipdate. My query is below. This is for Fox Pro using an ODBC driver.
SELECT
so1.sono,
so1.custno,
so1.item,
so1.shipdate as last_shipdate,
so1.price as last_price
FROM sotran01 so1
INNER JOIN (
SELECT
custno,
item,
MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
) so2 ON (so1.custno = so2.custno AND so1.item = so2.item AND so1.shipdate = so2.last_shipdate)
WHERE so1.item IN (
SELECT item
FROM arpric01
)
ORDER BY so1.custno, so1.item, so1.shipdate
This is what I get (using ADOdb):
ADODB_vfp Object
(
[databaseType] => vfp
[fmtDate] => {^Y-m-d}
[fmtTimeStamp] => {^Y-m-d, h:i:sA}
[replaceQuote] => '+chr(39)+'
[true] => .T.
[false] => .F.
[hasTop] => top
[_bindInputArray] =>
[sysTimeStamp] => datetime()
[sysDate] => date()
[ansiOuter] => 1
[hasTransactions] =>
[curmode] =>
[dataProvider] => odbc
[hasAffectedRows] => 1
[binmode] => 1
[useFetchArray] =>
[_genSeqSQL] => create table %s (id integer)
[_autocommit] => 1
[_haserrorfunctions] => 1
[_has_stupid_odbc_fetch_api_change] => 1
[_lastAffectedRows] => 0
[uCaseTables] => 1
[_dropSeqSQL] => drop table %s
[database] =>
[host] => DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=dbf;SOURCEDB=C:\Sites\hub.fieldfresh.dev\_cache\VP10\PRAXIS\;EXCLUSIVE=NO;
[user] =>
[password] =>
[debug] =>
[maxblobsize] => 262144
[concat_operator] => +
[substr] => substr
[length] => length
[random] => rand()
[upperCase] => upper
[nameQuote] => "
[charSet] =>
[metaDatabasesSQL] =>
[metaTablesSQL] =>
[uniqueOrderBy] =>
[emptyDate] =>
[emptyTimeStamp] =>
[lastInsID] =>
[hasInsertID] =>
[hasLimit] =>
[readOnly] =>
[hasMoveFirst] =>
[hasGenID] =>
[genID] => 0
[raiseErrorFn] =>
[isoDates] =>
[cacheSecs] => 3600
[memCache] =>
[memCacheHost] =>
[memCachePort] => 11211
[memCacheCompress] =>
[sysUTimeStamp] =>
[arrayClass] => ADORecordSet_array
[noNullStrings] =>
[numCacheHits] => 0
[numCacheMisses] => 0
[pageExecuteCountRows] => 1
[uniqueSort] =>
[leftOuter] =>
[rightOuter] =>
[autoRollback] =>
[poorAffectedRows] =>
[fnExecute] =>
[fnCacheExecute] =>
[blobEncodeType] =>
[rsPrefix] => ADORecordSet_
[autoCommit] => 1
[transOff] => 0
[transCnt] => 0
[fetchMode] => 2
[null2null] => null
[bulkBind] =>
[_oldRaiseFn] =>
[_transOK] =>
[_connectionID] => Resource id #8
[_errorMsg] => [Microsoft][ODBC Visual FoxPro Driver]Syntax error.
[_errorCode] => 37000
[_queryID] =>
[_isPersistentConnection] =>
[_evalAll] =>
[_affected] =>
[_logsql] =>
[_transmode] =>
[_error] =>
)
The error doesn't say much. I can copy and paste the code into MySQL and it runs fine and returns what I expect. Hopefully another set of eyes, with more Fox Pro experience, can see what the issue is here.
Thanks for any assistance.
Your subquery is incorrect. This is the subquery:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item, last_shipdate
-------------------------------^
)
That is an aggregation column. Remove it:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
)

Sort multidimensional array (PHP) - date complications and counting

I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..

Find the nearest campground by postal code

I have a database of Postal Codes in Canada and the scripting to do my searches. But for some reason I am not get the nearest campground based on a Postal Code search. I am doing something wrong with my SQL Query as when I am selecting the campgrounds it displays a business within range by not the nearest.
When I run the SQL Query to get Postal Codes within range order by distance I get this PHP
Array (
[N0C 1K0] => 0
[N0C 1G0] => 9.36
[N0C 1L0] => 9.36
[N0C 1E0] => 9.97
[N0H 2S0] => 12.95
[N0C 1H0] => 13.01
[N0G 1R0] => 15.04
[N0C 1B0] => 19.03
[N0G 2A0] => 19.07
[N0H 1C0] => 19.55
[N0G 1N0] => 20.24
[N0C 1J0] => 21.67
[N0G 2L1] => 25.39
[N0G 2L3] => 25.39
[N0G 2L0] => 25.39
[N0H 1R0] => 25.8 campground found
[N0C 1C0] => 25.83
[N0H 2V0] => 27.43
[N0G 1C0] => 29.1
....
[N0G 1A0] => 41.54
[L0N 1G0] => 41.86
[N0G 2J0] => 41.94
[N0G 2V0] => 42.21
[L0M 1P0] => 43.21
[N0H 2P0] => 43.76
[N0G 1Y0] => 44.27
[L0M 1L0] => 44.27
[N0G 2P0] => 44.28 campground found
[L0M 1G0] => 44.48
[N4L 1R4] => 44.49 campground found
)
I would like to get the campground details for 25.8, except I am getting the one for 44.28, help!
SELECT camp_id FROM campgrounds WHERE postal = '".addslashes($key)."' LIMIT 1;
Should I have an ORDER BY? - I did try this. But maybe the wrong order by in my query.
You might want to have a look at the discussion on this SO topic it starts from a dataset of longitudes and lattitudes - but the objective is still all about proximity.
HTH
C.

Categories