Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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';
    })
);
share|improve this answer
    
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? –  AntonB 17 hours ago
1  
you're a genius, appreciate the help, the implode worked GREAT, turned this into a function that will return the imploded values. –  AntonB 17 hours ago
1  
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. –  scrowler 17 hours ago
    
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. –  AntonB 16 hours ago

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;
  }
}
share|improve this answer
    
they are actually boolean values coming from a database! should have cleared that up so they can only be 1 or 0. –  AntonB 17 hours ago
    
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 –  scrowler 17 hours ago
    
When i did print_r that is how it displayed my array. –  AntonB 17 hours ago
    
No problem then :) –  scrowler 17 hours ago

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.