0

I have a string in a table field that looks like this:

part1=1,part2=S,part3=Y,part4=200000

To call it from the table I do this:

 while($row = mysqli_fetch_array($result)) {


        $row['mystring'];


}

My problem is that I need to separate the parts into variables for example:

From this:

part1=1,part2=S,part3=Y,someothername=200000

To This:

$part1 = '1';
$part2 = 'S';
$part3 = 'Y';
$someothername = '200000';

How can I do this?

5 Answers 5

6

Use like this

parse_str( str_replace(",", "&", "part1=1,part2=S,part3=Y,someothername=200000") );

Use with there name like:

$part1 // return 1
$part2 // return S
$part3 // return Y

works like you want, see the demo

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

Comments

1

First split string:

$s = "part1=1,part2=S,part3=Y,part4=200000";
$a = explode(",",$s);

then foreach part of string ("part1=1"...) create an array and explode as variable:

foreach($a as $k=>$v)
{
 $tmp = explode("=",$v);
 $tmp = array($tmp[0]=>$tmp[1]);
 extract($tmp);
}
echo $part1;

Comments

1

If you use PHP 5.3+ you can use array_map

<?php

$string = 'part1=1,part2=S,part3=Y,part4=200000';

array_map(function($a){
    $tmp = explode('=', $a, 2);
    global ${$tmp[0]}; //make vars available outside of this function scope
    ${$tmp[0]} = $tmp[1]; 
}, explode(',', $string));

//Variables available outside of array_map scope
echo $part1 . '<br>';
echo $part2 . '<br>';
echo $part3 . '<br>';
echo $part4 . '<br>';

?>

Comments

1

Double explode your string to get the wanted field :

$string ="part1=1,part2=S,part3=Y,someothername=200000";

foreach (explode(',', $string) as $parts) {
  $part = explode('=', $parts);
  $array[$part[0]] = $part[1];
}

var_dump($array);

Output :

 array(4) {
  ["part1"]=>
  string(1) "1"
  ["part2"]=>
  string(1) "S"
  ["part3"]=>
  string(1) "Y"
  ["someothername"]=>
  string(6) "200000"
}

I wouldn't suggest the use of variable variables to get the output as :

$part1 = ...
$part2 = ...

Using an array is probably the easier and safest way to do here. As suggested in my comment, it avoid potential conflicts with variable names.

3 Comments

You could use extract() at the end to get your variables, but I would also stick with an array
@DavidJones True, I should have specify that in that case, it's better to avoid to directly name the variables to avoid possible conflicts with already existing variables with the same name.
Yep thats true, I agree with you about keeping the array but the OP wanted variables :)
0

You can use something called 'Variable variables'. You can declare the content of a variable as a variable, if you use double dollar sign, instead of a single one. For example:

$fruit = "apple";
$$fruit = "this is the apple variable";
echo $apple;

It would output the following:

this is the apple variable

You have to be a bit more tricky with defining variables from an array, as you'd have to enclode the original variable in curly brackets. (for example: ${$fruit[0]})

So the answer to your question is:

$parts = "part1=1,part2=S,part3=Y,someothername=200000";
$parts_array = explode(",", $parts);
foreach ($parts_array as $value) {
    $temp = explode("=", $value);
    ${$temp[0]} = $temp[1];
}
echo "$part1, $part2, $part3, $someothername";

PHPFiddle link: http://phpfiddle.org/main/code/adyb-ug5n

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.