Good how can I organize this array without using a loop
Original Array
["Entrada" => 4, "Prato principal" => 1]
Intended Array
[["Entrada",4],["Prato principal",1]]
Ok I understood what you guys mean ... I had looped even more so I ended up changing by the answer I'm going back to the loop that's easier to understand. Now it turned out that an optimization even happened, because I took the code and adapted it to another part that was doing 2 queries in mysql, thus
$stmt = getConn()->query("SELECT hash,nome,categoria FROM produto");
$qryProduto = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
$qryProdNome = array_reduce(array_keys($qryProduto), function($novo, $chave) use ($qryProduto){
$novo[$chave] = $qryProduto[$chave][0]['nome'];
return $novo;
});
$qryProdCateg = array_reduce(array_keys($qryProduto), function($novo, $chave) use ($qryProduto){
$novo[$chave] = $qryProduto[$chave][0]['categoria'];
return $novo;
});
/*$stmt = getConn()->query("SELECT hash,categoria FROM produto");
$qryProdCateg = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
$qryProdCateg = array_map('reset', $qryProdCateg);
$qryProdCateg = array_map('reset', $qryProdCateg);*/
I was doing 2 queries one for product and another for category, there is no way I add that 2 reduces at once no? or no need since I avoided a search in mysql
NOTE ah and a detail, I wrote the loop in the ideone website and realized that with the loop, the response time was equal to 2ms, plus with the loop the weight was about 600kb smaller
Can you improve this logic? what is catching and what I'm doing 2 loop to organize the final answer, and this is bothering me
$stmt = getConn()->query("SELECT id,unix_timestamp(data) AS data FROM loginclient");
$qryLoginClient = $stmt->fetchAll();
$totaluser = $stmt->rowCount();
$onoff = ( $totaluser > 0 ? true : false ); // preparação para lazzyload
$usuarioHj = 0;
$usuarioOntem = 0;
$bugfix = 0;
$org_user = [];
foreach($qryLoginClient as $data){
if(strtotime($vhoje) <= $data['data']):
$usuarioHj++;
elseif(strtotime($vontem) <= $data['data']):
$usuarioOntem++;
endif;
$maskdate = date("d-m-Y", $data['data'] );
if($bugfix <= 15):
( !isset($org_user[$maskdate]) ? $org_user[$maskdate] = 1 : $org_user[$maskdate]++ );
endif;
$bugfix++;
}
foreach($org_user as $item => $id){
unset($org_user[$item]);
$org_user[] = [$item,$id];
}
$bugfix = ( $usuarioOntem == 0 ? 1 : $usuarioOntem );
$taxa_crescimento = ceil(($usuarioHj-$usuarioOntem)/$bugfix*100);
$respNovoUsuario = [$totaluser,$taxa_crescimento,$org_user];