Iterating through 3 loops of different length - PHP - php

I have 2 arrays and i want to make a 3rd array after comparison of the 2 arrays. Code is as follows:
foreach($allrsltntcatg as $alltests)
{
foreach($alltests as $test)
{
foreach($allCatgs as $catg)
{
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
$catcounts[$catg['testcategoryname']] +=1;
}
}
}
}
It, although returns the right answer, it also generates a PHP error and says undefined index and prints all errors and also the right answer.
I just want to avoid the array out of bound error. Kindly help me

Problem is in if condition correct like below : You have to initialize array first and than you can increment value
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
if (isset($catcounts[$catg['testcategoryname']]))
$catcounts[$catg['testcategoryname']] +=1;
else
$catcounts[$catg['testcategoryname']] =1;
}

When the array try to add some arithmetic operation of undefined index such as $catg['testcategoryname'] in the $catcounts array then the warning generates. Before add the number you have to check the index is present or not, and of not then just assign value otherwise add into it.
So do it in this way just if condition-
if(....){
if(array_key_exists($catg['testcategoryname'], $catcounts))
$catcounts[$catg['testcategoryname']] +=1; // Add into it
else
$catcounts[$catg['testcategoryname']] = 1; // Assign only
}
More about array key exists--See more

$catg['testcategoryname'] should represent an index in $catcounts array.

Related

PHP: how to condition max(array_filter) & min(array_filter) if empty

I have created an array using compact:
$rank_month = compact('jan','feb','mar');
Like this and the data will be fetch using queries and due to that the data will sometimes will be empty and the data will be numbers.
And then I'm using the max(array_filter) and min(array_filter) to get the highest and lowest in the array but when the query is empty I get the
ERROR: max(): Array must contain at least one element
ERROR: Min(): Array must contain at least one element
is it possible to condition it like:
if(empty(max(array_filter('$rank_month')))){
$high = ""; }
else{
$high = max(array_filter('$rank_month'
}
or is their any way to fix this error? Even if the data is empty
Thanks.
You are passing array_filter a string when it actually wants an array. '$rank_month' is an 11-character string, it's not an array.
You want to make sure you pass the array to array_filter (as well as min and max).
You also want to be checking the length of the filtered array before calling min/max.
$filtered_month = array_filter($rank_month);
if(!empty($filtered_month)){
$high = max($filtered_month);
}
else{
$high = '';
}
simply what the error says: $rank_month is an empty array
use sizeof or count
if (sizeof($rank_month) > 1) {
$high = max($rank_month);
} else {
...
}
Or
if (count($rank_month) > 1) {
$high = max($rank_month);
else {
...
}
You need to check if the Array is empty, not if the max value is empty.
You also don't need to pass in array_filter, the array itself should work.
if(empty($rank_month)){
$high = "";
} else {
$high = max($rank_month);
}

Undefined Index in php array

Looking to get a count of particular key=>values in an multi dimensional array. What I have works i.e. the result is correct, but I can't seem to get rid of the Undefined Index notice.
$total_arr = array();
foreach($data['user'] as $ar) {
$total_arr[$ar['city']]++;
}
print_r($total_arr);
Any ideas? I have tried isset within the foreach loop, but no joy...
$total_arr = array();
foreach($data['user'] as $ar) {
if(array_key_exists($ar['city'],$total_arr) {
$total_arr[$ar['city']]++;
} else {
$total_arr[$ar['city']] = 1; // Or 0 if you would like to start from 0
}
}
print_r($total_arr);
PHP will throw that notice if your index hasn't been initialized before being manipulated. Either use the # symbol to suppress the notice or use isset() in conjunction with a block that will initialize the index value for you.

shortcut to validate if an array exists and contains no values

What is a shortcut to validate if an array exists and contains no values?
for some reason, this looks weird
$warning = array();
if (isset($warning) && empty($warning)) {
//go on...
} else {
//either the array doesn't exist or it exist but contains values...
}
the array needs to exist and must contain no values
That is the shortest you will be able to get it if you do not know whether or not the variable is defined.
If you always go about defining the array ($warning = array()), you could skip the isset step.
First, check if the array object itself is allocated, then the individual indexes for allocation.
if ($warning) {
...
}
Would not that work? Of course before checking this you are probably assigning something to it.
Addendum:
This code outputs no, without even having the array initialized.
if ($warning) echo "yes";
else echo "no";

Deep array !empty check in php

I need to check array value, but when array is empty, I get this: Error: Cannot use string offset as an array
if (!empty($items[$i]['tickets']['ticket'][0]['price']['eur'])) { //do something }
How to do it correctly?
You need to check if the variable is set, then if it is an array and then check if the array's element is set. The statements of the if will be executed in order and will break when one is false.
if(isset($items) && is_array($items) && isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) {
//jep it's there
}
Or just try it (extra sipmle variant):
if (!isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) {
// do action
}

php array error - scalar value

Sorry guys, not being lazy, I know others have had the same error message solved but I still couldn't figure it out so I had to ask.
I have 2 2d arrays each with a string and a corresponding int.
I'm trying to compare the strings in the first array to the strings in the second and if they are the same, i want to add the corresponding integers together.
I am getting the error - "Cannot use a scalar value as an array" on the 7th line
for($countOne=0; $countOne<10; $countOne++)
{
for($countTwo=0; $countTwo<10; $countTwo++)
{
if($blekko_Array['url'][$countOne]==$bing_Array['url'][$countTwo])
{
$blekko_Array['score']['$countOne'] = $blekko_Array['score']['$countOne'] + $bing_Array['score']['$countTwo'];
}
}
}
Anyone know what the problem is?
Thanks
This should do it:
foreach ($blekko_Array as &$blekko) {
foreach ($bing_Array as $bing) {
if($blekko['url']==$bing['url']) {
$blekko['score'] += $bing['score'];
}
}
}
For one thing, your code is hardwired to look through 10 items, so if your array has less than 10 entries, you'll get errors on the missing ones.

Categories