Jump to content

fill array with " " to avoid "undefined index" errors?


Go to solution Solved by kicken,

Recommended Posts

Sometimes I receive arrays from a 3rd party with NULL values.
This causes "undefined index" errors on my end unless I check each and every value beforehand.
(Checking each of those with "if !empty" gets really time consuming and makes my code messy when there's lots of values to check.)
Someone on this forum posted a line of code that added " " to fill any NULL values found in arrays.
I can't remember what they posted. I can't find that forum post, and I can't find the answer on google anywhere (and of course, I've tried every permutation of "array_fill" that I could think of).
1.) What's that line of code?
2.) Is doing it this way a good idea?

Thank you.

Is the problem having elements with null values or is it that the element does not exist in the array?  You make it sound like the latter but are treating it as if it were the former.

If you are getting undefined index errors, then the element is not there.  Perhaps you could just add a check using isset() to avoid the index error?  Otherwise you would have to have some code that knows what elements must exist and have to go thru the array making sure that they are present and creating them if not.

 

Not sure what you are doing with your array to get those undefined index errors.

This code doesn't output much but it doesn't give undefined index errors...

$myarr = [ 'a' => null, 'b' => null, 'c' => null ];
echo $myarr['a'];
echo $myarr['b'];
echo $myarr['c'];

 

  • Solution
1 hour ago, ChenXiu said:

I've tried every permutation of "array_fill" that I could think of

Ah, but have you tried array_fill_keys?  See also, this post: Notice: Undefined index: driving me insane.

If you do want to replace null values with, say, '' (an empty string), then you could do this (usng my above array)...

$myarr = array_map('denullify', $myarr);

function denullify($v)
{
    return $v ?? '';
}

 

  • Like 1
20 hours ago, kicken said:

Ah, but have you tried array_fill_keys?

Yes! That's the post I tried to find! Thank you!! 😀

Regarding my original post... I know I conflagrated a couple things. Sometimes the arrays are actually missing the keys entirely, and sometimes when there are keys, the values are null. One of the cURL results I use presents arrays that are sometimes missing certain values I need, and sometimes are missing the entire key/value pair.
The only way (up till now) to avoid all PHP errors on my end is to initialize every variable that I plan on using like this:
$someVar = $curl_result["data"]["some_value"]["what_i_need"] ?? "";
$anotherVar = etc., etc., etc.,
And it ends up being a big long list of definining every variable to prevent PHP errors.
And long lists in my code make it hard to write code because it makes me have to scroll through so many lines to look for things...

19 hours ago, Barand said:

If you do want to replace null values

Thank you, this "array_map" function seems like a very useful thing! I'm reading up on it now.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.