问题描述:

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....

网友答案:

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.

网友答案:
$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;
}
网友答案:

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

  • Arrays - http://php.net/manual/en/language.types.array.php
  • explode - http://php.net/manual/en/function.explode.php
  • Variable variables - http://php.net/manual/en/language.variables.variable.php
网友答案:

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' );
网友答案:

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
相关阅读:
Top