Undefined offset: 0 after a loop using arrayholder in laravel 5.2 - php

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;
}
}
}

Related

PHP str_getcsv getting Undefined offset

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]];
}
}
}

Removing duplicate entries in an array- Undefined offset error(PHP)

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);

PHP function Error : Notice: Undefined offset

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.

Undefined variable for loop

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.

Notice: Undefined offset: 2

I'm new to php and I have done an example in php book. In there I got below notice. How to prevent this notice ?
<?php
require_once('AddingMachine.php');
$arrayofnumbers = array(100,200);
$objectname = new AddingMachine();
$objectname->addNumbers($arrayofnumbers);
?>
and
<?php
Class AddingMachine
{
private $total = 0;
function addNumbers(array $numbers)
{{
for($i=0;$i<=sizeof($numbers);$i++)
{
$this->total = $this->total + $numbers[$i];
}
echo $this->total;
}
}
}
Change your loop from
for($i=0; $i <= sizeof($numbers); $i++)
to
for($i=0; $i < sizeof($numbers); $i++)
Also preferable to use count.
for($i=0; $i < count($numbers); $i++)
The problem is with the <= sizeof($numbers) (which is equal to count($numbers). It will give you the total count of array elements, which is always one more than the maximum index, because arrays begin counting at 0.
Simply replace the <= with < and you'll be fine.

Categories