mardi 19 janvier 2021

loop an array or object for data to save its name and value in a variable with design pattern

I'm looking for how to loop an array or recursive object, to save the name and its value in a variable.

the condition is that when saving these two data you must maintain a design as similar to the examples below and respect the alignment of the arrows:

array that is given in linear format:

$example_array = ['i_txt_2' => 'CONT0000603','i_txt_3' => 100,'p_saveNumCont' => '','i_date_1' => '2021-Jan-17','i_date_2' => '2021-01-17','i_hour_1' => '6:31:00 AM','i_hour_2' => '17:31:00','i_txt_4' => '1.78','arr_datos' => [0 => true,1 => false,2 => true,],];

output del print:

$example_array = [
    'i_txt_2'       => 'CONT0000603',
    'i_txt_3'       => 100,
    'p_saveNumCont' => '',
    'i_date_1'      => '2021-Jan-17',
    'i_date_2'      => '2021-01-17',
    'i_hour_1'      => '6:31:00 AM',
    'i_hour_2'      => '17:31:00',
    'i_txt_4'       => '1.78',
    'arr_datos'     => [
        0 => true,
        1 => false,
        2 => true,
    ],
];

I should point out that none of the following functions can be used:

  1. var_dump (): displays information about a variable.
  2. print_r (): prints human readable information about a variable.
  3. debug_zval_dump (): generates a string that represents an internal value of zend.
  4. var_export (): prints or returns a string representation of a parseable variable.

I have tried this but it does not work correctly because recursion or that the online printing respects the alignments or multilevel wrap "[","]":

$string = "";
foreach($regs as $object) {
  foreach($object as $key=>$value) {
    $string += "{$key}=>{$value} ";
  }
}
echo $string;

the final output is supposed to add a comment to each line and that they must be aligned:

<?php 
    //Comment
    //Comment
    $example_array = [                      
        'i_txt_2'       => 'CONT0000603',       //Comment
        'i_txt_3'       => 100,                 //Comment
        'p_saveNumCont' => '',                  //Comment
        'i_date_1'      => '2021-Jan-17',       //Comment
        'i_date_2'      => '2021-01-17',        //Comment
        'i_hour_1'      => '6:31:00 AM',        //Comment
        'i_hour_2'      => '17:31:00',          //Comment
        'i_txt_4'       => '1.78',              //Comment
        'arr_datos'     => [                    //Comment
            0 => true,                          //Comment
            1 => false,                         //Comment
            2 => true,                          //Comment
        ],
    ];

Aucun commentaire:

Enregistrer un commentaire