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.

user1028866
  • 795
  • 2
  • 8
  • 19
  • 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 – user1028866 Oct 11 '15 at 07:13
  • I saw that this fellow had a similar question, but he was starting with the array. I have to start with the string. http://stackoverflow.com/questions/684553/convert-php-array-string-into-an-array – user1028866 Oct 11 '15 at 07:51

2 Answers2

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 );
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • 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. – user1028866 Oct 11 '15 at 08:06
  • Right. Bt in order to re-design that, much more information is needed as disclosed in your question! – Axel Amthor Oct 11 '15 at 08:12
  • 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! – user1028866 Oct 11 '15 at 08:40
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>';

?>
Tom
  • 691
  • 4
  • 7