Function return values

1

I am trying to include a code in function to make it more optimal but I can not visualize the values the code in question is this.

    function Ciudad() {
     if ($Datos[0]["Valor1"]>10) {$txte = " Ciudad, ";}
     if ($Datos[1]["Valor1"]>10) {$txte .= "Ciudad2, ";}
     if ($Datos[2]["Valor1"]>10) {$txte .= "Ciudad3, ";}
     if ($Datos[3]["Valor1"]>10) {$txte .= "Ciudad4, ";}
     if ($Datos[4]["Valor1"]>10) {$txte .= "Ciudad5, ";}
     if ($Datos[5]["Valor1"]>10) {$txte .= "Ciudad6, ";}
     if ($Datos[6]["Valor1"]>10) {$txte .= "Ciudad7, ";}
     if ($Datos[7]["Valor1"]>10) {$txte .= "Ciudad8, ";}
     if ($Datos[8]["Valor1"]>10) {$txte .= "Ciudad9, ";}
     if ($Datos[9]["Valor1"]>10) {$txte .= "Ciudad10, ";}
     if ($Datos[10]["Valor1"]>10) {$txte .= "Ciudad11, ";}
     if ($Datos[11]["Valor1"]>10) {$txte .= "Ciudad12, ";}
     if ($Datos[12]["Valor1"]>10) {$txte .= "Ciudad13, ";}
     if ($Datos[13]["Valor1"]>10) {$txte .= "Ciudad14, ";}
     if ($Datos[14]["Valor1"]>10) {$txte .= "Ciudad15, ";}
     if ($Datos[15]["Valor1"]>10) {$txte .= "Ciudad16";}
     return $txte;
    }

At the same time I thought about optimizing it since the values I take out of $ Data [0] ["Value1"] and the city of $ Data [0] ["Name"] had thought of a loop that gathered the information that meets with the requirement of > 10 something like that ...

    function Ciudad($id, $valor) {
    for ($i = $id; $i <  16; $i++) { 
        for ($j = $valor; $j < 17; $j++) { 
            if ($Datos[$i]["Valor".($j)]>10) {
            $ciudades .= $Datos[$i]["Nombre"]; } }
    }
     return $ciudades;
    }
    
asked by Vicente 09.11.2018 в 19:47
source

2 answers

0

You can pass your data as a parameter. I have declared the variable that you return empty so that it is always accessible although there is no data to add.

$Datos[0]["Valor1"]=3;
$Datos[0]["Nombre"]="Pekin";
$Datos[0]["Valor2"]=13;
$Datos[0]["Nombre"]="Tokio";
$Datos[1]["Valor1"]=3;
$Datos[1]["Nombre"]="Londres";
$Datos[1]["Valor2"]=13;
$Datos[1]["Nombre"]="Paris";

echo Ciudad(0,1,$Datos);
echo "<br>";
echo Ciudad(1,1,$Datos);

function Ciudad($id, $valor,$Datos) {
   $ciudades ="";
   for ($i = $id; $i < 2; $i++) { 
            for ($j = $valor; $j < 3; $j++) { 
                if ($Datos[$i]["Valor".$j]>10) {
                $ciudades .= $Datos[$i]["Nombre"]; } 
            }
  }
  return $ciudades;
}

Result:

TokioParis
Paris
    
answered by 09.11.2018 в 20:19
0

The following code creates an array by taking the value of the key Nombre of the original array in cases where the value of the key Valor of the same original array is greater than 10 .

I do not know if it is exactly what you want, but whatever it is, I intuit that it is something that can be done in a very simple way according to your requirements.

To print the list of cities separated by , , we apply implode on $partialData .

The code includes a simulation about the possible original state of your array.

$datos=array(
                array("Nombre"=>"Madrid",   "Valor"=>10),
                array("Nombre"=>"Tokio",    "Valor"=>15),
                array("Nombre"=>"New York", "Valor"=>5),
                array("Nombre"=>"París",    "Valor"=>10),
                array("Nombre"=>"Londres",  "Valor"=>11),
                array("Nombre"=>"Berlín",   "Valor"=>3)   
            );

foreach ($datos as $item){
    if ($item["Valor"]>10){
        $partialData[]=$item["Nombre"];
    }
}
echo implode(",",$partialData);

Exit:

Tokio,Londres

EDITION:

If instead the array is like this:

$datos=array(
                array("Nombre"=>"Madrid",   "Valor1"=>10),
                array("Nombre"=>"Tokio",    "Valor2"=>15),
                array("Nombre"=>"New York", "Valor3"=>5),
                array("Nombre"=>"París",    "Valor4"=>10),
                array("Nombre"=>"Londres",  "Valor5"=>11),
                array("Nombre"=>"Berlín",   "Valor6"=>3)   
            );

You just have to implement a counter to build the name of the key dynamically:

$i=1;

foreach ($datos as $item){
    if ($item["Valor$i"]>10){
        $partialData[]=$item["Nombre"];
    }
    $i++;
}
echo implode(",",$partialData);

The output will be the same. If also the key Nombre have a sequential number, you add the value here: $partialData[]=$item["Nombre$i"]; , although it is a pity that the data is like this, being able to have an easier and structured data design ...

NOTE: If it's something more complex, you should know that PHP has several tens of functions on arrays capable of doing any type of complicated operation in a simple way. Many of them, combined with other language functions, make possible any comparison, operation, calculation, construction of new arrays, etc.

    
answered by 09.11.2018 в 21:57