I have an array of data saved in cookie, like this
1:good,2:accelent,3:bad,4:good,fname:Ahmad,lname:Riaz,title:Developer,org:Magiclamp,email:Riaz#khan.com
here i want to save this data in different tables
This in one table
1:good,2:accelent,3:bad,4:good
and this in another table
fname:Ahmad,lname:Riaz,title:Developer,org:Magiclamp,email:Riaz#khan.com
how can i solve this problem
Read the cookie using $_COOKIE: $cookie_val = $_COOKIE['NAME'];
Split the input using explode(): $cookie_array = explode(",", $cookie_val);
From the result array use the values needed: $cookie_array[0], $cookie_array[1] ...
Clean the values before insertion into the db.
<?php
$str = "1:good,2:accelent,3:bad,4:good,fname:Ahmad,lname:Riaz,title:Developer,org:Magiclamp,email:Riaz#khan.com";
$rows = explode(',', $str);
$data['table1'] = $data['table2'] = array();
foreach($rows as $k => $v) {
$a = explode(':', $v);
$data[(is_numeric($a[0]) ? 'table1' : 'table2')][$a[0]] = $a[1];
}
var_dump($data);
?>
That will split the data into two arrays.
Related
I am a newbie in this and I have read lots of stuff about this matter (including some topics here), before starting this topic, but I do not quite get it yet, so I will ask for some help (if it is possible) :)
So, in the column that I want to print I have values like this on every row:
value1|value2|value5|value12|value25
value3|value5|value12|value14|value26|value32|value55
value1|value2|value14|value26|value31
The number of rows can be 3 or 1500+... So I want to merge the arrays and print those values sorted and without duplicates: value1, value2, value3, value5, value12, etc...
I have tried to explode the arrays, but I could not find out how to assign a variable to every array and merge them and all I have done is to print all values:
foreach ($rows as $areas) {
foreach (explode('|', $areas->value) as $area) {
var_dump($area);
}
}
Afterwards I have read somewhere this will be very slow if I have many rows (and I am going to have thousands), so I am stuck here and I do not know what else I could do...
I will appreciate any help and direction that you can give me, because it is too hard for me and I can not do it without help
Thank you in advance
You can store each value of your exploded string as key (if it's not an object nor array), it store only unique values. Then you have to just use array_keys() to get keys and sort returned array:
$rows = array(
'value1|value2|value5|value12|value25',
'value3|value5|value12|value14|value26|value32|value55',
'value1|value2|value14|value26|value31'
);
$results = array();
foreach ($rows as $row) {
$items = explode('|', $row);
foreach ($items as $item) {
$results[$item] = 0;
}
}
$results = array_keys($results);
sort($results, SORT_NATURAL);
Live demo on eval.in
There are two ways of doing this:
<?php
$str = 'value1|value2|value5|value12|value25';
$str1 = 'value3|value5|value12|value14|value26|value32|value55';
$str2 = 'value1|value2|value14|value26|value31';
//-- Method 1: Concat and make a single string and then explode and make a single array
$finalString = $str . '|' . $str1 . '|' . $str2;
print_r(array_unique(explode('|', $finalString)));
//-- Method 2: explode first and then merge into a single array
$strArr = explode('|', $str);
$strArr1 = explode('|', $str1);
$strArr2 = explode('|', $str2);
print_r(array_unique(array_merge($strArr, $strArr1, $strArr2)));
I have the data in my table like this
Arabic,Assamese,Azerbaijani,Belarusian
I want to show the data in an array so that I can use foreach and get the values for the array. So can someone tell me how to make it as an array and get values?
$string = "Arabic,Assamese,Azerbaijani,Belarusian";
$language_array= explode(',',$string);
Supposing you have perform the query to the database and already has the data you can use explode function. See this DEMO.
$data = $row['data_from_table']; //your data is Arabic,Assamese,Azerbaijani,Belarusian
$exp = explode(",", $data);
foreach ($exp as $value){
echo $value;
echo PHP_EOL;
}
?>
Use explode function
$database_value = $db['your_data'];
$value_array = explode(',',$database_value);
print_r($value_array);
foreach ($value_array as $value){
echo $value.'<br>';
}
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.
I have two arrays, one containing the field names which are imploded into a string called $fields, and one containing the data imploded into $data.
When data is first entered using the INSERT command the query looks like...
mysql_query("UPDATE table ($fields) VALUES ($data)")
(BTW: all data is sanitised)
My goal is to build a mysql UPDATE statement where the syntax is
mysql_query("UPDATE table SET $field1=$data1, $field2=$data2 ...")
and update all fields at once, so I need to combine the two arrays to build the alternating field/data/field/data structure instead of all of the fields followed by all of the data.
My idea is to use array_combine or array_merge and then implode into a string that will then set the function to
mysql_query("UPDATE table SET $imploded-combined-arrays")
I recognise that this won't work as the "glue" of the implode statement has two different values depending upon whether it is equating or separating field/data pairs.
How can I step through both arrays and build a string that is appropriate for the UPDATE syntax?
Thanks,
Cam
Try this
$a = array('key1', 'key2', 'key3');
$b = array('value1', 'value2', 'value3');
$c = array_combine($a, $b);
foreach($c as $key=> $value){
$result[]=$key."='". $value."'";
}
$updatefields= implode (', ', $result);
echo ("update table set " .$updatefields);
OUTPUT
update table set key1='value1', key2='value2', key3='value3'
DEMO
$names = array ('foo', 'bar');
$values = array ('hello', 'world');
$pairs = array ();
foreach ($names as $i => $name)
{
$value = $values [$i];
// $name = mysql_real_escape_string ($name);
// $value = mysql_real_escape_string ($value);
$pairs [] = "`$name` = '$value'";
}
echo ("UPDATE t SET " . implode (', ', $pairs));
For me outputs is:
UPDATE t SET `foo` = 'hello', `bar` = 'world'
I know its a strange question, but I have one input field that does a live search for locations. The final value is displayed like this:
State,Suburb,Post Code
NSW,Sydney,2210
What I need to do now is split the three values into single values and update them into my separate fields for one row.
I don't want to update multiple rows but just one.
e.g.:
fields ( state | suburb | postcode )
values ( NSW | sydney | 2210 )
What php commands would I use to split those commas off and create single $values for each item?
Use explode on the string.
$values = 'A,B,C,D';
$values = explode(',', $values);
Each item can then be accessed from an array indexed from 0.
$val = "NSW,Sydney,2210";
$valArr = explode(",", $val);
$query = "UPDATE MyTbl SET State = '$valArr[0]', Suburb = '$valArr[1]', Post_Code = '$valArr[2]' WHERE ...";
The easiest way I think, see the manual for explode:
$result_array = explode(',', $your_variable);
list($state, $suburb, $postcode) = explode(',', $value);
Would this work?
$header = "State,Suburb,Post Code"
$items = explode(",", $input)
$output = implode("|", $items)
so the output would become State|Suburb|Post Code
to access each value separately, you can use $items[0], $items[1] ..
$data = "NSW,Sydney,2210"
$items = explode(",", $input)
$output = implode("|", $items)
so the output would become NSW|Sydney|2210