ChenXiu Posted August 20, 2021 Share Posted August 20, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/ Share on other sites More sharing options...
ginerjm Posted August 20, 2021 Share Posted August 20, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/#findComment-1589257 Share on other sites More sharing options...
Barand Posted August 20, 2021 Share Posted August 20, 2021 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']; Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/#findComment-1589258 Share on other sites More sharing options...
Solution kicken Posted August 20, 2021 Solution Share Posted August 20, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/#findComment-1589259 Share on other sites More sharing options...
Barand Posted August 20, 2021 Share Posted August 20, 2021 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 ?? ''; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/#findComment-1589260 Share on other sites More sharing options...
ChenXiu Posted August 21, 2021 Author Share Posted August 21, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313581-fill-array-with-to-avoid-undefined-index-errors/#findComment-1589284 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.