Print array in php

3

I have the following array, how can I print it in an orderly way?

Array ( [return] => Array ( [0] => Array ( [codigo_materia] => 3525 [desc_materia] => Aprender Biologia [id] => 1 [nombre_materia] => Biologia ) [1] => Array ( [codigo_materia] => 3678 [desc_materia] => Aprender Quimica [id] => 2 [nombre_materia] => Quimica ) ) )

I would like it to be printed in this way:

that array itself is a list that returns a function in a webservice is a soap response.

require_once './nusoap/lib/nusoap.php';
$client = new soapclient('http://localhost:8080/WebApplicationMaterias/NewWebServiceMaterias?WSDL', true);
$result = $client->call('getMateria');

when I use echo "<pre>"; print_r($result); echo "</pre>"; print me the following:

Array
(
    [return] => Array
        (
            [0] => Array
                (
                    [codigo_materia] => 3525
                    [desc_materia] => Aprender Biologia
                    [id] => 1
                    [nombre_materia] => Biologia
                )

            [1] => Array
                (
                    [codigo_materia] => 3678
                    [desc_materia] => Aprender Quimica
                    [id] => 2
                    [nombre_materia] => Quimica
                )

        )

)

and with var_dump print me:

array(1) {
  ["return"]=>
  &array(2) {
    [0]=>
    array(4) {
      ["codigo_materia"]=>
      &string(4) "3525"
      ["desc_materia"]=>
      &string(17) "Aprender Biologia"
      ["id"]=>
      &string(1) "1"
      ["nombre_materia"]=>
      &string(8) "Biologia"
    }
    [1]=>
    &array(4) {
      ["codigo_materia"]=>
      &string(4) "3678"
      ["desc_materia"]=>
      &string(16) "Aprender Quimica"
      ["id"]=>
      &string(1) "2"
      ["nombre_materia"]=>
      &string(7) "Quimica"
    }
  }
}
    
asked by juancamilovallejos0 18.04.2018 в 22:26
source

3 answers

1

Try the following:

foreach($result['return'] as $elemento){
     foreach($elemento as $key => $data){
        echo $key.": ".$data."<br />";
     }
     echo "<hr />";
}
    
answered by 18.04.2018 / 22:43
source
2

In your specific case, you could program something similar to what Alfredo said, but taking into account that the Array you have is three-dimensional and that you have to sort the fields.

So, I would do something like this:

// Obtenemos la respuesta con las materias
require_once './nusoap/lib/nusoap.php';
$client = new soapclient('http://localhost:8080/WebApplicationMaterias/NewWebServiceMaterias?WSDL', true);
$result = $client->call('getMateria');

// Ahora iteramos sobre cada materia:
foreach ($result["return"] as $materia) {
  ksort($materia); // Ordenamos las propiedades de esta materia
  // Iteramos sobre estas propiedades, ahora ya ordenadas
  foreach ($materia as $campo => $valor) {
    // Imprimimos la propiedad de la materia correspondiente a
    // cada iteración (por ejemplo, "codigo_materia: 3542")
    echo $campo.": ".$valor."<br>";
  }
  echo "<br>"; // Saltamos de línea para separar las diferentes materias
}

Here I have done a little "shabby" separating the fields by a line break, and I'm assuming that you are using PHP to dynamically load HTML pages and not as CLI (in this case instead of finishing each line with <br> would end with \n or PHP_EOL ), but in any case later you can retouch this code to define the structure that you like and then add styles using CSS, if you wish.

    
answered by 18.04.2018 в 23:32
1

To be able to go through an associative array, like the one you are showing you can do with a foreach:

If the arrangement is one dimension, it can be like this:

<?php
$valores = array("valor_uno" => "Alfredo", "valor_dos" => "Jorge", "valor_tres" => "Diana");

foreach($valores as $key => $value){
    echo $value.PHP_EOL;
}
//LO CUAL IMPRIMIRÁ
//Alfredo
//Jorge
//Diana
  

To the echo that is inside you can already concatenate the structure of   the labels of a normal table such as tr and td and being a   Loop will print you as many rows of table as values have your arrangement

    
answered by 18.04.2018 в 22:37