0

A shipping service is providing me with this weird text format response, after some research i found it is very close to a .net array and i am trying to convert to PHP so i can do something useful with it.

My questions:

  1. What is this format?
  2. Is there any PHP function i could use to parse it?

    City[0] = "Elghorashi";
    State[0] = "";
    ZipCode[0] = "";
    CCity[0] = "Elghorashi";
    CState[0] = "";
    CZipCode[0] = "";
    City[1] = "Abugibha";
    State[1] = "";
    ZipCode[1] = "";
    CCity[1] = "Abugibha";
    CState[1] = "";
    CZipCode[1] = "";
    

Thanks

2
  • PHP variables and arrays have preceding $ symbol, unless those are constants.. Commented Apr 15, 2014 at 12:45
  • That looks like a var_dump of a php array. Are you using a var_dump to output that or is that the raw value being returned? Commented Apr 15, 2014 at 13:05

1 Answer 1

1

Here's a quick little snippit I whipped up to convert a string that looks like a PHP array into an actual PHP array. To be clear the $newArray variable is where the array is created to.

$string = 'City[0] = "Elghorashi"; State[0] = ""; ZipCode[0] = ""; CCity[0] = "Elghorashi"; CState[0] = ""; CZipCode[0] = ""; City[1] = "Abugibha"; State[1] = ""; ZipCode[1] = ""; CCity[1] = "Abugibha"; CState[1] = ""; CZipCode[1] = "";';
$string = substr($string, 0, -1);

$arr = explode('; ', $string);
$newArray = [];
foreach($arr AS $value){
    $keyVals = explode(' = ', $value);
    preg_match_all('/^(.*?)\[([0-9]+)\]$/', $keyVals[0], $matches);
    $value = is_string($keyVals[1]) ? substr($keyVals[1], 1, -1) : $keyVals[1];
    $newArray[$matches[2][0]][$matches[1][0]] = $value;
}
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.