0

I have an array as below

Array
(
    [0] => Array
        (
            [0] => Pedigree Dry
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [1] => Array
        (
            [0] => Professional Range
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [2] => Array
        (
            [0] => Pedigree Wet
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [3] => Array
        (
            [0] => PMM
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [4] => Array
        (
            [0] => Chappi
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [5] => Array
        (
            [0] => Care & Treat
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [6] => Array
        (
            [0] => Sheba
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [7] => Array
        (
            [0] => Whiskas Dry
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

    [8] => Array
        (
            [0] => Whiskas Wet
            [1] => N/A
            [2] => N/A
            [3] => N/A
        )

)

The above code is an multidimensional array.But all array elements(except index) is N/A.I want to replace all the N/A with 0. How can I replace all N/A with 0 ?

5
  • 3
    For something like this since almost all the values are N/A I would probably not bother changing them in the array and instead change them on output. Commented Oct 28, 2014 at 13:07
  • 2
    Like echo $val == 'N/A' ? 0 : $val; assuming a loop. Commented Oct 28, 2014 at 13:08
  • @Michael I know the above idea. i want to pass the above value to another function. In that case what i will do?? Commented Oct 28, 2014 at 13:20
  • If you really need to modify it, then Ali's answer below is a good one. Commented Oct 28, 2014 at 13:23
  • I know that, no need to suggest. Commented Oct 28, 2014 at 13:38

3 Answers 3

3
$newArray = array();    
foreach($array as $inner_array) {
    $newArray[] = str_replace("N/A", 0, $inner_array);
}

This loops over all the inner arrays and replaces all "N/A" with a zero and adds them to a new resultant array.

Working Demo

Sign up to request clarification or add additional context in comments.

2 Comments

how to str_replace a string in an array? str_replace replaces a string in string. basic syntax. anyway, i do not downvoted.
@lolka_bolka str_replace can replace all array values as it loops all over them. Read the documentation. The third parameter is mixed which means it can be an array. I also included a working demo.
1

Try this:

function replaceNa($var) {
    if ($var == 'N/A' ) {
        return 0;
    } else {
        return $var;
    }
}

$array = array(
    array('Pedigre dry', 'N/A', 'N/A', 'N/A'),
    array('Professional Range', 'N/A', 'N/A', 'N/A'),
    array('Pedigree Wet', 'N/A', 'N/A', 'N/A'),
    array('PMM', 'N/A', 'N/A', 'N/A'),
);


foreach ($array as $key => $item) {

    $array[$key] = array_map('replaceNa', $item);
}

var_dump ($array);

Comments

-1

I'm sure there's a better answer than this.. but you could always just do the nested foreach approach.

//loop through each Array (first level)
foreach ($arrays as $array) 
{
     //create two variables for each sub array so you have access to keys and values
     foreach ($array as $key=>$value) 
     {
         if ($value = "N/A")
         {
              $value = 0;
         }
     }
}

3 Comments

You may want to pass $array and $value by reference. foreach ($array as $key=>&$value)
You would need array references in the foreach to actually modify the loop while iterating it (near the top of foreach docs)
This will not change the value of the element. Either $array[$key] = 0; or foreach($array as &$value) and then $value = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.