0

I am using a Webservice Soap that returns a var_export/var_dump (not quite sure) of a PHP array. I need either convert the response into an actual php array or get the values of the specific array in [VALUE] as a list or xml: Formula, Aprueba, Describe, etc.

Thanks in advance!

      <getAttribDataResponse xmlns="urn:admin">
         <return>Array
(
    [0] => Array
        (
            [CDATTRIBUTE] => 49
            [NMATTRIBUTE] => SD1-ACCION
            [FGDATATYPE] => 1
            [FGATTRIBUTETYPE] => 1
            [NMLABEL] => Doc_Acción de la Normativa
            [FGMULTIVALUED] => 1
            [VALUE] => Array
                (
                    [1721] => Formula
                    [1477] => Aprueba
                    [1486] => Describe
                    [1506] => Reglamenta
                    [1522] => Constituye
                    [2128] => Agrega
                    [1485] => Deroga
                    [1497] => Oficializa                  
                )

        )

)</return>
      </getAttribDataResponse>
8
  • That's the actual returned "data" from a call? (Looks like print_r() btw) Wow. I'd call that a debug printout at the most. Commented Jan 19, 2022 at 14:17
  • 1
    Anyway, if that's the case.. One regexp to extract the contents of VALUE, and then another regexp that extracts all the lines in the contents. Commented Jan 19, 2022 at 14:27
  • 2
    I would ask the owner of the webservice to return the data in a machine readable format like json e.g. echo json_encode($data); Commented Jan 19, 2022 at 14:28
  • This may help github.com/simivar/reverse-print-r Commented Jan 19, 2022 at 14:29
  • Thanks both of you Commented Jan 20, 2022 at 15:09

1 Answer 1

1

Ok, here we go, the regexp route. In general though, if there's a package that can parse the data that I'm working on I'd try to use it instead.

// Copied straight off from <return> in the response,
// shortened for readability
$str = 'Array
(
    [0] => Array
    ...

)';

// 1st regexp to extract the content lines of [VALUE]
preg_match(
    '/\[VALUE\] => Array[^\(]*\(\s*\n([^\)]+)\n\s+\)\n/',
    $str,
    $matches
);

// $matches[0] is what matched the whole regexp,
// $matches[1].. is for any parentheses inside it.
// We want the contents of the 1st parenthesis, so $matches[1]
$value_contents = $matches[1];

// 2nd regexp to extract the key/value pairs of [VALUE]
// One parenthesis for the key and one for the value,
// so $macthes[1] and $matches[2]
preg_match_all(
    '/\[([^\]]+)\] => (.+)/',
    $value_contents,
    $matches
); 

In this case we get this:

print_r($matches);
//    Array
//    (
//        [0] => Array
//            (
//                [0] => [1721] => Formula
//                [1] => [1477] => Aprueba
//                ...
//            )

//        [1] => Array
//            (
//                [0] => 1721
//                [1] => 1477
//                ...
//            )

//        [2] => Array
//            (
//                [0] => Formula
//                [1] => Aprueba
//                ...
//            )
//    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.