0

I have an array as such:

$array = ["1","0","0","1","1"]


//replace with

$newArray = ["honda","toyota","mercedes","bmw","chevy"]

// only if the original array had a value of "1".
// for example this is my final desired output:

$newArray = ["honda","0","0","bmw","chevy"]

I want to change each value in a specific order IF and only if the array value is equal to "1".

For example the values, "honda", "toyota", "mercedes", "bmw", "chevy" should only replace the array values if the value is "1" otherwise do not replace it and they must be in the right position for example the first element in the array must only be changed to honda, not toyota or any of the other values.

I know I must iterate through the array and provide an if statement as such:

foreach($array as $val) {

    if($val == 1){

    //do something

  } else {
    return null;
  }
}

Please guide me in the right direction and describe how to replace the values in order, so that toyota cannot replace the first array value only the second position.

3 Answers 3

4

You can so something like this - iterating over the array by reference and replacing when the value is 1 (string) and the value in the replace array exists:

foreach($array as $key => &$current) {
    if($current === '1' && isset($replace[$key]))
        $current = $replace[$key];
}

Output:

Array
(
    [0] => honda
    [1] => 0
    [2] => 0
    [3] => bmw
    [4] => chevy
)

As per your comment, to output an imploded list of the cars that do have values, you can do something like this - filtering out all zero values and imploding with commas:

echo implode(
    // delimiter
    ', ', 
    // callback - filter out anything that is "0"
    array_filter($array, function($a) {
        return $a != '0';
    })
);
Sign up to request clarification or add additional context in comments.

4 Comments

I like your response, how to print the array values only if they are not 0 using the implode function and I suppose an IF statement?
you're a genius, appreciate the help, the implode worked GREAT, turned this into a function that will return the imploded values.
Nice - you'd be better to unset($array[$key) in an else condition of your original if statement in the foreach instead of filtering it at the end.
good call, unset will remove the default values of 1 , 0, I also threw in an exception so that the function only takes an array, I feel that I will use the function more than once plus it seems like better OOP practice to use extra functions.
2

Currently, our if is asking if $val is true (or if it exists) while your numeric array's values are strings.

Try this:

$array = ["1","0","0","1","1"]
$newArray = ["honda","toyota","mercedes","bmw","chevy"]

foreach($array as $key => $val) {
  if($val === '1'){ // if you only want honda
    $array[$key] = $newArray[$key];
  } else {
    return null;
  }
}

3 Comments

they are actually boolean values coming from a database! should have cleared that up so they can only be 1 or 0.
There's a difference between "0" and boolean 0 (false) - you should clarify this in your question as neither this answer nor mine will work for booleans, since you've specified strings in your question
When i did print_r that is how it displayed my array.
0

PHP Code

$array = array("1","0","0","1","1");
$newArray = array("honda","toyota","mercedes","bmw","chevy");

foreach($array as $key => $val) {
    if($val === '1')
        $array[$key] = $newArray[$key];
}

Would produce the answer you're looking for

Comments

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.