Compare and replace Intel array

Asked

Viewed 285 times

0

The problem and the following, I am creating daily OS chart, only that comes from the bank with day in English, I would like to replace each index with the value in Portuguese My code

    public function osSemanal($id_company)
{        
    $periodo = date("Y-m-d H:i:s", strtotime('-7 days'));
    $sql = $this->db->prepare("select DAYNAME(dt_chegada) as dia, count(id) as os_gerada from os where id_company= :id_company
    and dt_chegada >= :periodo group by DAYNAME(dt_chegada) order by dt_chegada");
    $sql->bindValue(":id_company", $id_company);
    $sql->bindValue(":periodo", $periodo);
    $sql->execute();
    $result = $sql->fetchAll(PDO::FETCH_OBJ);

    foreach ($result as $res):            
        $dados[$res->dia] = $res->os_gerada;
      endforeach;     

    echo json_encode($dados);
}

Comes from the bank:

[0]
"dia"=> "Friday",
"os_gerada"=> 1

[1]
"dia"=> "Sunday",
"os_gerada"=> 4

Is generated:

"Friday" => 1,
"Sunday" => 4

Expected result:

"Sexta" => 1,
"Domingo"=> 4

3 answers

3


You can use strftime after defining the location:

setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

echo ucfirst(strftime('%A', strtotime('Monday'))); // Segunda
echo ucfirst(strftime('%A', strtotime('Sunday'))); // Domingo
echo ucfirst(strftime('%A', strtotime('Wednesday'))); // Quarta
echo ucfirst(strftime('%A', strtotime('Friday'))); // Sexta
  • Mto good! I will delete my answer!

  • with implement in my code, I could not here

3

You can create an array with the days of the week translation, then go through your array translating the key in this way:

<?php

$arr1 = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 4];
$arrTranslate = ['Monday' => 'Segunda', 'Tuesday' => 'Terça', 'Wednesday' => 'Quarta'];

$newArray = [];
foreach($arr1 as $key => $value){

    $newArray[$arrTranslate[$key]] = $value;

}

print_r($newArray);

Or you can also before running your query call another command in Mysql that changes the language:

SET lc_time_names = 'pt_BR';

So your method would look like this:

public function osSemanal($id_company)
{
    $periodo = date("Y-m-d H:i:s", strtotime('-7 days'));
    $sqlAux = $this->db->prepare("SET lc_time_names = 'pt_BR'");
    $sqlAux->execute();
    $sql = $this->db->prepare("select DAYNAME(dt_chegada) as dia, count(id) as os_gerada from os where id_company= :id_company
    and dt_chegada >= :periodo group by DAYNAME(dt_chegada) order by dt_chegada");
    $sql->bindValue(":id_company", $id_company);
    $sql->bindValue(":periodo", $periodo);
    $sql->execute();
    $result = $sql->fetchAll(PDO::FETCH_OBJ);

    foreach ($result as $res):
        $dados[$res->dia] = $res->os_gerada;
    endforeach;

    echo json_encode($dados);
}
  • foreach ($result as $res): &#xA; $dados[$res->dia] = $res->os_gerada;&#xA; endforeach; &#xA; $arrTranslate = ['Monday' => 'Segunda', 'Tuesday' => 'Terça', 'Wednesday' => 'Quarta','Thursday'=>'Quinta','Friday'=>'Sexta',&#xA; 'Saturday'=>'Sabado','Sunday'=>'Domingo']; &#xA; $newArray = [];&#xA; foreach($dados as $key => $value){ &#xA; $newArray[$arrTranslate[$key]] = $value; &#xA; }

  • got better this way, and one foreach less kkk, vlw

  • because it is @Juniorramoty, when I answered the first time, I did not have the example of Query, I thought that the array could be coming from some plugin, so I answered the need of the moment. ai when posted.

  • i intendi, vlw

0

You can translate the days of the week with Switch or with the example IF below:

<?php
$DIA_en = array();
// Armazenar os dias da semana em uma array
$DIA_en['DIA'] = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
for($x = 0; $x <= count($DIA_en['DIA']); $x++) {
    if( $DIA_en['DIA'][$x] == "Monday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Segunda";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Tuesday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Terça";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Wednesday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Quarta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Thursday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Quinta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Friday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Sexta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Saturday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Sábado";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Sunday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Domingo";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    }
}
?>

Will return:

Before: Monday

Then: Monday

Before: Tuesday

Then: Tuesday

Before: Wednesday

Then: Wednesday

Before: Thursday

Then: Thursday

Before: Friday

Then: Friday

Before: Saturday

After: Saturday

Before: Sunday

Then: Sunday

  • 1

    Consider answering in need of question, it wants translation of array indices where your answer does not apply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.