84399

Given an array of arrays, how can I replace all empty values with 0?

Question:

Example array

$myArray[0] = array('23', null, '43', '12'); $myArray[1] = array(null, null, '53', '19'); $myArray[2] = array('12', '13', '14', null);

All nulls should be replaced with 0. I was hoping someone would have an efficient way of doing this, perhaps a built in PHP function that I am unaware of.

Answer1:

You could use the <a href="http://fr2.php.net/manual/en/function.array-walk-recursive.php" rel="nofollow"><strong>array_walk_recursive</strong></a> function, with a callback function that would replace null by 0.

<br /> For example, considering your array is declared this way :

$myArray[0] = array(23, null, 43, 12); $myArray[1] = array(null, null, 53, 19); $myArray[2] = array(12, 13, 14, null);

<em>Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.</em>

<br /> You could use this kind of code :

array_walk_recursive($myArray, 'replacer'); var_dump($myArray);

<br /> With the following callback functon :

function replacer(& $item, $key) { if ($item === null) { $item = 0; } }

Note that :

<ul><li>the first parameter is passed by reference ! <ul><li>which means modifying it will modify the corresponding value in your array</li> </ul></li> <li>I'm using the === operator for the comparison</li> </ul>

<br /> And you'd get the following output :

array 0 => array 0 => int 23 1 => int 0 2 => int 43 3 => int 12 1 => array 0 => int 0 1 => int 0 2 => int 53 3 => int 19 2 => array 0 => int 12 1 => int 13 2 => int 14 3 => int 0

Answer2:

If the single quotation marks are unintentional, and the arrays have integers and null values:

for ($i = 0; $i < count($myArray); $i++) { if ($myArray[$i] == null) $myArray[$i] = 0; }

Recommend

  • javascript: smallest JSON.stringify for Float32Array?
  • Find and remove parameter declaration inside Expression.Block
  • Batch - String replacement with special characters
  • Multidimensional array as a flat array problem
  • web.xml configuration based on Maven profile
  • Why nullable types will not be equal in this case?
  • Passing a Scala type to a function
  • Listview - list item selection and initial setting (custom made items)
  • Receive custom intent without activity restart
  • Allocating a 2D contiguous array within a function
  • NHibernate proxyexception
  • Can't compile Arduino CapSense example
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • can variables be set randomly when declaring them again?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • extjs4 catching the scroll event on panel
  • What Makes These Two Array Adds Different?
  • NHibernate manually control fetching
  • there is no graph with tensorboard
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • SAXReader not re-ecape characters
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Word Open XML Mail Merge
  • Yii2: Config params vs. const/define
  • Checking free space on FTP server
  • Ajax Loaded meta Tags
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • KeystoneJS: Relationships in Admin UI not updating
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal