I'm having a lot of trouble getting rid of this Undefined offset notice while using str_getcsv to read a CSV file and assign keys to it.
I've tried isset and array_key_exists which has been mentioned in other threads. isset did resolve undefined index notices but I can't remove the undefined offset notices:
$file = file("lookup.csv",FILE_SKIP_EMPTY_LINES);
$csv = array_map("str_getcsv",$file, array_fill(0, count($file), ';'));
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
$numberOfRows = count($csv);
$numberOfColumns = count(current($csv));
for ( $i = 0; $i <= $numberOfRows; $i ++ ) { // first loop to run thru to find correct sku template name in csv file
if ( $csv[$i]['sku'] === $sku ) { // if sku matches // Getting Undefined offset on this line
for ( $j = 0; $j <= $numberOfColumns; $j++) { // second loop to create variables from header line
${$keys[$j]} = isset($csv[$i][$keys[$j]]) ? $csv[$i][$keys[$j]] : ''; // // Getting 2 Undefined offset notices on this line
}
}
}
The CSV file in question does have some blank cells. Those blank cells will be maintained (I can't do anything about that).
Please can anyone point me in the right direction? Any help is much appreciated, thank you!
Try to remove the last element in the array, it sometimes is an empty item as an extra line in the file is picked up due to a new line character at the end of the actual last line.
$csv = array_map("str_getcsv",$file, array_fill(0, count($file), ';'));
array_pop($csv);
Ok, well I actually managed to figure it out myself.
The solution was to add isset - just in case it helps anyone else, here is my solution:
for ( $i = 0; $i <= $numberOfRows; $i ++ ) {
$csv[$i]['sku'] = isset($csv[$i]['sku']) ? $csv[$i]['sku'] : ''; // <-- isset added
if ( $csv[$i]['sku'] === $sku ) {
for ( $j = 0; $j <= $numberOfColumns; $j++) {
$keys[$j] = isset($keys[$j]) ? $keys[$j] : ''; // <-- isset added
$csv[$i][$keys[$j]] = isset($csv[$i][$keys[$j]]) ? $csv[$i][$keys[$j]] : '';
${$keys[$j]} = $csv[$i][$keys[$j]];
}
}
}
Related
Forgive the word game.
I have to cycle an array and sum the values, then multiply for the price (i already know how to do that).
The object is composed of 64 fields called val1, val2, val3 etc .. each field has a simple_array with the value of the quantity.
I get these data from the database using doctrine.
$item = $this->getDoctrine()->getRepository(ExpertationsAdvanced::class)->findBy(['father' => $id]);
dump($item[0]->getVal1());
for($i = 1; $i < 64; $i++) {
dump(${'$item[0]->getVal' . $i . '()'});
$i++;
if(${'$item[0]->getVal' . $i . '()'} == null) {
$return = '0';
} else {
$return = array_sum(${'$item[0]->getVal' . $i . '()'} );
}
dump($return);
}
the first dump return the array i'm requesting for, with no problems, but in the forloop, i get erro Notice: Undefined variable: $item[0]->getVal1().
I think I'm using the wrong logic but maybe worked so mutch time and can't see a way.
First of all if you have 64 fields, you should have(read docs for more info):
for($i = 1; $i <= 64; $i++) {
Secondary, you don't need to increment $i inside of loop
$i++;
To get the value:
$item[0]->{"getVal{$i}"}();
// OR
$method = "getVal{$i}";
$item[0]->$method();
Result will be:
for($i = 1; $i <= 64; $i++) {
$array = $item[0]->{"getVal{$i}"}();
$return = is_array($array) ? array_sum($array) : 0;
dump($return);
}
please help me figure this out. I just want to get the total quantity of the same inventory_record_id, as the image is shown below. And I wanted to store that total to an array name $salesQtyArrayHldr. However, it always return the undefined offset 0 error. Please help. Here is my code.
<?php
$salesQtyArrayHldr = array();
?>
#foreach($inventory as $val)
<?php
for($i = 0; $i < count($val->sales); $i++){
if($val->id == $val->sales[$i]->inventory_record_id ){
$salesQtyArrayHldr[$i] += $val->sales[$i]->quantity;
}
}
var_dump($salesQtyArrayHldr);
?>
Error
So check if isset:
if (isset($val->sales[$i])) {
// action...
}
Probably there is no such key in array $val->sales
Use helper dd() to check the structure of this variable.
for($i = 0; $i < count($val->sales); $i++){
if($val->id == $val->sales[$i]->inventory_record_id ){
if(isset($salesQtyArrayHldr[$val->id])){
$salesQtyArrayHldr[$val->id] += $val->sales[$i]->quantity;
}else{
$salesQtyArrayHldr[$val->id] = 0;
$salesQtyArrayHldr[$val->id] += $val->sales[$i]->quantity;
}
}
}
I am new to PHP and am trying to remove duplicate entries in an array. I end up with my desired output, but I am also getting two "Undefined offset" errors along the way. This is the code I have:
$this->master refers to an array declared in the beginning of the class.
public function removeDuplicates(){
$var = count($this->master);
for($i = 0; $i < $var; $i++){
for($j = 0; $j <$var; $j++){
if(($this->master[$i] == $this->master[$j]) && $i != $j){
$this->shiftLeft($j, $var);
$var --;
}
}
}
}
public function shiftLeft($t, $s){
while($t < $s){
echo "$t ";
$this->master[$t] = $this->master[$t+1];
$t++;
}
unset($this->master[$t-1]);
}
It is probably a really simple logical error but I cannot seem to find where. Any help is greatly appreciated.
See if it works
$unique = array_unique($this->master);
I have simple function but got the error message : Notice: Undefined offset: 3 in
i checked the problem is in line : $newValue += $value[$i];
the answer of below function is "6" but error message still displayed.
<?php
function countMe($value) {
$newValue = 0;
for ($i=0; $i<=count($value); $i++)
{
$newValue += $value[$i];
}
return $newValue;
}
$value[0] = 1;
$value[1] = 2;
$value[2] = 3;
echo countMe($value);
?>
I know the solution is just Suppress the error with the # operator such as #countMe($value) but i want to know What's wrong with this function ?? Any help would be appreciated. Thanks
Indexes are from 0 to 2... your function should count to count() - 1:
for ($i=0; $i<=count($value)-1; $i++)
or
for ($i=0; $i<count($value); $i++)
By the way... just use array_sum($value) to get the same result :)
Please see that the value of count($value) is 3.
When you're mentioning <= in the loop, it's trying to access $value[3] but there's no such index available.
You could simply change it to <.
Another approach would be to use foreach instead. That provides more flexibility.
for ($i = 0; $i< count($value); $i++)
OR
foreach ($value as $v) {
$newValue += $v;
}
Hope this helps.
I have a code in use and it generates a PHP error in the second "for" loop.
PHP Notice: Undefined variable: newmatches
if (empty($result['ERR'])) {
preg_match_all('(<h3><a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>)siU', $result['EXE'], $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$matches[1][$i] = urldecode($matches[1][$i]);
preg_match_all('/\*\*(http:\/\/.*$)/siU', $matches[1][$i], $urls);
$newmatches[1][$i] = $urls[1][0];
}
for ($i = 0; $i < count($newmatches[1]); $i++) { //PHP Notice: Undefined variable: newmatches
if(strstr($newmatches[1][$i], $domain))
return $i+1;
}
} else {
return '0';
}
Thank you in advance!
I don't see anywhere where $newmatches would be set, aside from the first for loop, which won't run if count($matches[1]) is 0.
Not sure what all this hardcoding of your index to 1 is about, but a simple fix is to set $newmatches[1] = array() before the first loop.