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"];
5
  • 1
    Can you show an example of the input data? Commented Nov 30, 2017 at 19:39
  • 2
    105 Kilojoule Artichoke is the name of my Flaming Lips cover band. Commented Nov 30, 2017 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. Commented Nov 30, 2017 at 19:50
  • The input data returned from the plugin comes over like this: Array ( [Gourd] =&gt; 40 kilojoules [Artichoke] =&gt; 105 kilojoules [Casava] =&gt; 550 kilojoules ) The keys are not wrapped in quotes, but brackets and there are no commas separating the key/value pairs. Commented Nov 30, 2017 at 19:53
  • Well, there's this: stackoverflow.com/questions/3531857/…, but I'd rather just not use a plugin that produced its output in var_dump format, if possible. Commented Nov 30, 2017 at 20:37

2 Answers 2

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"];
Sign up to request clarification or add additional context in comments.

4 Comments

aren't you supposed to have 'enclosing your strings?
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"];
write here exactly return value of 3rd party plugin (array or string).
I included that above .. here it is again: Array ( [Gourd] =&gt; 40 kilojoules [Artichoke] =&gt; 105 kilojoules [Cassava] =&gt; 550 kilojoules )
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

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.