How to modify data in array query result to BD?

0

I try to modify one of the data of array that I get from a query to BD , before passing it to JSON , but I do not get it, I access the value in a foreach but I do not know how to change the data

  

array (1) {["data"] = > array (2) {[0] = > array (8) {["id"] = > string (1) "3" ["customer_name"] = > string (8) "Cristina" ["customer_surname1"] = > string (9) "Martínez" ["customer_surname2"] = > string (10) "Fernández" ["born_date"] = > string (10) "1980-03-02" ["wish"] = > string (1) "1" ["type"] = > string (7) "Study" ["start"] = > string (19) "2017-12-19 17:00:00"} [1] = > array (8) {["id"] = > string (2) "78" ["customer_name"] = > string (8) "Hector" ["customer_surname1"] = > string (6) "Arraiz" ["customer_surname2"] = > string (4) "Something" ["born_date"] = > string (10) "1985-11-10" ["wish"] = > string (1) "1" ["type"] = > string (8) "Exterior" ["start"] = > string (19) "2017-12-21 17:00:00"}}}

$sql = "SELECT customers.id,'customer_name','customer_surname1','customer_surname2','born_date','wish', type_event.type, events.start FROM 'customers' INNER JOIN event_has_customers on customers.id = customer_id left JOIN events on event_id = events.id LEFT JOIN type_event on events.id = type_event.id";


    $result = $mysqli->query($sql);
    if($mysqli->errno) die($mysqli->error);
    $response["data"]=array();
    while($registros = $result->fetch_assoc()){
        $response["data"][]=$registros;
    }

    //aquí llego a la fecha
    foreach ($response as $key => $value) {
        foreach ($value as $k => $v) {
            var_dump(formatDte($v['start']));//aquí cambia el formato
        }
    }

echo json_encode($response);//start sigue en su formato original

function formatDte($cdn){
    $array = explode(' ', $cdn);
    return $array[0];
}
    
asked by crismf 17.12.2017 в 16:15
source

3 answers

0

The problem is that you are not " treading " the value in the array $response .

Solution 1

Overwrite the value in the position

// aquí llego a la fecha
foreach($response as $key => $value) {
  foreach($value as $k => $v) {

    // AQUI cambia el formato y guardamos en la posición correspondiente
    // del arreglo $response
    $response[$key][$k]['start'] = formatDte($v['start']);
  }
}

Solution 2

Use values by reference to step on the value of the variable.

// aquí llego a la fecha
foreach($response as $key => &$value) {
  foreach($value as $k => &$v) {

    // AQUI cambia el formato y escribimos en $v que apunta a la posicion
    // $response[$key][$k]['start']
    $v['start'] = formatDte($v['start']);
  }
}
    
answered by 23.12.2017 / 11:18
source
0

Try assigning the result of the formatDte() function:

 foreach ($response as $key => $value) {
        foreach ($value as $k => $v) {
            // modificamos el indice start por el nuevo formato
            $v['start'] = formatDte($v['start'])
        }
    }
    
answered by 17.12.2017 в 16:18
0

I do not know if I understood you correctly, but if what you are trying is to transform the canonical format date to another format I would try doing this function formatDte($cdn){ $date = new DateTime($cdn);//esto recibe un formato en string '2000-01-01' return $date->format('Y-m-d H:i:s');// esto es une ejemplo visita la docu } link

The value of that return is set as a value in the index you need, but if you already have an associative array and you know the value of the index, you do not have to scroll through it to only change a value.

    
answered by 23.12.2017 в 10:37