0
object(stdClass)#2 (1) { ["GetExchangeRatesResult"]=> string(6183) " 149 2016-08-10T00:00:00+02:00 978 EUR ЕМУ 1 61.1876 61.4951 61.8026 EMU евро Euro 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 840 USD С А Д 1 55.2335 55.511 55.7886 USA САД долар US dollar 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 826 GBP В.Британија 1 71.6483 72.0083 72.3684 U.K. британска фунта British pound 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 756 CHF Швајцарија 1 56.1406 56.4227 56.7048 Switzerland швајцарски франк Swiss franc 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 752 SEK Шведска 1 6.4493 6.4817 6.5141 Sweden шведска круна Swedish krona 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 578 NOK Норвешка 1 6.5392 6.5721 6.605 Norway норвешка круна Norwegian krone 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 392 JPY Јапонија 1 0.5402 0.5429 0.5456 Japan јапонски јен Japanese yen 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 208 DKK Данска 1 8.2278 8.2691 8.3105 Denmark данска круна Danish krone 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 124 CAD Канада 1 42.0043 42.2153 42.4264 Canada канадски долар Canadian dollar 2016-08-09T00:00:00+02:00 149 2016-08-10T00:00:00+02:00 36 AUD Австралија 1 42.3063 42.5189 42.7315 Australia австралиски долар Australian dollar 2016-08-09T00:00:00+02:00 " }

I get the response above from my SOAP request. But am unable to parse it into an array or at the very least Json.

This is what I've tried:

$xml = preg_replace(“/(<\/?)(\w+):([^>]*>)/”, “$1$2$3”, $xml);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$responseArray = json_decode($json,true);

When using:

function simpleXmlElement2array($xmlObject, $out = array()) {

    foreach ((array)$xmlObject as $index => $node) {
        $out[$index] = (is_object($node) || is_array($node)) ? self::SimpleXmlElement2array($node) : $node;
    }

    return $out;
}

I get:

Array
(
    [GetExchangeRatesResult] => 




























    149
    2016-08-10T00:00:00+02:00
    978
    EUR
    ЕМУ
    1
    61.1876
    61.4951
    61.8026
    EMU
    евро
    Euro
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    840
    USD
    С А Д
    1
    55.2335
    55.511
    55.7886
    USA
    САД долар
    US dollar
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    826
    GBP
    В.Британија
    1
    71.6483
    72.0083
    72.3684
    U.K.
    британска фунта
    British pound
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    756
    CHF
    Швајцарија
    1
    56.1406
    56.4227
    56.7048
    Switzerland
    швајцарски франк
    Swiss franc
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    752
    SEK
    Шведска
    1
    6.4493
    6.4817
    6.5141
    Sweden
    шведска круна
    Swedish krona
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    578
    NOK
    Норвешка
    1
    6.5392
    6.5721
    6.605
    Norway
    норвешка круна
    Norwegian krone
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    392
    JPY
    Јапонија
    1
    0.5402
    0.5429
    0.5456
    Japan
    јапонски јен
    Japanese yen
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    208
    DKK
    Данска
    1
    8.2278
    8.2691
    8.3105
    Denmark
    данска круна
    Danish krone
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    124
    CAD
    Канада
    1
    42.0043
    42.2153
    42.4264
    Canada
    канадски долар
    Canadian dollar
    2016-08-09T00:00:00+02:00


    149
    2016-08-10T00:00:00+02:00
    36
    AUD
    Австралија
    1
    42.3063
    42.5189
    42.7315
    Australia
    австралиски долар
    Australian dollar
    2016-08-09T00:00:00+02:00


)

I'd at least like to be able to get each item individually like $arr['12']

5
  • i was thinking about using explode() to create the array (using the spaces) and from there on forward, it's a dirty solution but it could get the job done. Commented Aug 10, 2016 at 10:41
  • I tried that...first used implode() then I used explode() but doesn't really do it. Commented Aug 10, 2016 at 10:43
  • well first you explode the soap response then with that array you make a new one with the first's indexes, and that one you convert to xml Commented Aug 10, 2016 at 10:43
  • so whats the $xml before you do xml_load_string, and what is the final result you would like to have Commented Aug 10, 2016 at 10:44
  • @sietse85 Instead of exploding I did $array = (array) $xml; The $xml is just the soap response — that is displayed with var_dump in my question. Commented Aug 10, 2016 at 10:46

1 Answer 1

1

Long time ago I've try recursive approach (it works for node). I've found my old code, rmemeber that recursion got limit (You can change it in server settings):

public static function SimpleXmlElement2array($xmlObject, $out = array())
{
    foreach ((array)$xmlObject as $index => $node) {
        $out[$index] = (is_object($node) || is_array($node)) ? self::SimpleXmlElement2array($node) : $node;
    }

    return $out;
}

edit: Ok no I understand what You want to do. The problem is invalid data. When I get string value from the node and try:

$temp = explode(' ',  trim($string));
print_r(array_chunk($temp, 13)); 

resluts won't be valid. Every try with explode or parse it using space as delimiter will failed, because of part 840 USD С А Д should be 840 USD САД

If not this case, You colud use aboive code. If You got this kind of response with unregular structure, than I don't have an idea how to dandle it without hardcode parse every value using regex.

Sign up to request clarification or add additional context in comments.

1 Comment

Not what I'm looking for...I edited the question with the result I'm getting with ur script, please take a look.

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.