links from an array to external variables? - php

How to make references to external variables from an array?
For example: if I change the array values ​​then
these values ​​should be stored in variables
referenced by array elements.
I tried to implement this idea, but it didn't work :(
According to my idea:
echo $one should output 1
echo $two should output 2
$one;
$two;
$link = [$first = &$one, $second = &$two];
$link[0] = 1;
$link[1] = 2;
echo $one; // does not show 1
echo $two; // does not show 2

You should structure your code like so:
$one;
$two;
//you did not initialize the $first and $second if you want them to be the keys like so
//$link = [$first => &$one, $second => &$two];
$link = [&$one, &$two];
$link[0] = 1;
$link[1] = 2;
//if you need to initialize them to another variable then do
//And it should come after you have assigned a value to $link[0] and $link[1]
$first = $link[0];
$second = $link[1];
echo $one; // show 1
echo $two; // show 2
echo $first; // show 1
echo $second; // show 2

Related

How to split a variable into 2 variables?

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++;
}

PHP variables not changing outside of function

I have a function in PHP. I am passing two arrays to the function as references. While the function updates $array2 outside the function, it does not update $array. Below is my function.
function JobAssign($i,&$array,&$array2,$Machine_Interchange) {
$compmachine = $array[$i]['Machine_Name'];
$CLT = $array[$i]["Estimated_Start_Date"];
$ET = $array[$i]["Estimated_Completion_Date"];
$Machine = $array[$i]['Machine_Name'];
$Compatibility_sql = "SELECT Machine2 FROM Machine_Interchange where Machine1 = '$Machine' and Value = 1";
$compatibility_query = mysql_query($Compatibility_sql) or die(mysql_error());
for($i=0; $compatibility[$i] = mysql_fetch_assoc($compatibility_query);$i++);
array_pop($compatibility);
$maxkey = max(array_keys($compatibility));
$maxnumber = max(array_keys($array2))+1;
$k=0;
do{
$compmachine = $compatibility[$k]['Machine2'];
$CLT = $array2[$compmachine];
if($array2[$compatibility[$k]['Machine2']]>$array2[$compatibility[$k+1]['Machine2']]) {
$compmachine = $compatibility[$k+1]['Machine2'];
$CLT = $array2[$compatibility[$k+1]['Machine2']];
}
$k=$k+1;
}
while($k<$maxkey);
$array[$i]["Machine_Name"] = $compmachine;
$array[$i]["Estimated_Start_Date"] = $CLT;
$prodhours = $array[$i]["Prod_Hrs"];
$array[$i]["Estimated_Completion_Date"] = date('Y-m-d H:i:s', strtotime($CLT . " +$prodhours hour"));
$array2[$compmachine] = $array[$i]["Estimated_Completion_Date"];
echo $array[$i]['Machine_Name'].'<br>';
echo $array[$i]['Estimated_Start_Date'].'<br>';
echo $array[$i]['Estimated_Completion_Date'].'<br>';
echo $array2[$compmachine].'<br>';
}
JobAssign(0,$Jobs_Data,$LT,$Machine_Interchange);
echo $Jobs_Data[0]['Machine_Name'].'<br>';
echo $Jobs_Data[0]['Estimated_Start_Date'].'<br>';
echo $Jobs_Data[0]['Estimated_Completion_Date'].'<br>';
echo $LT['Machine 3'];
Below is the output that I get.The first 4 lines of the output are from within the function and the next 4 are from outside the function.
Machine 3
2014-07-12 00:00:00
2014-07-15 11:00:00
2014-07-15 11:00:00
Machine 1
2014-07-30 00:00:00
2014-08-02 00:00:00
2014-07-15 11:00:00
When I echo the changed variables inside the function they do change but when I echo $arrayoutside of the function it does not change the values for $array. Only the values for $array2 have changed even though I have passed both arrays as reference in the function arguments. Which means that the line 4 and 8 are the same in the output which means that values for $array2 have changed but not for $array.
Try changing the variable $i on your for loop to another variable. Because it changes the value of the variable while it is looping.
function JobAssign($i,&$array,&$array2,$Machine_Interchange)
{
$compmachine = $array[$i]['Machine_Name'];
$CLT = $array[$i]["Estimated_Start_Date"];
$ET = $array[$i]["Estimated_Completion_Date"];
$Machine = $array[$i]['Machine_Name'];
$Compatibility_sql = "SELECT Machine2 FROM Machine_Interchange where Machine1 = '$Machine' and Value = 1";
$compatibility_query = mysql_query($Compatibility_sql) or die(mysql_error());
for($ii=0; $compatibility[$ii] = mysql_fetch_assoc($compatibility_query);$ii++);
array_pop($compatibility);
$maxkey = max(array_keys($compatibility));
$maxnumber = max(array_keys($array2))+1;
$k=0;
do{
$compmachine = $compatibility[$k]['Machine2'];
$CLT = $array2[$compmachine];
if($array2[$compatibility[$k]['Machine2']]>$array2[$compatibility[$k+1]['Machine2']])
{
$compmachine = $compatibility[$k+1]['Machine2'];
$CLT = $array2[$compatibility[$k+1]['Machine2']];
}
$k=$k+1;
}
while($k<$maxkey);
$array[$i]["Machine_Name"] = $compmachine;
$array[$i]["Estimated_Start_Date"] = $CLT;
$prodhours = $array[$i]["Prod_Hrs"];
$array[$i]["Estimated_Completion_Date"] = date('Y-m-d H:i:s', strtotime($CLT . " +$prodhours hour"));
$array2[$compmachine] = $array[$i]["Estimated_Completion_Date"];
echo $array[$i]['Machine_Name'].'<br>';
echo $array[$i]['Estimated_Start_Date'].'<br>';
echo $array[$i]['Estimated_Completion_Date'].'<br>';
echo $array2[$compmachine].'<br>';
}
The $array is changed as it should, the problem is that is changed in the wrong index.
Explanation:
You call the function with $i = 0 but $i is also used in the loop statement and exits the loop with a different value.
here is the line:
for($i=0; $compatibility[$i] = mysql_fetch_assoc($compatibility_query);$i++);
after the above $i is no longer 0 (as long as you have something to fetch)
So the index of $array that you echo inside the function is different from the index of $array you echo outside the function.
To fix your problem just change the variable of the above loop to something else, let's say $j:
for($j=0; $compatibility[$j] = mysql_fetch_assoc($compatibility_query);$j++);
so that no longer affects $i

Return some values with PDO in function

i am using PDO to get some values of a table like : (table name is ban)
ID word
1 one
2 two
3 three
4 four
MY function is :
function retBans() {
global $connect;
$result = $connect->prepare("SELECT * FROM ban");
$result->execute();
$a = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$a = $row['word'].",";
}
return $a;
}
and in the main php file, i wanted to get them back with this code :
$a = array();
$a = retBans();
$b = explode(",",$a);
print_r($b);
I wanted to have this :
Array {
[0] => one
[1] => two
[2] => three
[3] => four
}
But , it just return and print_r the last value (four) in array.
How can i get them like i said ?
Use this instead -
$a = '';
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$a .= $row['word'].",";
}
Then, you can use explode function
$a = retBans();
$b = explode(",",$a);
echo "<pre>"; print_r($b);

print name using for loop php

Using for loop in PHP can we have numbers associate with a variable name?
ex:
$name1="hi";
$name2="khj";
for($i=0;$i<=2;$i++)
{
echo ..
}
How can we print $name1 and $name2 using for loop?
Thanks!
Yes this is called variable interpolation.
$name1="hi";
$name2="khj";
for($i=1;$i<=2;$i++) {
$var = 'name' . $i;
echo $$var;
}
Note: There are multiple syntaxes for variable interpolation in PHP. Also, I modified your loop to start at 1.
for($i = 1; $i <= 2; $i++)
{
echo $name{$i};
}
It would be way easier to put it in an array though, that's what we have them for.
$names = array();
$names[1] = 'A';
$names[2] = 'B';
foreach($names as $name)
{
echo $name;
}
put this in the for loop:
echo ${'name'.$i}."\n";
Better to use something like:-
$names[] = $name1;
$names[] = $name2;
foreach($names as $name){
echo $name;
}
This print the name1 and name2 .And i value must be start from 1
for($i=1;$i<=2;$i++)
{
echo ${'name'.$i}."<br>";
}

php string name as variable

$string = "id";
want result to be like
$id = "new value";
How do I code this in php?
Edit..
How about the below?
$column = array("id","name","value");
let say found 3 row from mysql
want result to be like this
$id[0] = "3";
$id[1] = "6";
$id[2] = "10";
$name[0] = "a";
$name[1] = "b";
$name[2] = "c";
$value[0] = "bat";
$value[1] = "rat";
$value[2] = "cat";
Theres 2 main methods
The first is the double $ (Variable Variable) like so
$var = "hello";
$$var = "world";
echo $hello; //world
//You can even add more Dollar Signs
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a
$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
//... and so on ...//
#source
And the second method is to use the {} lik so
$var = "hello";
${$var} = "world";
echo $hello;
You can also do:
${"this is a test"} = "works";
echo ${"this is a test"}; //Works
I had a play about with this on streamline objects a few weeks back and got some interesting results
$Database->Select->{"user id"}->From->Users->Where->User_id($id)->And->{"something > 23"};
You are looking for Variable Variables
$$string = "new value";
will let you call
echo $id; // new value
Later in your script
Second answer in response to your edit:
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$i = 0;
$id = array();
$name = array();
$value = array();
if ($num > 0) {
while ($row = mysql_fetch_assoc($result)) {
$id[$i] = $row['id'];
$name[$i] = $row['name'];
$value[$i] = $row['value'];
$i++;
}
}
This loops around your result, using the counter $i as the key for your result arrays.
EDIT
Additional answer in response to your comment:
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $column_name => $column_value) {
$temp_array[$column_name][$i] = $column_value;
}
$i++;
}
foreach ($temp_array as $name => $answer) {
$$name = $answer;
}
This code creates a temporary multidimensional array to hold the column names and values the loops around that array to create your variable variable arrays. As a side not I had to use the temp array as $$column_name[$i] doesn't work, I would love to see alternative answers to this problem.
Final note #Paisal, I see you have never accepted an answer, I wouldn't have put this much effort in if I had seen that before!
You can do this
$$string = "new value";
juste double $
Are you referring to variable variables?
That would accomplish something like this:
$string = "id";
$$string = "new value";
This produces a variable $id with the value "new value".
Don't do that. Just use an array.
$arr[$string] = 'new value';
ref: How do I build a dynamic variable with PHP?
Try this :
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
$i = 0;
if ($num_rows) {
while ($row = mysql_fetch_assoc($result)) {
foreach($row AS $key => $value) {
${$key}[$i] = $value;
}
$i++;
}
}
For those of us who need things explained in great detail...
// Creates a variable named '$String_Variable' and fills it with the string value 'id'
$String_Variable = 'id';
// Converts the string contents of '$String_Variable', which is 'id',
// to the variable '$id', and fills it with the string 'TEST'
$$String_Variable = 'TEST';
// Outputs: TEST
echo $id;
// Now you have created a variable named '$id' from the string of '$String_Variable'
// Essentially: $id = 'Test';

Categories