PHP array problems - grouping array -


i'm trying convert array values (group actually), don't know how that.

i have this:

[  {   "nsr": "000086310",   "type": "3",   "date": "2015-07-18",   "time": "00:06",   "pis": "12138790985"  },  {   "nsr": "000086313",   "type": "3",   "date": "2015-07-18",   "time": "00:33",   "pis": "16073736879"  },  {   "nsr": "000086316",   "type": "3",   "date": "2015-07-18",   "time": "00:58",   "pis": "16634402451"  },  {   "nsr": "000086316",   "type": "3",   "date": "2015-07-19",   "time": "00:58",   "pis": "98127981729"  },  {   "nsr": "000086316",   "type": "3",   "date": "2015-07-19",   "time": "00:58",   "pis": "12398712938"  } ] 

and want convert this:

[  "date" : "2015-07-18",  "pis" : [      "12138790985",      "16073736879",      "16634402451"   ] ], [  "date" : "2015-07-19",  "pis" : [      "98127981729",      "12398712938"   ] ] 

i tried this:

    public function index()     {         $this->settxtdata('../../txt_files/cap  3 18 07 2015 fabrica.txt');         $txtdata = $this->gettxtdata();          $datatocompare = array();          foreach($txtdata $ponto){             $time = $ponto['time'];             $date = $ponto['date'];             $pis = $ponto['pis'];  //            $datatocompare = array(); //            if(strpos($ponto['pis'], '00000000000') === false){ //                $pis_temp[][''] = $ponto['pis']; //            }              if(isset($datatocompare)){                 foreach($datatocompare $datetoset){                     if($datetoset['data'] == $date){                         $datetoset['pis'][] = $pis;                     }                     else{                         $datetoset['data'] = $date;                         $datetoset['pis'][] = $pis;                     }                 }             }             else{                 $datatocompare = array(                     [                         'data' => $date,                         'pis' => array($pis)                     ]                 );             }               $funcionario_id = db::table('funcionario')                 ->select('id')                 ->where('pis_pasep', '=', $pis)                 ->pluck('id');              if($funcionario_id !== null){                  $validate = db::table('horas_trabalho')                     ->select('id')                     ->where('hora', '=', $time)                     ->where('data', '=', $date)                     ->where('funcionario_id', '=', $funcionario_id)                     ->pluck('id');                  if($validate === null){                     db::table('horas_trabalho')                         ->insert([                             'hora' => $time,                             'data' => $date,                             'funcionario_id' => $funcionario_id                         ]);                 }             }         }          //-------------------------------------lógica para faltas-----------------------------------           /**          * pega o pis          */         $db_all_funcionarios = db::table('funcionario')             ->select('pis_pasep')             ->where('pis_pasep', '!=', 0)             ->get();          foreach($db_all_funcionarios $pis){             if(strpos($pis->pis_pasep, '00000000000') !== true){                 $global_pis[] = $pis->pis_pasep;             }         }  //        $faltantes = array_diff($pis_temp, $global_pis);  //        foreach($faltantes $faltante){ //            db::table('falta') //                ->insert([ //                   'data' => date('2015-07-16') //                ]); //        }  //        $ponto_db[] = db::table('horas_trabalho') //            ->join('funcionario', 'horas_trabalho.funcionario_id', '=', 'funcionario.id') //            ->select('funcionario.nome', 'horas_trabalho.hora', 'horas_trabalho.data') //            ->get();            return $txtdata;       } 

i see date in filename , think want group entities @ all:

$datatocompare = array(   'date' => $txtdata[0]['date'],   'pis' => array_map(function($el){return $el['pis'];}, $txtdata) ); 

for multiple dates:

$hash = array(); foreach ($txtdata $entity) {   if (!isset($hash[$entity['date']])) $hash[$entity['date']] = array();   $hash[$entity['date']][] = $entity['pis']; } $result = array(); foreach($hash $date=>$pis) {   $result[] = array('date'=>$date, 'pis'=>$pis); } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -