Parsear array object in php

1

I am consuming a Webservice Soap of type asmx from PHP . I receive the data but it does not give me the object array ordered with the fields that returns the XML of the Webservice , it is like that it shows all the data at the same time. This is what he gives me:

  

stdClass Object ([getClientsResult] = & stdClass Object ([any] => 210819SECTOR 1ALVAREZSAEZROBERTO ALEJANDRO210950SECTOR 1BOBADILLASILVAGLORIA JIMENA210968))

The fields should be: id, sector, apellido1, apellido2, nombre .

How can I pair or obtain this data in a more orderly way?

This is what I have in PHP :

$servicio = "http://192.168.1.100:8093/wsclientes.asmx?WSDL";
$cod = 11;
$params = array('cod_sec' => $cod);
$client = new SoapClient($servicio);
$arr = $client->getClientes($params);
$res = $arr->getClientesResult;
print_r($res);

Thank you.

    
asked by daniel2017- 05.01.2017 в 20:59
source

2 answers

0

You can try creating your SOAP client with the following option:

$client = new SoapClient($servicio, ['trace' => 1, 'features' => SOAP_SINGLE_ELEMENT_ARRAYS]);

At the end sends

var_dump($client);
var_dump($arr);

That should tell you your current situation, if what the WS returns is a string then we would have to see how to split that string maybe with explode()

    
answered by 08.01.2017 в 09:06
0

The returned string seems to respond to a specific pattern:

First there is a series of 6 numbers then there is the word "SECTOR" followed by a number and to end a string that represents the full name.

Starting from the basis that this pattern will always be followed and assuming that there will be other more complete solutions to this problem, I propose an approach to the desired solution.

To solve it, I used regular expressions to separate the data from the string in the observed patterns, so we get this data:

id, sector, nombre completo

The name is impossible to divide by name and surname following this strategy.

The strategy is as follows:

  • We separate the string into substrings using the id as the delimiter (attainment of 6 numbers)
  • For each substring: we separate using the sector that is composed of the word "SECTOR" plus a number.
  • assemble the array with the results.
  • The code would be more or less like that.

    CODE:

     <?php
        //Partiendo del array devuelto por el Webservice
        $array['any'] = '210819SECTOR1ALVAREZSAEZROBERTOALEJANDRO210950SECTOR1BOBADILLASILVAGLORIAJIMENA210968';
    
        //Separamos la cadena por el id del usuario que responde al patrón de una consecución de 6 números
        preg_match_all("/[0-9]{6}[A-Z]+[0-9]{1}[A-Z]+/u", $array['any'], $array);
    
           /* ahora $array contiene esto:
           Array
             (
             [0] => Array
             (
               [0] => 210819SECTOR1ALVAREZSAEZROBERTOALEJANDRO
               [1] => 210950SECTOR1BOBADILLASILVAGLORIAJIMENA
             )
           )*/
    
        $datos = [];
        $persona = [];
    
        foreach ($array[0] as $datos) {
    
            //Separamos el id del cliente
            preg_match("/[0-9]{6}/u", $datos, $id);
            $persona['id'] = $id[0];
            //separamos el sector al que pertenece
            preg_match("/SECTOR[0-9]{1}/u", $datos, $sector);
            $persona['sector'] = $sector[0];
            //Separamos el nombre competo utilizando como delimitador el sector
            $nombre = preg_split("/SECTOR[0-9]{1}/u", $datos);
            $persona['nombre'] = $nombre[1];
            //Lo añadimos al array de personas
            $personas[] = $persona;
        }
    
        //Este array contiene un array por cada uno de los clientes devueltos
        print_r($personas);
    

    RESULT:

    Array (
        [0] => Array
        (
            [id] => 210819
            [sector] => SECTOR1
            [nombre] => ALVAREZSAEZROBERTOALEJANDRO
       )
    
        [1] => Array (
            [id] => 210950
            [sector] => SECTOR1 
            [nombre] => BOBADILLASILVAGLORIAJIMENA
      )
    
    )
    

    Note:

    I'm not an expert in regular exprexiones, I suppose you can improve.

        
    answered by 22.03.2018 в 09:51