Extract PHP variables from one line separated by comma - php

i'm storing data in a database column like this.
1920,1927,3772,6127,3671
and i want to extract this value to variable as many as they are.
$var1 = 1920
$var2= 1927
$var3= 3772
$var4= 6127
$var5= 3671
and automatically read any new value WHILE there is "," comma and add it to a new var

Try something like this :
$vars = '1920,1927,3772,6127,3671';
$array_vars = explode(",",$vars);
foreach($array_vars as $key => $value){
${'var' . $key} = $value;
}
echo $var1;

Use explode function
$values = "1920,1927,3772,6127,3671";
$split_to_var = explode(',', $values);
$var1 = $split_to_var[0] ; // first one
echo $var1; // Returns 1920

It's not a very good idea to store them like this, but you can do it with explode.
$ar = explode(',',$initial_var);
Now you have the $ar array with all values and you can access them as $ar[1], $ar[2] etc.

You can use explode. something like below
$str = '1920,1927,3772,6127,3671';
$arr = explode(',' , $str);
//var_dump($arr);
For accessing the values use foreach
foreach($arr as $val){
//echo $val;
}
or
$var1 = $arr[0];
$var2 = $arr[1];
$var3 = $arr[2];
$var4 = $arr[3];
$var5 = $arr[4];

It is bad relational database technique to store information in this way. Break it into a separate table with a foreign key. This will make querying a lot easier and you won't have to worry about breaking up the string.

Related

how to plug out numbers saved as string like ["3","4","5"] in php

how do i plug out the numbers in the string in php and store it in different variables
$var = '["3","4","5"]';
Expected Result:
$var1=3;
$var2=4;
$var3=5;
Actually there are many ways to do it with loop, list, extract and others etc. You can try with PHP list() to make variables out of your array values.
<?php
$var=["3","4","5"];
// Listing all the variables
list($var1, $var2, $var3) = $var;
echo "var1 is $var1, var2 is $var2 and var3 is $var3. \n";
?>
DEMO: https://3v4l.org/8sDdI
with extract() slightly modify your array
<?php
$array = array("var1" => "3","var2" => "4", "var3" => "5");
extract($array);
echo "\$var1 = $var1; \$var2 = $var2; \$var3 = $var3";
?>
DEMO: https://3v4l.org/hG3PX
You can easily convert the type :
$var=["3","4","5"];
$var1 = (int)$var[0];
$var2 = (int)$var[1];
$var3 = (int)$var[2];
Try this:
$i = 1;
$var=array("3","4","5");
foreach($var as $value){
${'var'.$i} = $value;
$i++;
}
It will work perfectly for any case. Let me know if you need any other help in this regard.

How do I concatenate each the values of a variable1 with each values of variable2

// a simpler thing that would get me what I need is:
How do I concatenate each the values of a variable1 with each values of variable2
$Var1 = 'my1, my2, my3'; // here I have dozens of entries, they are symbols
$Var2 = 'word1, word2, word3'; // here also dozens of entries, are words.
How do I have all the keys of a variable, placed together of the keys of another variable?
$Values_that_I_needed = 'my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3word3';
How would I build this values this variable up with all those values without having to type everything!?
Imagine an example with 60 my1, my2 … and 130 word1, word2 …. it’s my case!
Put each of the 60my before each of the 130 words !!
// I need to concatenate / join / join each values / keys of a variable, with all the values/keys of another variable, to avoid making all these combinations by hand. and put in another variable.
The solution using explode and trim functions:
$Var1 = 'my1, my2, my3'; // here I have dozens of entries, they are symbols
$Var2 = 'word1, word2, word3';
$result = "";
$var2_list = explode(',', $Var2);
foreach (explode(',', $Var1) as $w1) {
foreach ($var2_list as $w2) {
$result .= trim($w1) . trim($w2). ', ';
}
}
$result = trim($result, ', ');
print_r($result);
The output:
my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3word3
Below cod should work if var1 and var2 have the same length
<?php
$tab1=explode(',',$var1);
$tab2=explode(',',$var2);
$c=$count($tab1);
$output='';
for($i=0;$i<$c;$i++){
$output.=$tab1[$i].$tab2[$i].', ';
}
echo $output;
$Var1 = 'my1, my2, my3';
$Var2 = 'word1, word2, word3';
$Array1 = explode(", ",$Var1); // create array from $Var1
$Array2 = explode(", ",$Var2); // create array from $Var2
foreach($Array1 as $My){
foreach($Array2 as $Word){
$Result[] = $My.$Word; // Join Var1 & Var2
}
}
$Values_that_I_needed = implode(", ", $Result);
echo $Values_that_I_needed; // my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3word3

How to replace part of a multi array variable with a different variable?

Here is an example of what I am trying to accomplish:
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
echo $array['aaa']$subarray; // these 2 echos should be the same
echo $array['aaa']['bbb']['ccc']; // these 2 echos should be the same
It should display the same as $array['aaa']['bbb']['ccc'] i.e., "value".
This doesnt work, of course. But is there some simple solution to this?
There could be some function and the $subarrayvalue may be used as a parametr and/or as an array itself like: $subarray = array('bbb','ccc'); I dont mind as long as it worsk.
You could try something like below.
$subarray = "['bbb']['ccc']";
$temp = parse_str("\$array['aaa']".$subarray);
echo $temp;
OR To ignore single quotes -
$subarray = "[\'bbb\'][\'ccc\']";
$temp = parse_str("\$array[\'aaa\']".$subarray);
echo $temp;
Also you may refer - http://php.net/manual/en/function.parse-str.php
Just try using array chunk function http://php.net/manual/en/function.array-chunk.php
here is what actually works!!
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
$string = 'echo $array[\'aaa\']' . $subarray . ';';
eval($string);

storing multiple values in same array with same index

If I want to add multiple values in array having same index in PHP, then can it be possible to create this type of an array? For e.g.,
fruits[a]="apple";
fruits[a]="banana";
fruits[a]="cherry";
fruits[b]="pineapple";
fruits[b]="grappes";
I want array to look like as below:-
fruits = {[a]=>"apple",[a]=>"banana",[a]=>"cherry",[b]=>"pineapple",[b]=>"grappes"};
You cannot define multiple value under same key or index.
In your case -
fruits[a]="apple";
fruits[a]="banana";
Here apple will be replaced by banana.
Instead, you may define array as -
fruits[a][] = "apple";
fruits[a][] = "banana";
Edit: i updated my answer with php code, but i don't code php usually, this might not be the most optimal solution, i tried this code in a php sandbox
$subarray1[0] = "apple";
$subarray1[1] = "banana";
$subarray1[2] = "cherry";
$subarray2[0] = "pineapple";
$subarray2[1] = "grappes";
$fruits[0] = $subarray1;
$fruits[1] = $subarray2;
foreach( $fruits as $key => $value ){
foreach( $value as $key2 => $value2 ){
echo $key2."\t=>\t".$value2."\n";
}
}
use implode and explode .
subarray1[0] = "apple"
subarray1[1] = "banana"
subarray1[2] = "cherry"
subarray2[0] = "pineapple"
subarray2[1] = "grappes"
It is store data with ,(comma)
$ar="";
for($i=0;$i<=count(subarray1);$i++)
{
$ar[]=subarray1[$i];
}
$rt=implode(',',$ar);
echo $rt;
It is Remove ,(comma) form array
$ex=explode(",",$ar);
print_r($ex);

How to pass an array of variable names to list()

I'm trying to pass an array of variable names to be assigned to the list() function, and I'm wondering if it's even possible. My intention is for list($variables) to parse $values.
$variables = array("$var1","$var2","$var3");
$values = array('Value1','Value2','Value3');
//Can I simply pass an array of variable names to be assigned here
list($values) = explode("&", $values);
To clarify, my intention is to have PHP execute this:
list($var1, $var2, $var3) = explode("&", $values);
You'd have to do a little trickery, but with array_combine and extract you could achieve the same effect:
$keys = array("var1","var2","var3");
$values = array('Value1','Value2','Value3');
extract(array_combine($keys, $values));
echo $var1; //"Value1"
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
http://php.net/manual/en/function.list.php
list is used to assign a list of variables in one operation. This is the sytax:
$string = 'VarA, VarB, VarC';
list($varA, $varB, $varC) = explode(',', $string);
When u use variables you can need to keep the quotes away so the second is the right one.
$array = ($var1, $var2, $var3);
You dont need to use explode anymore cause you already have an array. explode creates an array. In your example it would be:
$array = array($var1, $var2, $var3);
list($varA, $varB, $varC) = $array;
Note that i changed the array syntax. You forgot the array keyword.
I understand you want to explode the response variable and assign each value to the corresponding variable.
You can achieve it this way instead of using list:
$varnames = array('var1','var2','var3');
$varvals = explode('&',$response);
foreach($varnames as $k => $v){
$$v = $varvals[$k];
}
This way $var1 will have the first value in response, $var2 the second, and so on.
You can't do that. Why you don't use the second your variant?

Categories