1

I have an array like so

$dataArray = array(
    array( 20800, 21679, 0 ),
    array( 15254, 0, 3726 ),
    array( 17426, 2973, 0 ),
    array( 4391, 37, 0 ),
    array( 39194, 435, 0 )
);

If an array element is above 2000, I want to give it a value of 1. Else I want to give it a value of 0. So the above array should turn into

$dataArrayOutput = array(
    array( 1, 1, 0 ),
    array( 1, 0, 1 ),
    array( 1, 1, 0 ),
    array( 1, 0, 0 ),
    array( 1, 0, 0 )
);

What would be the best way to achieve this? At the moment I am trying

foreach ($dataArray as $data) {
    foreach ($data as $key => $value) {
        if($value > 1999) {
            $value = 1;
        } else {
            $value = 0;
        }
    }
}

But nothing seems to change. How can I achieve what I am after?

Thanks

4 Answers 4

2

More for entertainment value, but still a viable alternative. Using array_walk_recursive() to walk through the array structure, then if the item is an integer (is_int()) it will set it according to if it's > 1999 or not. If it's not an integer it just leaves the data as is.

array_walk_recursive($dataArray,  function (&$data) { 
            $data = (is_int($data))?(($data> 1999)?1:0):$data;    });
Sign up to request clarification or add additional context in comments.

1 Comment

much more elegant solution
1

Array values inside a foreach loop are passed by value. You can either explicity use & to pass values by reference, or, use the key to modify the original array. Try:

foreach ($dataArray as $key_out => $data) {
    foreach ($data as $key => $value) {
        if($value > 1999) {
            $dataArray[$key_out][$key] = 1;
        } else {
            $dataArray[$key_out][$key] = 0;
        }
    }
}

Comments

1

You are close. You need to prepend a & to pass the memory address to the loop, and then update the value:

foreach ($dataArray as &$data) {
    foreach ($data as $key => &$value) {
        if($value > 1999) {
            $value = 1;
        } else {
            $value = 0;
        }
    }
}

You can read more about pass by reference here: http://php.net/manual/en/language.references.pass.php

If you have nested/different levels of your array, you can use a recursive function like this:

function recursiveUpdateValue($item) {
    if(is_array($item)) {
        foreach($item as $k => $v) {
            $item[$k] = recursiveUpdateValue($v);
        }
        return $item;
    } else {
        return ($item >= 2000) ? 1 : 0;
    }
}

$updatedArray = recursiveUpdateValue($dataArray);

Comments

0

I understand this will be hard to read, but it's a short code.

I use array_intersect to find the zeros in your dataArray ($da).
Then I use array_replace with an array of [1,1,1] (by using array_fill) and overwrite what zeros I found.

$arr = [0,0,0];

foreach($dataArray as &$da){
    $da = array_replace(array_fill(0,count($da),1), array_intersect($da, $arr));
}

var_dump($dataArray);

Output

array(5) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(1)
    [2]=>
    int(0)
  }
  [1]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(0)
    [2]=>
    int(1)
  }
  [2]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(1)
    [2]=>
    int(0)
  }
  [3]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(1)
    [2]=>
    int(0)
  }
  [4]=>
  &array(3) {
    [0]=>
    int(1)
    [1]=>
    int(1)
    [2]=>
    int(0)
  }
}

https://3v4l.org/uaTcX

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.