Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:51:17+00:00 2026-05-23T10:51:17+00:00

I’m working with a large array. We’re displaying all fields of data in the

  • 0

I’m working with a large array. We’re displaying all fields of data in the array with a table. Some fields in the array are null because the user hasn’t accumulated anything in that field. However, we wanted a zero for when they have such a result. Our solution was to display the value along with intval()

intval(@$users[$user_id]['loggedin_time'])

Which is fine, but it is ugly and inelegant. Is there a way, without a foreach loop, to set all values of ” in the array to be 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:51:18+00:00Added an answer on May 23, 2026 at 10:51 am

    Yes, with array_map:

    $input = array(...);
    $output = array_map(function($item) { return $item ?: 0; }, $input);
    

    The above example uses facilities of PHP >= 5.3 (inline anonymous function declaration and the short form of the ternary operator), but you can do the same in any PHP version (only perhaps more verbosely).

    You should think a bit about the conditional inside the callback function; the one I ‘m using here will replace all values that evaluate to false as booleans with zeroes (this does include the empty string, but it also includes e.g. null values — so you might want to tweak the condition, depending on your needs).

    Update: PHP < 5.3 version

    It’s either this:

    function callback($item) {
        return $item == false ? 0 : $item;
    }
    
    $output = array_map('callback', $input);
    

    Or this:

    $output = array_map(
         create_function('$item', 'return $item == false ? 0 : $item;'),
         $input);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.