Issue accessing an associative array php pdo - php

I'm running into an issue with work i'm trying to use the value provided by using
$row['make']
and pulling that data which is plain text out of our data base from an import. Now im running into an issue when I run this code i get this
Notice: Undefined index: FORDX in convert.php on line 203
I'm unsure why this is happening, i'm guessing it has something to do with the way i'm accessing the array.
Here's the array
$makes = array(
"ACURA" => "Acura",//does not exist
"AMRTR" => "American",
"ATLAS" => "Atlas",//does not exist
"BRRTR" => "Barrett",
"BMWXX" => "BMW", //does not exist
"BRAE" => "Brae", //does not exist
"BUDDX" => "Budd",
"BUICK" => "Buick", //does not exist
"BUTLR" => "Butler",
"CDLLC" => "Cadillac", //does not exist
"CNDCR" => "Can. Car",//does not exist
"CPCTY" => "Capacity",
"CPTRB" => "Capital",//does not exist
"CARRR" => "Carrier",//does not exist
"CHVRL" => "Chevrolet",
"CHRYS" => "Chrysler", //does not exist
"CLARK" => "Clark", //does not exist
"CLLMN" => "Collins", //does not exist
"CMMRC" => "Commercial Truck", //does not exist
"CMMTR" => "Commercial Truck", //does not exist
"CMMVN" => "Commercial Vans", //does not exist
"CRANE" => "Crane Carrier", //does not exist
"CRNFR" => "Crane Fruehauf", //does not exist
"DATSN" => "Datsun", //does not exist
"DMNDB" => "Diamond B.", //does not exist
"DMNRE" => "Diamond Reo",
"DODGE" => "Dodge",
"DORSY" => "Dorsey",
"DRBTR" => "Durabody", //does not exist
"EASTX" => "East",
"ESPAR" => "Espar", //does not exist
"FNTCN" => "Fontaine",
"FNTTR" => "Fontaine",
"FORDX" => "Ford",
"FRDLT" => "Ford",
"FRGHT" => "Freightliner",
"FRECS" => "Frght Custom Chassis",
"FRUHF" => "Fruehauf",
"FRYEX" => "Frye", //does not exist
"GMXXX" => "GM", //does not exist
"GMCCL" => "GMC",
"GMCXX" => "GMC",
"GRAY" => "Grays", //does not exist
"GRTDN" => "Great Dane",
"GRMAL" => "Grumman", //does not exist
"HEILX" => "Heil",
"HIAB" => "Hiab", //does not exist
"HGHTR" => "Highway", //does not exist
"HINMT" => "Hino", //does not exist
"HBBMF" => "Hobbs",
"HOBBS" => "Hobbs", //does not exist
"HOLND" => "Holland",
"HLLEQ" => "Holland Equipment", //does not exist
"HLLND" => "Holland Hitch Co", //does not exist
"HTCHN" => "Hutchens", //does not exist
"HTCIN" => "Hutchinson", //does not exist
"HYSTER" => "Hyster",
"INGRN" => "Ingersoll Rand",
"IHT" => "International",
"ISUZU" => "Isuzu",
"KNTCK" => "Kentucky",
"KNWRT" => "Kenworth",
"KINDRON" => "Kindron", //does not exist
"KNGTB" => "King's", //does not exist
"KRHNR" => "Krohnert",//does not exist
"LAMDR" => "Land Rover", //does not exist
"LFKTR" => "Lufkin",
"MACK" => "Mack",
"MCKTR" => "Mack",
"MACKX" => "Mack",
"MANAC" => "Manac",
"MTLTR" => "Matlock", //does not exist
"MXNIN" => "Maxon",//does not exist
"MAZDA" => "Mazda",//does not exist
"MRCBN" => "Mercedes-Benz",
"MERC" => "Mercury",//does not exist
"MCKBD" => "Mickey",//does not exist
"MILLR" => "Miller",
"MISCX" => "Miscellaneous",//does not exist
"MITSUB" => "Mitsubishi",
"FUSOX" => "Mitsubishi",
"MTSBS" => "Mitsubishi",
"MNDIN" => "Mond",//does not exist
"MONON" => "Monon",
"MORGN" => "Morgan",//does not exist
"MNCPW" => "Munchie",//does not exist
"M2" => "M2",//does not exist
"NVSTR" => "Navistar",//does not exist
"NSSDA" => "Nissan",//does not exist
"NSSMT" => "Nissan",
"OTHER" => "Other",
"OTTST" => "Ottawa",
"PALFGR" => "Palfinger", //does not exist
"PTRBL" => "Peterbilt",
"PTRSN" => "Peterson",//does not exist,
"PINES" => "Pines",
"POLAR" => "Polar",
"PONT" => "Pontiac",//does not exist
"PRVST" => "Prevost",//does not exist
"PRINC" => "Princeton",//does not exist
"RELIA" => "Reliable",//does not exist
"RENLT" => "Renault",//does not exist
"RSSIN" => "Roussy",//does not exist
"SATRN" => "Saturn",//does not exist
"STDMN" => "Steadman",//does not exist
"STRLG" => "Sterling",
"STWDR" => "Stewart",//does not exist
"STGHT" => "Stoughton",//does not exist
"STRCK" => "Strick",//does not exist
"SUPRR" => "Superior",//does not exist
"SPRRT" => "Superior Tech",//does not exist
"SUPRM" => "Supreme",//does not exist
"THRKN" => "Thermo King",//does not exist
"TIMPT" => "Timpte",
"TOYOT" => "Toyota",//does not exist
"TRLMT" => "Trailermaster",//does not exist
"TRLMB" => "Trailmobile",
"TRLCN" => "Trailmobile",
"TRNSI" => "Transicold",//does not exist
"TRNST" => "Transit",//does not exist
"TRIM" => "Trim",//does not exist
"UTILIT" => "Utilit",//does not exist
"UTLTY" => "Utility",
"VLVNA" => "Volvo",//does not exist
"VOLVO" => "Volvo",
"WABSH" => "Wabash",
"WALKR" => "Walker",
"WARNI" => "Warner",//does not exist
"WEBST" => "Webasto",//does not exist
"WSTNK" => "Westank",//does not exist
"WSTST" => "Western Star",
"WSTAR" => "Westinghouse Air",//does not exist
"WHITE" => "White"
);
Sample data from the db as i looped through the $row['make']
FORDX VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO GRTDN GRTDN
Here's the loop code
$result = $pdo->prepare("SELECT * FROM csvImport limit 20");
$data = array();
if ($result->execute()) {
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($row['status']== "P-Sale Pending")
$status = "Pend";
else
$status = "Live";
$unit = $row['unit'];//stocknumber
$make = $makes[$row['make']];
}
}

I know this is not an answer but a debugging tip - but formatting in comments is insufficient.
Try this:
if (isset($makes[$row['make']]) {
$make = $makes[$row['make']];
} else {
echo "unrecognized make for unit {$unit}: ";
var_dump($row['make']);
die();
}
...I bet there's some sort of whitespace character in one of the rows' make column...

Related

How to merge two different array PHP

I would need to combine two different fields.
In the first field I generate days of the month. I want to list all days of the month.
I would like to add a second field to them, where there are items for each day. But, for example, there are no items on weekends or on other days. Ie. that field two will always have fewer items.
The second field is tightened from the DB.
I would need to do a JOIN like in MySQL for the first field.
It occurred to me that in MySQL it would be possible to make a temporary table with a given month and link it here, but I don't think it's right.
$arrayDate = [0 => '20210401',1 => '20210402',2 => '20210403',3 => '20210404',4 => '20210405',5 => '20210406',6 => '20210407',7 => '20210408',8 => '20210409',9 => '20210410',10 => '20210411',11 => '20210412',12 => '20210413',13 => '20210414',14 => '20210415',15 => '20210416',16 => '20210417',17 => '20210418',18 => '20210419',19 => '20210420',20 => '20210421',21 => '20210422',22 => '20210423',23 => '20210424',24 => '20210425',25 => '20210426',26 => '20210427',27 => '20210428',28 => '20210429',29 => '20210430'];
$arrayItem[35] = ['id' => 35, 'date' => '20210401', 'item' => 'aaaa'];
$arrayItem[36] = ['id' => 36, 'date' => '20210402', 'item' => 'bbbb'];
$arrayItem[37] = ['id' => 36, 'date' => '20210430', 'item' => 'cccc'];
// i need output
20210401 - aaaa
20210402 - bbbb
20210403 - empty
20210404 - empty
...
20210430 - cccc
EDIT: I use nested loops, but I still can't get the right output
foreach ($arrayDate as $date) {
foreach ($arrayItem as $item) {
if ($date == $item['date']) {
bdump($item['date']);
} else {
bdump($date);
}
}
}
bdump($item['date']) = '20210401', '20210402', '20210430'
bdump($date) = '20210401', '20210401', '20210402', '20210402', '20210403', '20210403', '20210403', '20210404', '20210404', '20210404', '20210405', '20210405', '20210405' ....
With array_column you create a array from $arrayItem with date as key.
$dateItem is an array like
array (
20210401 => "aaaa",
20210402 => "bbbb",
20210430 => "cccc",
)
The output you can do with a simple foreach.
$dateItem = array_column($arrayItem,'item','date');
foreach($arrayDate as $date){
echo $date.' '.($dateItem[$date] ?? 'empty')."<br>\n";
}
Note:
With
array_column($arrayItem,null,'date')
you get a two-dimensional array with a date as a key that can be used.
array (
20210401 =>
array (
'id' => 35,
'date' => "20210401",
'item' => "aaaa",
),
20210402 =>
array (
'id' => 36,
'date' => "20210402",
'item' => "bbbb",
),
20210430 =>
array (
'id' => 36,
'date' => "20210430",
'item' => "cccc",
),
)

search array with multitext value

in this array, I want to find word "in" from value and make new array from that songs including number (key) songs from album.
$londonCalling = array(
'song-01' => 'London Calling',
'song-02' => 'Brand New Cadillac',
'song-03' => 'Jimmy Jazz',
'song-04' => 'Hateful',
'song-05' => 'Rudie Cant Fail',
'song-06' => 'Spanish Bombs',
'song-07' => 'The Right Profile',
'song-08' => 'Lost in the Supermarket',
'song-09' => 'Clampdown',
'song-10' => 'The Guns of Brixton',
'song-11' => 'Wrong Em Boyo',
'song-12' => 'Death or Glory',
'song-13' => 'Koka Kola',
'song-14' => 'The Card Cheat',
'song-15' => 'Lovers Rock',
'song-16' => 'Four Horsemen',
'song-17' => 'Im Not Down',
'song-18' => 'Revolution Rock',
'song-19' => 'Train in Vain');
This can be done with preg_grep oneliner:
print_r(preg_grep('/in/', $londonCalling));
alternatively array_filter
$res = array_filter($londonCalling , function($e){
return strpos($e, 'in') !== false;
});
print_r([$res]);

Laravel 5: return number of affected rows PGSQL

I have such a query and I want to get how many records have been added or updated.
$insertData = Lpis::UpdateOrCreate(['objectid' => $insert[$i]['objectid']],['identyfikator' => $insert[$i]['identyfikator'],'powierzchnia' => $insert[$i]['powierzchnia'],'teryt' => $insert[$i]['teryt'],'numer' => $insert[$i]['numer'],'wojewodztwo' => $insert[$i]['wojewodztwo'],'powiat' => $insert[$i]['powiat'],'gmina' => $insert[$i]['gmina'],'data_od' => $insert[$i]['data_od'],'shape_leng' => $insert[$i]['shape_leng'],'shape_area' => $insert[$i]['shape_area']]);
Anyone help ?

MongoDb aggregate error "each element of the 'pipeline' array must be an object"

Here is my aggregation code for mongodb collection :
$mongo_db_name = "db_".$tenant_id;
$con = new MongoClient($this->config->item('mongo_instance'));
$db = $con->$mongo_db_name;
$collection = $db->$module;
$options = array('cursor' => array("batchSize" => 4 ));
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
$data = $collection->aggregate($pipeline,$options);
var_dump($data);
I am getting this error with the above code
array (size=4) 'ok' => float 0 'errmsg' => string 'Each element of
the 'pipeline' array must be an object' (length=54) 'code' => int 14
'codeName' => string 'TypeMismatch' (length=12)
If I try to encode the pipeline using json_encode the result is 'null'.
If I use find(array('deleted'=>'0')); it returns all documents as expected.
Can anyone help me about where I am going wrong or whatmust I do to resolve this ?
aggregation pipeline takes objects in array so your pipeline should be like
[
{$match:{}};
{$group:{}};
{$somepipelinestageOperator:{}}
]
but if you have pipeline with array as tages and not object you will get above error(each element of pipeline must be an array..).
[
[$match:{}],
[$group:{}],
[$somepipelinestageOperator:{}]
]
in your code there are array for match and group so its failing.
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
you can print your pipeline and see that your pipeline stages are having array and not object.

Displaying one string from a list of multiple strings

I'm trying to grab data from this array, but when I do var_dump($ArrayedLevels['Attack']); it returns NULL, I know for a fact that it was able to grab the data from the SQL database, I think it has something to do with my array. Any help would be greatly appreciated.
include("highscoresconfig.php");
$GrabXP = $database2->prepare("SELECT * FROM `skills` WHERE `playerName` = ?");
$GrabXP->execute(array($playerName));
$MainResult = $GrabXP->fetchAll();
$ArrayedLevels = $array = [
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
];
var_dump($ArrayedLevels["Attack"]);
Problem might be because the fetchAll() returns all the rows of your query.
if($GrabXP->execute(array($playerName))){
//Success
$MainResult = $GrabXP->fetchAll();
/*
This will give you all the rows. Use a foreach loop to iterate through all the rows.
If you want only the first row, add this-
*/
$MainResult = $MainResult[0];
//The rest of your code.
$ArrayedLevels = $array = Array(
"Attack" => $MainResult['Attacklvl'],
...
);
var_dump($ArrayedLevels["Attack"]);
}
else{
//Failure
}
The brackets don't work in PHP when creating a new array, use array() to create one.
Actually: as of PHP 5.4 they do work, see here: http://nl3.php.net/manual/en/language.types.array.php
Try var_dump($MainResult);
if all good, try create your array like this:
$ArrayedLevels = array(
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
);
first of all try to print the structure of the variable $MainResult:
var_dump($MainResult);
FetchAll return an array with all restuls like
resutl = [
[0] => ['first row'],
[1] => ['sec. row']
]
you will See The variable $MainResult looks like:
MailResult => [
[0] => ['Attacklvl' => 'foo'],
[1] => ['Defencelvl' => 'bar']
]

Categories