Trying To Get a Custom Array With Looping PHP

Good day, i am trying to generate a csv with array below

array[0] => array(
  ["a"] => 1, 
  ["b"] => 1, 
  ["c"] => 1, --> the value is only 1 and 2
  ["d"] => user2, 
),
array[1] => array(
  ["a"] => 1,
  ["b"] => 1,
  ["c"] => 2,
  ["d"] => user3,
),
array[2] => array(
  ["a"] => 2,
  ["b"] => 2,
  ["c"] => 1,
  ["d"] => user4,
),
array[3] => array(
  ["a"] => 2,
  ["b"] => 2,
  ["c"] => 2,
  ["d"] => user5,
)

edit: i want to get the 2 array with same number on "a" and "b" value, but different in "c" value i tried my code below

$results = array();

for($i = 1 ; $i <= 10 ; $i++){
    $j = $i+1;

    $response = array(
        array(
            $i, $i, 1, "user".$i
        ),
        array(
            $i, $i, 2, "user".$j
        )
    );
    array_push($results, $response);
}

but the array result is not as i wanted like above result. is there any solutions?, sorry for the confusion of this question, i am trying my best to ask. thank you for the help!.

2 answers

  • answered 2020-02-19 09:56 Anant Singh---Alive to Die

    1.You need to use % (modules operator) in your code to set 1 or 2 at third index of each of your child-array.

    2.Don't create too many array/variables in your code.Do direct assignments

    $results = array();
    $j = 1;
    for($i=1; $i<=10; $i++){
        $j++;
        $results[] = array($i, $i, 1, "user".$j);
        $j++;
        $results[] = array($i, $i, 2, "user".$j);
    }
    
    print_r($results);
    

    Output: https://3v4l.org/GE7MD

  • answered 2020-02-19 09:56 RiggsFolly

    I think you will have to work with a for loop and a manually increments value for your userx value like this.

    $results = [];
    $user = 1;
    
    for($i = 1 ; $i <= 10 ; $i++){
        $x = 1;
        $user++;
        $results[] = [$i, $i, $x, "user$user"];
        $x = 2;
        $user++;
        $results[] = [$i, $i, $x, "user$user"];
    }
    print_r($results);
    

    RESULT:

    Array
    (
        [0] => Array
            (
                [0] => 1
                [1] => 1
                [2] => 1
                [3] => user2
            )
    
        [1] => Array
            (
                [0] => 1
                [1] => 1
                [2] => 2
                [3] => user3
            )
    
        [2] => Array
            (
                [0] => 2
                [1] => 2
                [2] => 1
                [3] => user4
            )
    
        [3] => Array
            (
                [0] => 2
                [1] => 2
                [2] => 2
                [3] => user5
            )
    
        [4] => Array
            (
                [0] => 3
                [1] => 3
                [2] => 1
                [3] => user6
            )
    
        [5] => Array
            (
                [0] => 3
                [1] => 3
                [2] => 2
                [3] => user7
            )
    
        [6] => Array
            (
                [0] => 4
                [1] => 4
                [2] => 1
                [3] => user8
            )
    
        [7] => Array
            (
                [0] => 4
                [1] => 4
                [2] => 2
                [3] => user9
            )
    
        [8] => Array
            (
                [0] => 5
                [1] => 5
                [2] => 1
                [3] => user10
            )
    
        [9] => Array
            (
                [0] => 5
                [1] => 5
                [2] => 2
                [3] => user11
            )
    
        [10] => Array
            (
                [0] => 6
                [1] => 6
                [2] => 1
                [3] => user12
            )
    
        [11] => Array
            (
                [0] => 6
                [1] => 6
                [2] => 2
                [3] => user13
            )
    
        [12] => Array
            (
                [0] => 7
                [1] => 7
                [2] => 1
                [3] => user14
            )
    
        [13] => Array
            (
                [0] => 7
                [1] => 7
                [2] => 2
                [3] => user15
            )
    
        [14] => Array
            (
                [0] => 8
                [1] => 8
                [2] => 1
                [3] => user16
            )
    
        [15] => Array
            (
                [0] => 8
                [1] => 8
                [2] => 2
                [3] => user17
            )
    
        [16] => Array
            (
                [0] => 9
                [1] => 9
                [2] => 1
                [3] => user18
            )
    
        [17] => Array
            (
                [0] => 9
                [1] => 9
                [2] => 2
                [3] => user19
            )
    
        [18] => Array
            (
                [0] => 10
                [1] => 10
                [2] => 1
                [3] => user20
            )
    
        [19] => Array
            (
                [0] => 10
                [1] => 10
                [2] => 2
                [3] => user21
            )
    
    )
    

    I removed the array_push() as it is quicker (no function call) to do a simple $arr[] = $something;