-1

how to get all possible variable values in separate variables..... separated with commas

$bin="1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";

how to get all possible variables something like this.

$NN01="1";
$NN02="2";
$NN03="3";
$NN04="4";
$NN05="5";
$NN06="6";
$NN07="ABC";
$NN07="AAA";
$NN08="77";
$NN09="00";
$NN10="11";
$NN11="AS";
$NN12="Code";

above sting can be separated with any operator other than comma thanks for your help....

4
  • 3
    What determines the name of the end variable? Should it simply be $NNxx? What if there are more than 100 variables... $NNxxx? Commented Aug 24, 2012 at 0:41
  • 2
    Creating such repetitive variables is silly. Use an array. Be a better PHP Developer. Commented Aug 24, 2012 at 0:43
  • While you guys are probably right to question the application here, I don't think that it is impossible the OP has their reasons for wanting to do this. Suggest a better route, sure, but do so with the understanding that you are guessing the use case, and might be completely wrong in your assumption. Commented Aug 24, 2012 at 0:52
  • 1
    Checkout this other Question that should answer yours: stackoverflow.com/questions/11258665/… Commented Aug 24, 2012 at 1:31

5 Answers 5

6

You won't be able to do that, but you can explode them to an array.

$bin="1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";
// any delimiter in place of ','
$arr = explode(',',$bin);
echo $arr[0];

Edit: Okay, it's possible. I wrongly stated that you won't be able to do that.

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

9 Comments

Downvoter want to explain themselves?
Well, you could be able to do it using eval()... but your solution is of course much better. (I'm not the downvoter)
+1 using an array is the way to go.
@LonelyWebCrawler One could, but one shouldn't.
In all fairness, this answer doesn't address the question as asked, and it wrongly states that the OP "won't be able to do this", implying that it cannot be done. It can. Downvote-worthy, probably not, but this isn't an answer to the question as asked, strictly speaking.
|
3
$bin = "1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";
list ($NN01,$NN02,$NN03,$NN04,$NN05,$NN06,$NN07,$NN07,$NN08,$NN09,$NN10,$NN11,$NN12) = $bin;

or for something completely dynamic

$i = 0;
foreach ($bin as $val) {
    $temp = 'NN' . str_pad(++$i, 2, '0');
    $$temp = $val;
}

Comments

2

You can use the explode function to turn the initial value into an array:

$bin="1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";
$array_of_values = explode(',' $bin);

That will give you a numerically indexed array, which looks like this:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => ABC
    [7] => AAA
    [8] => 77
    [9] => 00
    [10] => 11
    [11] => AS
    [12] => Code
)

You can just use that and access individual values by their index:

echo $array_of_values[0]; // 1

See more about arrays: http://php.net/manual/en/language.types.array.php

From there, if you want to set the values into variables, you can use this loop, which uses "variable variables":

$bin= "1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";
$array_of_values = explode(',', $bin);
$vn = 'NN';
foreach ($array_of_values as $k=>$v) {
  ${$vn.$k} = $v;
}
echo 'NN1:'.$NN1; // NN1:2
echo 'NN2:'.$NN2; // NN2:3
echo 'NN2:'.$NN3; // NN3:4

Try it: http://codepad.org/Ee8F9F18

Documentation

Comments

2

Another idea is to use extract, and provide a desired prefix.

$bin="1,2,3,4,5,6,ABC,AAA,77,00,11,AS,Code";

This will give you $NN_0 .. $NN_11, where 'NN' is the prefix you chose.

extract( explode( ',', $bin ), EXTR_PREFIX_ALL, 'NN' );

1 Comment

Nice! I had thought about this route too, but forgot about the prefix argument and didn't want to loop the array to add keys. Well played :) (OTOH, extract is very evil)
1

Not sure what you are trying to accomplish, but why not to use array() ?

It works simple.

$YourArray = array ( "NN01" => "1", "NN02" => "2") // And so on

2 Comments

He probably doesn't want to hardcode it...
Wasn't sure as the real question wasn't too clear. Seeing other answers make it a lot clearer now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.