How to add value to an empty space arrangement (PHP)?

1

I have this arrangement:

   $datos = array(
                            "nombre" => array("Juan Perez","Pablo Manrique","Nancy Peña"),
                            "direccion" => array("Cra. 45 # 45 -56","Clle. 23 # 12 -19 Sur","Av. 34 # 16 -12"),
                            "telefono" => array("3456789","3214567","2135423"),
                            "fecha" => array("23/12/1997","12/10/1980"," 07/06/2000"),
                            "color" => array("Amarillo","Verde","Rojo"),
                            "significado" => array("Riqueza y alegría.","")
                        );

How can I validate that if you have empty spaces, add "No se encuentra el significado." to the arrangement and then print it?

This is what I tried, but it does not work for me, since the idea is that si color verde y rojo no tienen significado should put the same meaning for both, but it does not work for me.

if(in_array("",$datos['significado']))
            {
                array_push($datos['significado'],"No se encuentra el significado.");
            }

I would appreciate the interest.

    
asked by JDavid 26.05.2017 в 23:20
source

2 answers

1

How I understood you is that when you have the value of "" replace it with "No se encuentra el significado." if so you can go through the arrangement and if you find one that I replaced it in this way:

for ($i=0; $i < count($datos["significado"]) ; $i++) {
  if($datos["significado"][$i]==""){
    $datos["significado"][$i]="No se encuentra el significado.";
  }
}
    
answered by 26.05.2017 / 23:56
source
1

You can do it in two ways in terms of the execution:

  • Make a cycle that traverses the array and changes it:
  •     $datos = array(
                                    "nombre" => array("Juan Perez","Pablo Manrique","Nancy Peña"),
                                    "direccion" => array("Cra. 45 # 45 -56","Clle. 23 # 12 -19 Sur","Av. 34 # 16 -12"),
                                    "telefono" => array("3456789","3214567","2135423"),
                                    "fecha" => array("23/12/1997","12/10/1980"," 07/06/2000"),
                                    "color" => array("Amarillo","Verde","Rojo"),
                                    "significado" => array("Riqueza y alegría.","")
                                );
    
        for ($i=0;$i<count($datos["significado"]);++$i) {
                if ($datos["significado"][$i] == "") {
                   $datos["significado"][$i] = "No se encuentra el significado.";
                }
            }
    
    
    var_dump($datos["significado"]);
    
  • A function for when you are going to paint, that validates if it is white, assign it by default.
  • Greetings,

        
    answered by 27.05.2017 в 00:03