0

I'm converting MySQL data from one system to another, and the database structure is not quite the same. One database field contains multiple parameters in a format like this:

option1=value1
option2=value2
option3=value3

Yes, it is a text field with line breaks. I need to get the data (using PHP) from the one field and make variables out of it. For example, I need "option1=value1" converted to a variable $option1 that contains the value "value1".

I have pulled the data of this field into an array and tried imploding that data into a string that I could make variables from... but have had no luck. Either I wasn't doing it right or that's not the right direction to go.

I was trying something like this:

$string = implode("&",$array);
echo $string;

...but I just get a bunch of "Notice: Array to string conversion" messages along with an echo of "&Array" over and over. Help appreciated!

UPDATE: For further explanation, I've realized that I need a two level array with the user ID (pulled from the same table as the option values noted above) in one array... then each of those IDs having a nested array containing the option values.

2
  • Is the data literally on separate lines like that, or does that represent several rows of data? if you query the field, what does an example result look like when you var_dump it? Commented Jun 18, 2014 at 16:38
  • @cale_b I have updated the initial question... but to answer your question... I'm pulling a user ID along with this option field value into an array... and it looks like this (a piece of it at least): Array ( [0] => Array ( [uidNumber] => 1 [params] => access_optin=2 ) [1] => Array ( [uidNumber] => 1012 [params] => show_bio=1 show_url=1 show_picture=1 show_organization=1 ) [2] => Array ( [uidNumber] => 1003 [params] => access_optin=2 ) So I need to get the 'params' value into a nested array. Commented Jun 19, 2014 at 16:24

1 Answer 1

2

From your description

option1=value1
option2=value2
option3=value3

isn't an array, but a text field with line breaks. You should do

$myarray=explode("\n",$textfield);

to get a proper array.

From then on, your attempted solution seems to rely in converting that array into a query string you will pass with a GET request to another page. For that matter, you can implode the array using & as the glue.

However you also said that you need to convert array items into variables. For that purpose, you can create a new array in the form

$assocarray=[];
foreach($myarray as $item) {
  $keyvalue=explode('=',$item);
  $assocarray[$keyvalue[0]]=$keyvalue[1];
}

which will result in $assocarray being

Array [
    'option1' => value1,
    'option2' => value2,
    'option3' => value3,
];

So you could perform

extract($assocarray);

to get 3 variables called $option1, $option2 and $option3 in the global namespace.

Edit: I see that, for some reason, you have nested arrays. Something like

$myarray = Array [
                  Array [ 
                     "access_optin=2", 
                     "", 
                     ""
                  ],
                  Array [
                     "show_bio=1",  
                     "show_url=1", 
                     "show_picture=1", 
                     "show_organization=1", 
                     "access_optin=2", 
                     "access_jobtitle=0",  
                     "access_email=0"
                  ]
            ];

and the iteration should be

$assocarray=[];
foreach($myarray as $subarray) {
    foreach($subarray as $item) {
        $keyvalue=explode('=',$item);
        if(count($keyvalue)==2) $assocarray[$keyvalue[0]]=$keyvalue[1];
    }
}

Note that I'm validating the size of the $keyvalue array, because some not every value will have the name=value pattern.

Second Edit:

Since each $subarray represents a different user, you need to store the parsed key/value pair in a subarray too.

$assocarray=[];
foreach($myarray as $user => $subarray) {
    foreach($subarray as $item) {
        $keyvalue=explode('=',$item);
        if(count($keyvalue)==2) $assocarray[$user][$keyvalue[0]]=$keyvalue[1];
    }
}

This assumes your $user matches the user_id you need to set. Otherwise, you'll have to go a bit backwards to where you give form to the $myarray variable. I'm afraid you'll need to, because your example starts with zero and I bet your user_id doesn't.

Sign up to request clarification or add additional context in comments.

11 Comments

closer, but not quite yet. The data was pulled already into an array so I started with: while($rows = mysqli_fetch_assoc($options)) { $myarray[] = explode("\n", implode($rows)); } Now the options are in a regular array and no longer have the line break. The next part isn't working though... $assocarray = array(); foreach($myarray as $item) { $keyvalue = explode("=", $item); $assocarray[$keyvalue[0]] = $keyvalue[1]; } Result: Warning: explode() expects parameter 2 to be string, array given ...refers to exploding $keyvalue.
Please post a var dump of $myarray
It's very long but starts with... array(6206) { [0]=> array(3) { [0]=> string(14) "access_optin=2" [1]=> string(0) "" [2]=> string(0) "" } [1]=> array(32) { [0]=> string(10) "show_bio=1" [1]=> string(10) "show_url=1" [2]=> string(14) "show_picture=1" [3]=> string(19) "show_organization=1" [4]=> string(14) "access_optin=2" [5]=> string(17) "access_jobtitle=0" [6]=> string(14) "access_email=0"
It seems to have nested arrays. You'll need to iterate them using a second foreach.
sorry, but can you help me with how exactly to iterate this?
|

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.