PHP > Array > assign value to text key duplicate the key - php

My code:
$a['page'] = 1;
function change($a) {
$a['page'] = 2;
}
My output:
$a['page'] = 1;
$a['page'] = 2;
Why am I get two keys 'page'?
I was expecting the function changed the value.

you could pass $a by reference instead and it would work as expected. It would be slower but not significantly so, given the function.
$a['page'] = 1;
function change(&$a) {
$a['page'] = 2;
}
change($a);
echo "<pre>";
print_r($a);

$a['page'] = 1;
function change($a) {
return $a['page'] = 2;
}
echo change($a);

Related

Create an array from pairwiseDifference function

this is my code:
$arry = (10,20,30,40,50)
function pairwiseDifference($arry, $n)
{
$diff = 0;
for ($i = 0; $i < $n - 1; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
}
}
// Driver Code
$n = sizeof($arry);
echo "<br> Percent of commisions are: "; pairwiseDifference($arry,$n); // this echo 10,10,10,10
So now i wanna make the results of pairwise funcition an array to use in another code, i tried to put:
$arru = pairwiseDifference($arry,$n)[1];
$arru1 = pairwiseDifference($arry,$n)[2];
$arru2 = pairwiseDifference($arry,$n)[3];
$arru3 = pairwiseDifference($arry,$n)[4];
echo $arru;
echo $arru1;
echo $arru2;
echo $arru3;
But are not working.
What im wrong?
Thanks in advance!
An array declaration in PHP needs to either have the array keyword or it can just be declared with square brackets []:
$arry = array(10,20,30,40,50);
or
$arry = [10,20,30,40,50];
You can get the size of an array with the count() method, and there is no need to pass it as a parameter to your function:
function pairwiseDifference($arry)
{
$n = count($arry);
// ...
}
Your function doesn't return anything, so the variables $arru, $arru1, ... won't get anything back from the function and will remain empty. In order to return an array of differences and solve your problem you can do this:
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
for ($i = 0; $i < $n; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(10,20,30,40,50);
echo "<br> Percent of commisions are: ";
// this echos 10,10,10,10 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
$arru = $diffArray[0]; // array elements start at index 0
$arru1 = $diffArray[1];
$arru2 = $diffArray[2];
$arru3 = $diffArray[3];
echo $arru; // prints 10
echo $arru1; // prints 10
echo $arru2; // prints 10
echo $arru3; // prints 10

PHP. Repeat Array Value in accordance to the number of compared Array

I tried looping in PHP but It won't work
repeat the ket strings until it reach the length of the message.
This is the code
private function initiate_encryption() // BREAKING DOWN THE MESSAGE INTO ARRAY
{
for($a = 0; $a < count($this->raw_message); $a ++)
{
$this->raw_msg_brkdown[$a] = array_search($this->raw_message[$a],$this->set); // ASSIGNING DATA TO ARRAY ACCORDING TO ALP
}
return $this->mix_passprase();
}
private function mix_passprase() // BREAKING DOWN THE PASSPHRASE INTO AN ARRAY AND ASSIGNING VALUE
{
for($a = 0; $a < count($this->passphrase); $a ++)
{
$this->raw_pass_brkdown[$a] = array_search($this->passphrase[$a],$this->set); // ASSIGNING DATA TO ARRAY ACCORDING TO ALP
}
return $this->mix_elements();
}
private function mix_elements() // THE MOMENT OF TRUTH
{
$str = count($this->raw_message);
$pass = count($this->passphrase);
for($a = 0; $a < $pass; $a += $str)
{
echo $a;
}
}

adding numbers in two different while loops

I am fetching data from database in two different while loops and I want to add the variable between them outside the loop. Example:
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a = 500; //suppose I am fetching from database
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b = 1000; //suppose I am fetching from database
}
$total = $a+$b;
echo $total;
I want to do this thing exactly but I am getting inappropriate results. Please help.
You can try this -
$a = $b = 0; // set to 0 by default
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a += 500; //Increment
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b += 1000; //Increment
}
$total = $a + $b;
echo $total;
You can try with this:
$a = 0;
$b = 0;
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a = 500; //suppose I am fetching from database
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b = 1000; //suppose I am fetching from database
}
$total = $a+$b;
echo $total;
You may want to do like this:
$total = 0;
while($cash_fetch = mysql_fetch_array($cash_qur))
{
++$total; //Increment
}
while($card_fetch = mysql_fetch_array($card_qur))
{
++$total; //Increment
}
echo $total; // this will give you a count of total fetched records

Recursive Functions and linked lists PHP

I was given a quiz by an employer to determine my ability as a programmer and the test was more or less "Write a function that counts the length of this linked list". I failed the quiz because for whatever reason my function didn't return anything (It was a timed quiz). This is my code.
class IntList{
var $value = 1;
var $next = null;
}
$A = new IntList();
$B = new IntList();
$C = new IntList();
$D = new IntList();
$A->next = $B;
$B->next = $C;
$C->next = $D;
main($A);
$count = 0;
function main($L)
{
global $count;
$final = getListLength($L, $count);
print $final;
}
function getListLength($L, $count)
{
if (isset($L->next))
{
$count++;
getListLength($L->next, $count);
} else
{
print $count;
return $count;
}
}
in getListLength im getting 3 when i print count before the return statement. But after the function returns I'm left with no output. I feel really stupid right now. Any thoughts?
Assuming this is the code from the quiz (argh, PHP4 --'):
class IntList{
var $value = 1;
var $next = null;
}
$A = new IntList();
$B = new IntList();
$C = new IntList();
$D = new IntList();
$A->next = $B;
$B->next = $C;
$C->next = $D;
I don't think you need recursion to solve that. You could just:
function getListLength($list) {
$count = 0;
$item = $list;
while($item instanceof IntList) {
$count++;
$item = $item->next;
}
return $count;
}
You just forgot to put global $count; in the second function.
Also, if you want to count the last one, you should move the $count++ outside of the conditional.
Here's a fiddle.
Alternatively, you can pass the $count variable by reference
function getListLength($L, &$count){...}
Another fiddle..
Since you are trying to use recursion here, I think the only thing that is missing is that your recursive case is not returning. You really should not need global. If you need to start at zero, you can give your getListLength a default count, or explicitly call it with zero in main.
function main($L) {
$final = getListLength($L);
print $final;
}
function getListLength($L, $count = 0) {
if (isset($L->next)) {
$count++;
// this case should return
return getListLength($L->next, $count);
} else {
return $count;
}
}

PHP: Passing parameter by reference didn't work

I'm creating a function which passing parameter by reference. I just can't get return value with this code and I need your help.
function wow ($id, &$a, &$b)
{
$detail[0][0] = 1;
$detail[0][1] = 2;
$detail[0][2] = 3;
$detail[0][3] = 4;
$detail[1][0] = -1;
$detail[1][1] = -2;
$detail[1][2] = -3;
$detail[1][3] = -4;
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
else
{
$b = $detail[$i][0];
$b = $detail[$i][1];
$b = $detail[$i][2];
$b = $detail[$i][3];
}
}
This is the way I call the function.
$a = $b = null;
wow(1, $a, $b);
echo $a[0]." ".$a[1]." ".$a[2]." ".$a[3]." ".$a[4]." ".$b[0]." ".$b[1]." ".$b[2]." ".$b[3]." ".$b[4]." ";
I think you are missing how arrays work:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
Each of these statemenets is overwriting the previous one.
Try something like this:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i];
}
That was you are copying the entire array inside the first one into $a.
Additionally, you set both $a and $b to null, yet only set one or the other in your function - you will always return at least a warning telling you about a null variable you are trying to echo.

Categories