0
Good how I can organize this array without using a loop
Original array
["Entrada" => 4, "Prato principal" => 1]
Desired array
[["Entrada",4],["Prato principal",1]]
ok I understood what you mean... I had done in loop even more I ended up changing by the of the answer I will go back to the loop that is easier to understand. Now it turned out that there was an optimization, because I took the code and adapted it to another part that was making 2 queries in mysql, getting like this
$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 making 2 queries one for product and another for category, there is not as I put together that 2 reduce at once not? or n the need since I avoided a search in mysql
NOTE ah and a detail, I wrote the loop on the ideone site and realized that with the loop, the response time was equal 2ms, more with the loop the weight was about 600kb lower
Is there any way to improve this logic? what’s going on and what I’m doing 2 loop to organize the final answer, and it’s 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];
Set "organize". You can’t tell what you want. Even more with tag optimization. And what is the goal? The array is exactly this?
– Maniero
What you want has no logic; see, in the original array, the Arrows of the input element indicates that that value will be defined for that element. In the intended array you are separating the elements, then the second reading would be: array(0){ with elements 0.1 with the values "input" and "4" }.
– Mauro Alexandre
@Maniero yes array and that, but the information and mutable can have more indexes or less
– Willian
It’s impossible to do without one loop, What you can do is use an abstraction that hides it. Honestly, this is not optimization, it’s the other way around. It may make it shorter, but often less readable. Actually in this case I make shorter with the loop, and with more performance, so it is of those requirements that does not make the slightest sense.
– Maniero
@Maniero blz understood, now I edited the post ended that this answer helped me with another problem that I was not liking very much as it was, if you have like a look and see if it needs modification
– Willian