0

This seems like a simple task, but I haven't been able to find a way to do this. I have an output of data stored as a string like this:

$rawData = "array('first' => '$first', 'middle' => '$middle', 'last' => '$last')";

What I simply need to do is convert it to this array:

$arrData = array('first' => "$first", 'middle' => "$middle", 'last' => "$last");

The closest I have been able to get is this shown below with print_r results:

$Data = explode(',', $rawData);

Array
(
    [0] => 'first' => '$first'
    [1] =>  'middle' => '$middle'
    [2] =>  'last' => '$last'
)

What I need is this:

Array
(
    [first] => $first
    [middle] => $middle
    [last] => $last
)

Must be something very easy I have overlooked. Please help.

2
  • I have spent a lot of time looking at similar questions on this site and looking at w3schools and other references. I know I must be overlooking something simple, but can't find it. Something more than just a -1 vote would be greatly appreciated. Thanks Commented Oct 11, 2015 at 7:13
  • I saw that this fellow had a similar question, but he was starting with the array. I have to start with the string. stackoverflow.com/questions/684553/… Commented Oct 11, 2015 at 7:51

2 Answers 2

2

Depending on where you get the string from, you may use eval. It's highly recommended to not use this function, whenever the contents of that string may be influenced directly or indirectly by a user, see hint at http://php.net/manual/en/function.eval.php. And it must contain valid PHP syntax, thus putting it into a try/catch block is necessary:

$evalArray = false;
$first = 'a';
$middle = 'b';
$last = 'c';

$rawData = "array('first' => '$first', 'middle' => '$middle', 'last' => '$last')";

try {
    $evalArray = eval($rawData);
} catch ( $e )
{
    echo "parsing failed " . $e
}

print_r($evalArray );
Sign up to request clarification or add additional context in comments.

3 Comments

this quote at the end was quite revealing! "If eval() is the answer, you're almost certainly asking the wrong question." -- Rasmus Lerdorf, BDFL of PHP Thanks again.
Right. Bt in order to re-design that, much more information is needed as disclosed in your question!
I understand. I was appreciating your caution. The whole data string comes from an on-line form. It was cleansed and encrypted before insertion into a sql db. I am pulling it out of the db, de-crypting and then trying to use the information for it's intended purpose. Anyway, the whole string fell into your warning: "whenever the contents of that string may be influenced directly or indirectly by a user". I'm glad you brought that to my attention. Thanks!
1

This does what you want ( i think ), even if its not pretty

<?php

$arr = array();
$first = 'one';
$middle = 'two';
$last = 'three';

$rawData = "array('first' => '$first', 'middle' => '$middle', 'last' => '$last')";

$arrData = explode( ',', ( str_replace( array( 'array(\'', '=>', '\')', '\'' ), array( '', '%@@%', '', '' ), $rawData ) ) );

foreach( $arrData as $val ) {
  $v = explode( '%@@%', $val );
  $arr[trim($v[0])] = trim($v[1]);
}

echo '<pre>' . print_r( $arr, true ) . '</pre>';

?>

4 Comments

Thanks for replying Tom. I get the following error:
Parse error: syntax error, unexpected '[' in: for the $arrData[] = explode( '%@@%', $val )[1];
OK, got the edit and it is working now. Thanks so much for replying. I thought there would be a much simpler way to do this!
Thanks Tom, this is working in my program now. I'm so glad to be unstuck!

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.