How to split a variable into 2 variables? - php

So, I have a text box with a form. Where people can write thing in this specific order:
1:2
2:3
3:4
7:8
And it will get sended with POST to another .php file. There everything will be getted with $_POST['input']. But now I want the following thing: That it will get ordered. So from
1:2
2:3
3:4
7:8
to
$arg['1'] = 1;
$arg1['1'] = 2;
$arg['2'] = 2;
$arg1['2'] = 3;
$arg['3'] = 3;
$arg1['3'] = 4;
$arg['4'] = 7;
$arg1['4'] = 8;
How can I do this stuff? thanks!

What you need to do is:
If there is a space breaking up each number:number, use the explode function to contain each one to an array.
Create a foreach loop to deal with each number:number element in the array. You should use the explode function again to seperate the numbers each side of the comma.
$input = "1:2 2:3 3:4 7:8";
//Will put input into an array as array("1:2", "2:3" ...)
$inputToArray = explode(" ", $input);
$arg = array();
$arg1 = array();
$i = 1;
foreach ($inputToArray as $element)
{
//Will turn element in array from 'number:number' into a new array
//(e.g array('1:1') will now be array(1, 1);
$splitNumbers = explode(":", $element);
$arg[$i] = $splitNumbers[0];
$arg1[$i] = $splitNumbers[1];
// To visibly show that it is working.
echo "arg['$i'] : " . $arg[$i] . "<br>";
echo "arg1['$i'] : " . $arg1[$i] . "<br>";
$i++;
}

Related

Add new data in foreach loop multidimensional array

I try to add new data in a multidimensional array for each loop, but it wont work.
This shows the values in my array:
$keys = array_keys($klanten);
for($i = 0; $i < count($klanten); $i++) {
foreach($klanten[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
$klanten['newData'] = 'test';
}
}
With $klanten['newData'] = 'test'; I try to add "test" to each array, but it won't work.
I also tried to use this, AFTER the script above:
foreach ($klanten as &$item ){
$item['newData'] = 'test';
}
That will work, but I think there must be an option to do it in the foreach loop above the first time. How can I do that?
Hi so you got most of it right but, you see when you are looping around a variable and adding a new item in it you have to make sure to give a index for that new array index so...
$keys = array_keys($klanten);
for($i = 0; $i < count($klanten); $i++) {
foreach($klanten[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
// $klanten['newData'] = 'test'; // instead of this
$klanten[$key]['newData'] = 'test'; // do this
}
}
This will save each and every value of newData index according to its key in $klanten array.

Select one item from array

So, I wanna select item per item from this array ["A185","A740","A540"]
Like for example I wanna select
$cie[0] = A185 or A740
with something like $cie[0] = A185
This is my code so far, since I fetch that code from a row in a MySQL table.
while ($row = pg_fetch_array($resul)) {
$cie10 = array($row["cie"]);
}
$cie = ["A185","A740"];
$values = array_count_values($cie);
$top = array_slice($values, 0, 1);
print_r($top);
What I get:
Array ( [["A185","A740","A540"]] => 1 )
It just won't work.
I'm Sure you are looking to display the data that is in the array variable
$var = ["A185","A740","A540"]; // Asume as you stored the values in the array called var
foreach($var as $x){
print_r($x);
echo "<br/>";
}
EDIT: Highlighted the code
If i understand your problem. You are looking for this:-
$Fullarray = ["A185","A740","A540"];
$cie = array_slice($Fullarray,0,2);
foreach ($cie as $name) {
$d[] = '"' . $name . '"';
}
$implodekeys = "[".implode(',',$d)."]";
$newarray[$implodekeys] =1;
echo "<pre>"; print_r($newarray);
Hope it helps!

array_diff doesn't work (PHP)

I have 2 array in my code, like the shown below:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
for($i=0;$i<$count;$i++){
for($j=0; $j<1; $j++){
$stopword[$i] = $stoplist[$i][$j];
}
}
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
var_dump($hasilfilter);
?>
$stopword contain of some stop word like attached in http://xpo6.com/list-of-english-stop-words/
All I wanna do is: I want to check if save the element that exist in array $kata and it is not exist in array $stopword
So I want to delete all the element that exist in both array $kata and $stopword .
I read some suggestion to use array_diff , but somehow it doesn't work to me. Really need your help :( Thanks.
array_diff is what you need, you are right. Here is a simplified version of what you try to do:
<?php
// Your string $kalimat as an array of words, this already works in your example.
$kata = ['I', 'just', 'want', 'to', '...'];
// I can't test $stopword code, because I don't have your file.
// So let's say it's a array with the word 'just'
$stopword = ['just'];
// array_diff gives you what you want
var_dump(array_diff($kata,$stopword));
// It will display your array minus "just": ['I', 'want', 'to', '...']
You should also double check the value of $stopword, I can't test this part (don't have your file). If it does not work for you, I guess the problem is with this variable ($stopword)
There is a problem in your $stopword array. var_dump it to see the issue.array_diff is working correct.
Try following code I wrote to make your $stopword array right:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
$stopword= call_user_func_array('array_merge', $stoplist);
$new = array();
foreach($stopword as $st){
$new[] = explode(' ', $st);
}
$new2= call_user_func_array('array_merge', $new);
foreach($new2 as &$n){
$n = trim($n);
}
$new3 = array_unique($new2);
unset($stopword,$new,$new2);
$stopword = $new3;
unset($new3);
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
print "<pre>";
var_dump($hasilfilter);
print "</pre>";
?>
I hope it helps

how to get individual element from array in php?

I have dateValues as follows,
[dateVals] = 031012,041012;
it is comma seperated values. I want to make this as array and to get individual values . As i am new to PHP , i want some one's help .
$val = array[dataVals];
for($i=0;$i<sizeof($val);$i++) {
echo "val is".$val[$i]."\n";
}
is not working
use this code
$dateVals = '031012,041012';
$pieces = explode(",", $dateVals);
for($i=0;$i<sizeof($pieces);$i++) {
echo "val is".$pieces[$i]."\n";
}
it will give you proper output.
working example http://codepad.viper-7.com/PQBiZ3
$dateVals = '031012,041012';
$dateValsArr = explode(',', $dateVals);
foreach( $dateValsArr as $date) {
}
Try this code.
Try this One
$dateVals = '031012,041012';
$date_arr[] = explode(',',$dateVals);
for($i = 0 ; $i < count($date_arr) ; $i++)
{
print $date_arr[$i].'<br />';
}

How can I do this with a loop so I dont have to do it manually for array data[]

How can I do this as a loop, so I don't have to do it manually for each element of the array?
$filename=$_POST['filename'];
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE)
{
$data[0] = mysql_escape_string($data[0]); /// customer id
$data[1] = mysql_escape_string($data[1]); /// first name
$data[2] = mysql_escape_string($data[2]); /// last name
$data[3] = mysql_escape_string($data[3]); /// email
$data[4] = mysql_escape_string($data[4]); /// phone
$data[5] = mysql_escape_string($data[5]); /// mobile
$data[6] = mysql_escape_string($data[6]); /// website
$data[7] = mysql_escape_string($data[7]); /// DOB
$data[8] = mysql_escape_string($data[8]); /// spouse first name
$data[9] = mysql_escape_string($data[9]); /// spouse last name
$data[10] = mysql_escape_string($data[10]); /// spouse cell
$data[11] = mysql_escape_string($data[11]); /// spouse email
$data[12] = mysql_escape_string($data[12]); //// created by
$data[13] = mysql_escape_string($data[13]); //// created on
}
something like:-
put this in your while loop
$count=count($data);
for ($i=0; $i< $count; $i++)
{
$data[$i] = mysql_escape_string($data[$i]);
}
array_map, array_walk, or foreach. put one (and only one!) these inside while loop
$dataSafe = array_map('mysql_escape_string', $data);
or
array_walk($data, function(&$x) {$x = mysql_escape_string($x);});
Note that array_map is like map in the FP sense, it returns a new array and you must assign. array_walk can alter your array in place when & is used
http://php.net/manual/en/function.array-walk.php
http://php.net/manual/en/function.array-map.php
foreach(&$data as $x)
$x = mysql_escape_string($x)
PS - mysql_real_escape_string is preferred
Alternatively you can put this in your while loop:
$data = array_map("mysql_escape_string", $data);
I haven't tested it but it may be faster.
edit: Added "$data = " - as jon_darkstar pointed out array_map does not change the array provided as a parameter, you need to assign it.

Categories