0

I have a return string from a 3rd party plugin that looks like a var_dump from an array. I'm trying to parse into a valid associative array. Looking at various examples and doing some tests with the code that follows. The last segment demonstrates the problem I'm having. I'm trying to parse out the data into a string and then programmatically create an array after my data string has been finished. When I do a print_r on $vegetables2, I get:

Array([0] => “Gourd”=”40 kilojoules”,”Artichoke”=”105 kilojoules”,”Cassava”=”550 kilojoules”)

And the echo $vegetables2["Artichoke"] produces no value. Can someone please guide me at the correct syntax to create the array equivalent to the first two examples?

//this works:
echo "From creating the entire array with a static string:<br/>";
$vegetables = array("Gourd"=>"40 kilojoules", "Artichoke"=>"105 kilojoules", "Cassava"=>"550 kilojoules");
echo "Artichoke: " . $vegetables["Artichoke"] . "<br/>";

//this works too
$vegetables1['Gourd'] = "40 kilojoules";
$vegetables1['Artichoke'] = "105 kilojoules";
$vegetables1['Cassava'] = "550 kilojoules";
echo "From creating one element at a time:<br/>";
echo "Artichoke: " . $vegetables1["Artichoke"] . "<br/>";

//this doesn't work
$strData = "\"Gourd\"=\"40 kilojoules\",";
$strData = $strData . "\"Artichoke\"=\"105 kilojoules\",";
$strData = $strData . "\"Cassava\"=\"550 kilojoules\"";
echo $strData ."<br/>";
$vegetables2 = array($strData);
print_r($vegetables2);
echo "Artichoke: " . $vegetables2["Artichoke"];
  • 1
    Can you show an example of the input data? – Don't Panic Nov 30 '17 at 19:39
  • 2
    105 Kilojoule Artichoke is the name of my Flaming Lips cover band. – Alex Howansky Nov 30 '17 at 19:41
  • This simply makes a one element array of the entire string you got: `$vegetables2 = array($strData);` ... Please show us an example of the RAW data you are trying to parse. It would shed light on many avenues for answers. – IncredibleHat Nov 30 '17 at 19:50
  • The input data returned from the plugin comes over like this: Array ( [Gourd] => 40 kilojoules [Artichoke] => 105 kilojoules [Casava] => 550 kilojoules ) The keys are not wrapped in quotes, but brackets and there are no commas separating the key/value pairs. – Doug V. Nov 30 '17 at 19:53
  • Well, there's this: https://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable, but I'd rather just not use a plugin that produced its output in var_dump format, if possible. – Don't Panic Nov 30 '17 at 20:37

2 Answers2

0
$strData = "\"Gourd\"=\"40 kilojoules\",";
$strData = $strData . "\"Artichoke\"=\"105 kilojoules\",";
$strData = $strData . "\"Cassava\"=\"550 kilojoules\"";


$dd=str_replace('"','',"$strData");
$ff=explode(',',$dd);
foreach ($ff as $c)
{
$xx=explode('=',$c);
$vegetables2["$xx[0]"]=$xx[1];
}
print_r($vegetables2);
echo "Artichoke: " . $vegetables2["Artichoke"];
  • aren't you supposed to have `'`enclosing your strings? – Andreas Nov 30 '17 at 20:01
  • Being that your end result array looks exactly like my starting array string, how do I go about just using that original string? This code does not do it: $arrayString = "Array ( [Gourd] => 40 kilojoules [Artichoke] => 105 kilojoules [Cassava] => 550 kilojoules )"; $vegetables3 = $arrayString; print_r($vegetables3); echo "Artichoke: " . $vegetables3["Artichoke"]; – Doug V. Nov 30 '17 at 20:41
  • write here exactly return value of 3rd party plugin (array or string). – Halis Tosun Dec 01 '17 at 07:27
  • I included that above .. here it is again: Array ( [Gourd] => 40 kilojoules [Artichoke] => 105 kilojoules [Cassava] => 550 kilojoules ) – Doug V. Dec 01 '17 at 14:43
0

This string is your input:

"Array ( [Gourd] => 40 kilojoules [Artichoke] => 105 kilojoules [Casava] => 550 kilojoules )"

  1. use strpos() to locate "("
  2. use strpos() to locate ")"
  3. use substr() to get what is between ( and )
  4. use explode to divide the result wherever "[" appears
  5. use array() to start a new array
  6. use foreach to reformat each piece
  7. use explode to divide each piece wherever "=>" appears
  8. use trim to remove excess characters (left and right)
  9. use $data[$key] = $value to create a new array to your liking
Wayne S
  • 1
  • 2