Define array by Index

0

I have a multi array with data that includes dates, I want to do it is to pass that data to another multi array but sorted by month index, that is, if I have date 02/05/2017 in the other array you have to create the index 5 and put there all the arrays with month 5.

The array with the data comes from a query.

This is the code I have from while.

$result=[
            array("131","2017-05-24","2017-06-24","A","B","1","0","1"),
            array("132","2017-05-24","2017-06-10","C","D","0","1","0"),
            ];

$FArray=[];

            for ($i=0; $i<count($result);$i++){
                $NIMes=substr($result[$i][1],-5,5);
                $MesIN=substr($NIMes,1,2);
                $FIMes=substr($result[$i][2],-5,5);
                $MesFI=substr($FIMes,0,2);
                $MesI=(int)$MesIN;
                $MesF=(int)$MesFI;


                if (array_key_exists($MesI,$FArray)) {
                    array_push($FArray[$MesI],$result[$i]);
                }
                else{
                        $FArray[$MesI]=$result[$i];

                    }
            }

            echo "<pre>",var_dump($FArray),"</pre>";

He shows it to me like this.

     array(1) {
     [5]=>
     array(9) {
        [0]=>string(3) "131"
        [1]=> string(10) "2017-05-24"
        [2]=>string(10) "2017-06-24"
        [3]=>string(1) "A"
        [4]=>string(1) "B"
        [5]=>string(1) "1"
        [6]=>string(1) "0"
        [7]=>string(1) "1"
        [8]=>array(8) {
           [0]=>string(3) "132"
           [1]=>string(10) "2017-05-24"
           [2]=>string(10) "2017-06-10"
           [3]=>string(1) "C"
           [4]=>string(1) "D"
           [5]=>string(1) "0"
           [6]=>string(1) "1"
           [7]=> string(1) "0"
   }
  }
 }

But I want you to show it to me like that.

     array(1) {
     [5]=>array(2){
        [0]=>array(8) {
         [0]=>string(3) "131"
         [1]=> string(10) "2017-05-24"
         [2]=>string(10) "2017-06-24"
         [3]=>string(1) "A"
         [4]=>string(1) "B"
         [5]=>string(1) "1"
         [6]=>string(1) "0"
         [7]=>string(1) "1"
          }
         [1]=>array(8) {
           [0]=>string(3) "132"
           [1]=>string(10) "2017-05-24"
           [2]=>string(10) "2017-06-10"
           [3]=>string(1) "C"
           [4]=>string(1) "D"
           [5]=>string(1) "0"
           [6]=>string(1) "1"
           [7]=> string(1) "0"
           }
  }
 }

Example to explain it better.

Here is the multi array with the data and dates that are from month 5 and 6.

$result=[
            array("131","2017-05-24","2017-06-24","A","B","1","0","1"),
            array("132","2017-06-24","2017-06-10","C","D","0","1","0"),
            ];

Here the array as I want it to be.

         $FArry=[
      /*aqui index 5 que hace referencia mes de Mayo*/   
               [5] => [0]=>array("131","2017-05-24","2017-06-24","A","B","1","0","1"),

    /*aqui index 6 que hace referencia mes de Junio*/
               [6]=>  [0]=>array("132","2017-06-1","2017-06-10","C","D","0","1","0"),
            ]

Not if this time I explained better.

    
asked by Kast 18.09.2017 в 10:01
source

1 answer

1

Your approach is correct, except for one detail, $FArray is an "array of arrays", however, when you initialize it the first time (if the index does not exist) you do it equaling it to the array you are evaluating instead to store this array in the first position of the array. It's easy to solve

<?php 

        $result=[
            array("131","2017-05-24","2017-06-24","A","B","1","0","1"),
            array("132","2017-05-24","2017-06-10","C","D","0","1","0"),
            ];

$FArray=[];

            for ($i=0; $i<count($result);$i++) {

                $NIMes=substr($result[$i][1],-5,5);
                $MesIN=substr($NIMes,1,2);
                $FIMes=substr($result[$i][2],-5,5);
                $MesFI=substr($FIMes,0,2);
                $MesI=(int)$MesIN;
                $MesF=(int)$MesFI;


                if (array_key_exists($MesI,$FArray)) {
                    array_push($FArray[$MesI],$result[$i]);
                }else{
                        $FArray[$MesI][] = $result[$i];

                    }
            }

            echo "<pre>",var_dump($FArray),"</pre>";

See the difference:

$FArray[$MesI][] = $result[$i];

Instead of:

$FArray[$MesI] = $result[$i];
    
answered by 18.09.2017 / 11:06
source