Convert an element of an array into a string in php

0

I have a list of people that I bring from my bd , I want to go through that list and change each person in my list one of the attributes of the same one that is of type int as it can be the document of the person and pass that parameter from a int to a string .

Personas.php

<?php
class atencion {
public static function get($peticion){
        if ($peticion[0] == 'getAll') {
        return self::getAll();
        }

public static function getAll(){

        $sql = "SELECT * FROM persona";

        $sentencia = ConexionBD::obtenerInstancia()->obtenerBD()->prepare($sql);

        try{    
            if ($sentencia->execute()) {
                $resultado = $sentencia->fetchAll(PDO::FETCH_CLASS);
                return $resultado;  
            }
             else {
                return null;
            }
        }catch(PDOException $e){
            throw new ExcepcionApi(self::ESTADO_FALLA_DESCONOCIDA, "Falla desconocida". $e->getMessage(), 400);
        }
}

public static function post($peticion){

        if ($peticion[0] == 'agregar') {

            return self::agregar();

        }  else {

            throw new ExcepcionApi(self::ESTADO_URL_INCORRECTA, "Url mal formada", 400);

        }

} 

public static function agregar(){

            $body = file_get_contents('php://input');
    $input = json_decode($body);
    $array = (array)$input;

        $idPersona = $array['idPersona'];

        $documento = $array['documento'];

$sql = "INSERT INTO persona (idPersona, documento) VALUES(:idPersona, :documento)";

     try{

        $pdo = ConexionBD::obtenerInstancia()->obtenerBD(); 

        $stmt = $pdo->prepare($sql);

        $stmt->bindParam(':idPersona', $idPersona);

        $stmt->bindParam(':documento', $documento);

        $stmt->execute(); 

        $pdo = null;

        echo '{"notice": {"text": "Persona Agregada"}';

     } catch(PDOException $e){

        echo '{"error": {"text": '.$e->getMessage(). '}';

     }

}
}
    
asked by Germanccho 24.05.2018 в 17:32
source

1 answer

1

If you want to take the value of the key documento convert it to string and insert it as such in the array, you can do this:

If it is an array with a single row

$array=array("idPersona"=>1, "documento"=>7);
$array["strDocumento"]=(string)$array["documento"];

Here what you do is create a new key called strDocumento and save in it the value of documento converted to string .

Test:     var_dump ($ array);

Exit:

array(3) {
  ["idPersona"]=>
  int(1)
  ["documento"]=>
  int(7)
  ["idDocumento"]=>
  string(1) "7"
}

If it is an array with several rows

It would be a procedure similar to the previous one, but within a loop foreach that will allow us to read the different rows of the array:

$array=array(
                array("idPersona"=>1, "documento"=>7),
                array("idPersona"=>2, "documento"=>-9),
            );

foreach ($array as $k=>$row){
    $array[$k]["strDocumento"] = (string)$row["documento"];
}

Test:

var_dump($array);

The new key will be displayed in the array, with a data type string :

array(2) {
  [0]=>
  array(3) {
    ["idPersona"]=>
    int(1)
    ["documento"]=>
    int(7)
    ["strDocumento"]=>
    string(1) "7"
  }
  [1]=>
  array(3) {
    ["idPersona"]=>
    int(2)
    ["documento"]=>
    int(-9)
    ["strDocumento"]=>
    string(2) "-9"
  }
}
    
answered by 25.05.2018 / 01:16
source