gitguddoge
gitguddoge

Reputation: 172

Is it possible to avoid the warning of undefined offset PHP array?

I know the undefined offset gives out warning instead of error but I want to know is there any possible way to avoid this kind of warning to pop out? I intend to write a function to test the undefined offset because I think that writing multiple similar if-else condition to test offset could be much work to be done.

function testOffset($item){
        if(isset($item)){
            return $item;
        }else{
            return "Nothing here!";
        }
    }
$array1[] = "Hello!";
echo testOffset($array1[1]);

In this case the function is work well but the warning will also pop out the moment I assign the unset element into function. Anyway to work around with it?


I purposely set the checking index to 1 to prove the function is working well

Upvotes: 0

Views: 5019

Answers (6)

mleko
mleko

Reputation: 12243

You can try function like this

function issetOr($arr, $key)
{
    if (isset($arr[$key])) {
        return $arr[$key];
    } else {
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo issetOr($array1, 1);

Or if you wan't to check for key existence use

function issetOr($arr, $key)
{
    if (\array_key_exists($key, $arr)) {
        return $arr[$key];
    } else {
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo issetOr($array1, 1);

Demo online

Upvotes: 4

Naruto
Naruto

Reputation: 1200

There are multiple ways of handling this problem, but depending on what you are trying to do you could do:

foreach($array as $key => $val) {}

If you are just trying to get 1 element based on the key, you should just go with array_key_exists:

$array1[] = "Hello!";
echo (array_key_exists(1, $array1)) ? $array[1] : "No key";

Upvotes: 3

Nigel Ren
Nigel Ren

Reputation: 57131

If your using PHP 7+ you can use null coalesce to make the whole thing just a one liner...

echo $array1[1] ?? "Nothing here!";

Upvotes: 4

Mohamed El Mrabet
Mohamed El Mrabet

Reputation: 633

For escape warning and notice in php you can use simply an @

function testOffset($item){
    if(isset($item)){
        return $item;
    }else{
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo @testOffset($array1[1]);

Upvotes: 1

Afraz Ahmad
Afraz Ahmad

Reputation: 5396

You can handle this by php array_values builtin function:

  1. $yourArray = array_values($yourArray);

This will rearrange your array indices e.g. You have a array like this:

 $a[0] = '...'
 $a[4] = '...'
 $a[7] = '...'
 $a[8] = '...'
 $a[9] = '...'

after using array_values

 $a[0] = '...'
 $a[1] = '...'
 $a[2] = '...'
 $a[3] = '...'
 $a[4] = '...'

Upvotes: -2

Ray A
Ray A

Reputation: 1351

Because you are using offset 1, which is not exist,

you can do a simple code fixing by using the below:

function testOffset($item){
        if(isset($item)){
            return $item;
        }else{
            return "Nothing here!";
        }
    }
$array1[] = "Hello!";
echo testOffset($array1[0]);

Upvotes: 0

Related Questions