0

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
gitguddoge
  • 172
  • 1
  • 9

6 Answers6

4

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

mleko
  • 11,650
  • 6
  • 50
  • 71
3

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

echo $array1[1] ?? "Nothing here!";
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
3

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";
Naruto
  • 1,210
  • 3
  • 25
  • 28
  • I guess I didn't consider well into this because I used this in the same project by checking whether the _string-based key_ exists in the array or not... Thanks ya! – gitguddoge Aug 14 '18 at 08:54
  • If they are string based keys, you just add " " around the key – Naruto Aug 14 '18 at 09:47
1

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]);
  • Thanks! This is exactly the answer I was looking for. I knew it would come out the warning notice but I just want to simply ignore it! – gitguddoge Aug 14 '18 at 07:52
  • 5
    Using @ is not a good practice since it suppresses the error or warning which will be hard to debug for developer. – Afraz Ahmad Aug 14 '18 at 07:52
  • You are welcome, @AfrazAhmad here the developper assume the responsability, its an focntionallity who offer PHP for a lot of situations – Mohamed El Mrabet Aug 14 '18 at 07:57
  • There is other solution like array_key_exists or other @gitguddoge if you want we can chat and i will give you other solution. – Mohamed El Mrabet Aug 14 '18 at 07:59
  • @MohamedElMrabet really thanks for this answer but mleko's answer is better in this way. I'll still upvote for your answer anyway! – gitguddoge Aug 14 '18 at 08:06
  • You can use this native function http://php.net/manual/fr/function.array-key-exists.php its more better if you want other solution – Mohamed El Mrabet Aug 14 '18 at 08:09
0

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]);
Ray A
  • 1,283
  • 2
  • 10
  • 22
-2

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] = '...'
Afraz Ahmad
  • 5,193
  • 28
  • 38