Create new array based on element position

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 )
);

I am creating a sub array from this. Any value greater than 2000 becomes a 1, otherwise it becomes a 0.

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

This produces the following

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

Now what I am trying to do is produce another array that shows the positions of the 1's within the above array. Each row should be represented by a new array. The output I am looking for is this

$dataArray = array(
    array( 1, 2 ),
    array( 1, 3 ),
    array( 1, 2 ),
    array( 1 ),
    array( 1 )
);

So I can see that row 1 has a 1 in position 1 and 2, row 2 has a 1 in positions 1 and 3 etc.

I wanted to make use of my current loop, so I am trying something like this

$position = array();

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

But this seems way off according to my output. How can I achieve what I am after?

Thanks

3 answers

  • answered 2018-10-11 19:21 Jeto

    You could use array_map along with array_keys:

    $dataArray = [
      [1, 1, 0],
      [1, 0, 1],
      [1, 1, 0],
      [1, 0, 0],
      [1, 0, 0]
    ];
    
    $onePositions = array_map(function($row) {
      return array_map(function($position) { return $position + 1; }, array_keys($row, 1));
    }, $dataArray);
    
    echo '<pre>'; var_dump($onePositions); echo '</pre>';
    

    Demo here

  • answered 2018-10-11 19:23 sietse85

    <?php
    $dataArray = array(
        array( 20800, 21679, 0 ),
        array( 15254, 0, 3726 ),
        array( 17426, 2973, 0 ),
        array( 4391, 37, 0 ),
        array( 39194, 435, 0 )
    );
    $endResult = [];
    
    foreach ($dataArray as $key => $value) {
       foreach ($value as $subkey => $subvalue) {
        if ($subvalue >= 2000) {
           $endResult[$key][] = $subkey +1;
        }
       }
    }
    
    print_r($endResult);
    
    Array
    (
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )
    
    [1] => Array
        (
            [0] => 1
            [1] => 3
        )
    
    [2] => Array
        (
            [0] => 1
            [1] => 2
        )
    
    [3] => Array
        (
            [0] => 1
        )
    
    [4] => Array
        (
            [0] => 1
        )
    )
    

  • answered 2018-10-11 19:24 mtr.web

    You can make use of the second parameter in the array_keys() function to get the positions after you populate the ones and zeros:

    $position = array();
    $binary = array();
    
    foreach ($dataArray as $row => $data) {
    
        // if you want to have the positions start at 1 instead of 0
        // add a dummy value to the final array
        $binary[$row][] = 0;
    
        foreach ($data as $key => $value) {
            if($value > 1999) {
                $binary[$row][] = 1;
            } else {
                $binary[$row][] = 0;
            }
        }
        $positions[] = array_keys($binary[$row], 1);
    }
    

    Demo Here