Php replace null in array

Replacing null values in PHP array

The $value you currently have in if ($value == null) is not the actual array value, but a copy created by foreach .

You should also use a strict null check: if ($value === null)

You should unset the reference after the foreach loop. (See the Warning box in the foreach documentation.)

Another possible way to accomplish this is to use an expression in your query to returns zeroes instead of nulls. That will vary depending on which database you’re using.

More Answer

  • Sort values inside an Array on PHP
  • PHP Object has values but when I try to use it in FOREACH() I am getting NULL value
  • remove values from an array php
  • PHP — Is there a way to verify all values in an array
  • Join array values and keys between two php arrays
  • Using foreach loop to echo array values in PHP
  • Finding same values on multiple array data PHP
  • PHP Get values from nested array
  • How to solve error: Trying to access array offset on value of type null with Prepared Statement PHP
  • PHP Multidimensional Array Combine Values With The Same Index Number
  • How to get values from array object in PHP
  • PHP Bitwise compare values from database with an array
  • PHP check if array contains multiple values
  • PHP how to arrange array key values based on date
  • PHP Find keys in one array that exist as values in another array, and pull back the value
  • PHP — performant way to remove duplicate values from a huge multidimensional indexed array
  • PHP using preg_match to match items in array with values that can or not contain accent characters
  • Print values ​in an index whose array has variable parts PHP
  • How to remove duplicate values from a multi dimensional array for specific key in php
  • Merge PHP array having same keys different values
  • php associative array : compare adjacent values
  • How to get all values inside multidimensional array in PHP
  • Updating value in array updates all values with the last inserted value PHP
  • PHP Push simple array values in multidimensional array not working
  • PHP create new arrays based on the values of another array
  • Find array key and get values in php
  • Get values of multiple select boxes in PHP and add them to an array
  • Extract same key from array and count same values in new array php
  • PHP parse array and count values in new array
  • PHP remove duplicate array values
  • Php Array not Working With Varible But Working With Direct Values
  • Compare multidimensional array values by key where child arrays can be empty or different in php
  • How can I code this script better? Search & extract values from an array in PHP
  • Combining PHP array values based on the start of a string
  • How to use for loop to print values from array in mpdf PHP
  • How to push values form a php associative array to jquery array?
  • searching inside array values to get primary key in php
  • Can’t assigning values to error array to handle errors in PHP
  • add values to first array for each matching ID in the second array in PHP
  • find the greatest key value and return other keys and its corresponding values from multidimensional array in php
  • Return all node values with their parents in PHP multidimensional array
  • How to print multidimensional array values in php
  • php array access first element return null
  • take several values in a multidimensional php array
  • Sort array without replacing index elements in php
  • PHP — Get all values that has been checked in checkbox and combine it in one array
  • PHP — Get a values from a multidimentional array and appending them as a string
  • Sort or display php array values as a bell curve
  • PHP update array with values and keys from another array
  • Showing word ‘Array’ for each array values instead of value itself inside foreach loop — php
Читайте также:  Enable javascript for mac

More answer with same ag

  • PHP 7.0 — rounding and/or casting float to int gives incorrect values
  • Urls for results of Ajax requests
  • PHP multidimentional array has all subarrays of another multidimentional array
  • Identify a word and extract data corresponding to it
  • PHP Regex: find many words between 2 parentheses
  • Combine two values of a single array
  • Symfony2 Service Class. Function Call
  • PHP 7.4: How to check if typed property is initialized (including initialization with null)?
  • Ajax Search as user types
  • Do I need to initialise every key of a multidimensional array in PHP?
  • echo or include multiple xml files in php page
  • How can I search for value to get key and use that key to get another key in a different array?
  • PHP sending JSON via CURL POST
  • PHP removing an object from array returns empty array
  • PHP select row from session[a] array to store session[b]
  • Is there a way to compare two strings where one is the parent string and the other a child string and make bold similar substrings?
  • 403 Forbidden Error on Apache2 server
  • i have add time query while fetching data but it says no data found in php
  • Undefined offset error in php?
  • Can’t verify the form details with database using php
  • To get many tags for a question effectively from Postgres by PHP
  • how to display data from database on full calendar?
  • PHP Passing variable in URL
  • how to assign a variable to a html code including an if statement in php
  • Get only letters in a string INCLUDING whitespace
  • How curl php this is bash
  • Json Encode working in one server and on another giving error undefined
  • heredoc with eval code execution
  • Run javascript function on specific URL dir (htaccess)
  • PHP search a string from an array for match using preg_filter
  • Angular post method to php
  • Not able to get post data from ajax to php
  • How do I get this php statement that gets all the values from database and outputs as checkboxes to work? (PHP/Mysql)
  • Modify all queries in Zend\Db (add where)
  • array of special characters to replace in a text, php, json
  • How to put POST form data in action
  • Echo only a part of an URL (string)
  • undefined variable in php
  • What is faster in php: syslog, file append or error_log
  • Validate checkbox in jquery
  • PHP: sort ZIP in alphabetical order
  • Easiest way to create drop down with database generated options
  • htaccess wasn’t working on online server?
  • Symfony2 buildForm — adding entity type input with count
  • How do I install Imagick for PHP without PEAR?
  • Load different PHP files based on JavaScript availability
  • Consistency around the .phar extension in PHP
  • ordering array keys?
  • Trouble validating md5 hashed password with randomly generated salt?
  • Phalcon Incubator not loading translation namespace
Читайте также:  Создать резюме на html

Источник

Replace all `null` values in an array in PHP

To replace all occurrences of null in an array with another value you can use the array_map() function that is built into PHP. This function applies a callback function to each item in your array and returns a new array with all the return values of that callback function.

As a first argument array_map() takes the callback function, as a second argument it takes an array of values on which the callback function should be applied.

To replace all null values by another value, the letter «c» in this example, you can take the following snippet:

<?php $REPLACEMENT = 'c'; $values = ['a', 'b', null, 'd']; $result = array_map( fn ($value) => $value ?? $REPLACEMENT, $values ); print_r($result); // Array // ( // [0] => a // [1] => b // [2] => c // [3] => d // ) 

Keep in mind here that in the above snippet I’m using two features that are at the time still fairly recent: the null coalescing operator and an arrow function.

The null coalescing operator returns the first operand, except for when it is null in which case it falls back to the second operand.

Источник

Replace null array value from array in php

I run a query and got id values in an array. Then I push those id like this array_push($id); then I implode ‘,’ into an array and place it in a variable like this $val = implode(«,»,$id); . When I output it i get following result ,2,3,4,5,6,7,8 . Problem in this result is that first value is ‘,’ and it is because first record that I am getting from database is giving me Null value. I want to remove ‘,’ from first place if first record is empty. So that desire result should be 2,3,4,5,6,7,8 .

Please anybody can help me how can I remove ‘,’ from first place. Or any suggestion to solve this problem

Answer

Solution:

$id = array_filter($id, 'ord'); // removes 0 length values, or $id = array_filter($id, 'strlen'); // same as above but a bit slower, or $id = array_filter($id, 'count'); // removes only NULL values and empty arrays $val = implode(",",$id); 

You might also want to consider using is_numeric as the array_filter callback.

Doing just array_filter($id) will remove falsy (like 0 ) elements that you might want.

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Оцените статью