extract elements from php array and assign as variables - php

I have been working on this for a while and cannot seem to figure it out at all. Any help would be appreciated. here we go.
I have an html form that has a text box and a submit button. the text entered in the text box is posted to my .php processor form. Once it gets here, I use:
$textdata = $_POST['textdata'];
$input = explode("\n", $textdata);
this takes the data, splits it by line, and stores each line in an array called $input.
from here i can echo $input[0] to get the first line and so on. But I need to use this further down in my script and need to assign a variable to the first line, or $input[0].
$input[0] = $line1; does not work. I think I might have to use extract() and a foreach loop? Any help would be greatly appreciated. thanks!

well fo one thing $input array will always be available, or what you can do if i understand correctly is:
$textdata = $_POST['textdata'];
$input = explode("\n", $textdata); //this should have the array of lines assuming
//that $textdata was \n delimited
$line1 = $input[0]; //use $line1 later in code

$line1 = $input[0];
$line2 = $input[1];
$line3 = $input[2];
// etc.
or:
for ($i=0, $inputlen = count($input); $i < $inputlen; $i++) {
${'line'.($i+1)} = $input[$i];
}
or simply:
list($line1, $line2, $line3) = $input;

$input[0]. $input[0] = $line1;
I can't tell if the full-stop in that line is a full-stop or concatenation operator.
For concatenation, it should be this way.
$input[0] = $input[0] . $line1;
or even shorter
$input[0] .= $line1;
If you're just wanting to assign $input[0] to $line1 by value, it's
$line1 = $input[0];
You can also assign a reference using
$line1 =& $input[0];
Using the latter, any changes to $line1 will be present in $input[0].

Related

Parts of variables save to another var

I have this autogenerated code:
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
How can I save to a var the first and the last part of this code like this?
$first = "k9sdhfkr9235kdh5";
$last = "fdh4hwshnchjgrj";
The code is always separated by this character (|) and the code consists random character number so sometimes it is 16 characters, sometimes 11 etc...
foreach($code as $v){
$pos = strpos($v, "|");
$first = substr($v,...?
You may use the explode
explode("|",$code );
It will return an array of values
You can use builtin functionality called explode(); given below the solution for your question
<?
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
$codeArr=explode("|",code);
$first = $codeArr[0];
$last = $codeArr[1];
?>
Please try this. More about the explode function is here

Compare string from input field with value in multidimension array

$klasseinput = strtoupper(trim($_POST["klasseliste"]));
$data = file('student.txt');
$data = array_filter($data);
foreach($data AS $row){
$student[] = explode(';', $row);
}
$antall = count($student);
for ($i = 0; $i < $antall; $i++){
if($klasseinput == $student[$i][3]){
print('<tr><td>'.$student[$i][0]."</td><td>".$student[$i][1]."</td><td>".$student[$i][2]."</td><td>".$student[$i][3]."</td></tr>");
}
}
/////////STUDENT.txt//////////
ph;petter;hanssen;IT1
gb;Geir;Bjarvin;IT2
mj;Marius;Johansen;IT3
/////////////////////////////
I am trying to compare an input form with an item in the multidimension array, but even tho the variable from the input field is exactly the same as the value in the array, it doesnt pass the if check.
$student[0][3] = IT1
$student[1][3] = IT2
$student[2][3] = IT3
If you have made sure there is no white space spoiling the comparison, then you might find a function like this useful to look at the strings on both sides of the comparison. You might find there are spurious characters causing trouble.
function hexdump($str)
{
for($i=0; $i<strlen($str);$i++)
{
echo "[$i] [".bin2hex($str[$i])."] [".$str[$i]."]<br />";
}
}
For instance, the string read from the file might contain CR LF characters. You could get rid of them using str_replace().
Thanks to Ravinder Reddy for the answer that was simple and worked for me:
" trim the value for \t\n if($klasseinput == trim($student[$i][3])){ "

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);

php reading in strings from text area

$data = explode("\n", trim($_GET['names']));
while($i < count($data)) {
if(!in_array($data[$i], $unique_names)){
$unique_names[] = $names[$i];
}
$i = $i + 1;
}
I am trying to accept only unique results from a textarea using this php code. It is not working because every string has an extra blank space, except for the last one.
I.E.
"jeff "
"fred "
"bill "
"jeff"
so jeff will be added twice.
My question is:
How Do I get rid of that space?? I tried trim and it won't perform as intended.
First of all, you don't need to find unique values manually, you can use array_unique(). Secondly, trim() doesn't work on arrays.
Try:
$raw_names = explode("\n", $_GET['names']);
$trimmed_names = array_map('trim', $raw_names);
$unique_names = array_unique($trimmed_names);
Use trim(string) to remove whitespace from the start and end. http://php.net/manual/en/function.trim.php
Inspired by kba :
$data = explode("\n", trim($_GET['names']));
foreach ($data as &$e) {
$e = trim($e);
}
$unique_names = array_unique($data);

Assign variable to values in array php

This is what I want to do:
Split a word into separate charachters. The input word comes from a form and can differ from each user.
Assign variables to each charachter so that i can manipulate them separately.
Her's my code so far (which doesn't work). Apoligize if ther's a lot of stupid mistakes here, but I am new to PHP.
<?php
$word = $_POST['input'];
//split word into charachters
$arr1 = str_split($word);
//assigning a variable to each charchter
$bokstaver = array();
while($row = $arr1)
{
$bokstaver[] = $row[];
}
$int_count = count($bokstaver);
$i=0;
foreach ($bokstaver as $tecken) {
$var = 'tecken' . ($i + 1);
$$var = $tecken;
$i++;
}
?>
I'd like to end up with as many $tecken variables (With the names $tecken, t$tecken1, $tecken2 etc) as the number of charachters in the input.
All help much appreciated, as always!
You don't need to create separate variables for each letter because you have all the letters in an array. Then you just index into the array to get out each letter.
Here is how I would do it.
//get the word from the form
$word = $_POST['input'];
//split word into characters
$characters = str_split($word);
//suppose the word is "jim"
//this prints out
// j
// i
// m
foreach($characters as $char)
print $char . "\n"
//now suppose you want to change the first letter so the word now reads "tim"
//Access the first element in the array (ie, the first letter) using this syntax
$characters[0] = "t";
I dont think its a good idea, but heres how you do it:
<?php
$input = 'Hello world!';
for($i = 0; $i < strlen($input); $i++) {
${'character' . $i} = $input[$i];
}
why do you want that?
you can just go with:
$word = 'test';
echo $word[2]; // returns 's'
echo $word{2}; // returns 's'
$word{2} = 'b';
echo $word{2}; //returns 'b'
echo $word; // returns 'tebt'
...

Categories