Meta Code
Meta Code

Reputation: 607

How to create a message string with array?

I need to create a function that takes a multidimensional array and converts its subarray data into a string with a specific structure.

I have this array:

  array(2) {
  [257]=>
  array(5) {
    ["recaudacion"]=>
    string(10) "2017-10-07"
    ["Contribuyentes Locales,Activid. Especiales y Salas de Recreacion"]=>
    string(10) "2017-10-16"
    ["Contribuyentes Convenio Multilateral"]=>
    string(10) "2017-10-13"
    ["Regimen simplificado"]=>
    string(10) "2017-10-00"
    ["Declaracion Jurada Fiscal 2016 Contibuyentes Locales"]=>
    string(10) "2017-10-00"
  }
  [258]=>
  array(3) {
    ["Impuestos de Sellos"]=>
    string(10) "2017-10-10"
    ["Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"]=>
    string(10) "2017-10-00"
    ["Recategorizacion cuatrimestral regimen simplificado"]=>
    string(10) "2017-10-00"
  }
}

and I want something like:

Array (
    257=>"recaudacion Vence: 2017-10-07

          Contribuyentes Locales,Activid. Especiales y Salas de Recreacion Vence: 2017-10-00

          Contribuyentes Convenio Multilateral Vence: 2017-10-00

          Regimen simplificado Vence: 2017-10-00

          Declaracion Jurada Fiscal 2016 Contibuyentes Locales Vence: 2017-10-00",
    258=>"Impuestos de Sellos Vence: 2017-10-10

          Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral Vence: 2017-10-00

          Recategorizacion cuatrimestral regimen simplificado Vence: 2017-10-00"
)

My code here:

function crearMailDeVencimientos($idPerfil, $impuestosDelPerfil){
    $datos = getPerfilData($idPerfil);
    $mail = "impuestos de: ".$datos[0]['nombre'];
    $destinatario = $datos[0]['email'];

    foreach ($impuestosDelPerfil as $impuesto) {
        $keys = key($impuestosDelPerfil);
        $mail .= "\n".$keys;
        $mail .= " Vence: ".$impuesto;
    }
    return $mail;
    return $destinatario;
}

function enviarMail(){
    $impuestosPorPerfil = filtrarImpuestos();
    foreach ($impuestosPorPerfil as $idPerfil => $impuestosDelPerfil) {
        $mail = crearMailDeVencimientos($idPerfil, $impuestosDelPerfil);
        $destinatario = crearMailDeVencimientos($idPerfil, $impuestosDelPerfil);
        $correo = "[email protected]";
        $asunto = "Vencimientos del mes actual";
        $headers = "From: [redacted]"." <".$correo.">\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        // mail($destinatario,$asunto,$mail,$headers);
        echo '<div class="enviado">Enviado</div>';
   }

First I create the message with crearMailDeVencimientos, then send an email to each mail (each id) with each message. $datos contains the name to whom it was sent and the mail.

Upvotes: 0

Views: 86

Answers (2)

mickmackusa
mickmackusa

Reputation: 48011

You can use this custom function to build your desired data structure without using conditionals.

Code: (Demo)

$array=[
    257 => [
        "recaudacion" => "2017-10-07",
        "Contribuyentes Locales,Activid. Especiales y Salas de Recreacion" => "2017-10-16",
        "Contribuyentes Convenio Multilateral" => "2017-10-13",
        "Regimen simplificado" => "2017-10-00",
        "Declaracion Jurada Fiscal 2016 Contibuyentes Locales" => "2017-10-00"
    ],
    258=> [
        "Impuestos de Sellos"=>"2017-10-10",
        "Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"=>"2017-10-00",
        "Recategorizacion cuatrimestral regimen simplificado"=>"2017-10-00"
    ]
];

function stringifySubarrays($array){
    foreach($array as &$a){  // modify $a by reference
        foreach($a as $k=>&$v){  // modify $v by reference
          $v="$k Vence: $v";  // write new value using key and value
        }
        $a=implode("\n\n",$a); // glue new subarray of string together using 2 newline characters
    }
    return $array;  // return the new array
}
var_export(stringifySubarrays($array));

Output:

array (
  257 => 'recaudacion Vence: 2017-10-07

          Contribuyentes Locales,Activid. Especiales y Salas de Recreacion Vence: 2017-10-16

          Contribuyentes Convenio Multilateral Vence: 2017-10-13

          Regimen simplificado Vence: 2017-10-00

          Declaracion Jurada Fiscal 2016 Contibuyentes Locales Vence: 2017-10-00',
  258 => 'Impuestos de Sellos Vence: 2017-10-10

          Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral Vence: 2017-10-00

          Recategorizacion cuatrimestral regimen simplificado Vence: 2017-10-00',
)

p.s. I should point out that you have a logic error in your code. return $destinatario; will never happen because return $mail; marks the end of the function call.

Here's a suggestion:

function crearMailDeVencimientos($idPerfil, $impuestosDelPerfil){
    // ... processing ...
    return [$mail,$destinatario];  // send back both pieces of data
}

function enviarMail(){
    // ...  code ...
    foreach ($impuestosPorPerfil as $idPerfil => $impuestosDelPerfil) {
        list($mail,$destinatario) = crearMailDeVencimientos($idPerfil, $impuestosDelPerfil);
        //   ^^^^^ ^^^^^^^^^^^^^ assign variable names to the two returned values
        // ... code ...
    }
}

Depending on how you wish to display the data, you might use something like this:

function crearMailDeVencimientos($idPerfil, $impuestosDelPerfil){
    $datos = getPerfilData($idPerfil);
    $mail = "impuestos de: ".$datos[0]['nombre'];
    $destinatario = $datos[0]['email'];
    foreach($impuestosDelPerfil as $i=>$a){
        $mail.="\n\n$i";  // double spacing before each id number
        foreach($a as $k=>$v){
            $mail.="\n\n\t$k Vence: $v";  // double spacing before each child, addintg \t (tab) for visual
        }
    }
    return [$mail,$destinatario];  // send back both pieces of data
}

function enviarMail(){
    $impuestosPorPerfil = filtrarImpuestos();
    foreach ($impuestosPorPerfil as $idPerfil => $impuestosDelPerfil) {
        list($mail,$destinatario) = crearMailDeVencimientos($idPerfil, $impuestosDelPerfil);
        $correo = "[email protected]";
        $asunto = "Vencimientos del mes actual";
        $headers = "From: [redacted]"." <".$correo.">\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        // mail($destinatario,$asunto,$mail,$headers);
        echo '<div class="enviado">Enviado</div>';
   }
}

Upvotes: 1

Prime
Prime

Reputation: 2482

Assuming the Array(2) is the top level in the multi-dimensional array, something like this should work:

$newarray = Array();

foreach($arr as $firstkey => $arr2) { //$arr here is the variable that contains your array
    $newstring = "";
    $first = true;
    foreach($arr2 as $key => $val) {
        $newstring .= ($first ? "" : "\r\n\r\n") . $key . " Vence: " . $val;
        $first = false;
    }
    $newarray[$firstkey] = $newstring;
}

3v4l.org result

The purpose of $first is so that there are no trailing or preceeding newlines added. This is done by a ternary operator, (1 = 1 ? [this will execute if 1=1 is true] : [this will execute if 1=1 is false])

Upvotes: 0

Related Questions