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.
Related
The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.
$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];
I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.
This is what i tried
<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
$e = 0;
for($i=0;$i<$c;$i++){ //b
for($j=0;$j<$d;$j++){
if($b[$i] != $a[$j]){
$z[$d+1] = $b[$i];
break;
}else{
//$z[$e] = $a[$j];
}
}
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}
//echo"<pre>ans"; print_r(unique_names($a,$b));
?>
Also i made it work using in_array but was later told that even that function is not allowed.
<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
if(! in_array($b[$i], $a)){
$z[$d+1] = $b[$i];
}
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
print_r(unique_names($a,$b));
?>
You can use next code:
<?php
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$values = [];
// Add values from first array
foreach($a as $v) {
$values[$v] = true;
}
// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
$values[$v] = true;
}
// Transform keys to plain result
foreach($values as $key=>$val) {
$result[] = $key;
}
var_dump($result);
Execute PHP online
I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;
There are three numbers in set A {3,4,7} and in set B {2,4,7}.
It is not possible to get the result true because the the first numbers in a and b are not same.
But I need to get the result as true by comparing other two number and leaving the first number.
How is it possible to do it in PHP?
try this it will helps you.. it will display the 1 => for more number of combinations as same and 0 => for more of combination as different . in the below code the more number of combination as different so its returns 0.
<?php
$A = array(2,3,7,5,6);
$B = array(4,3,7,8,9);
$flagTrue = 0;
$flagFalse = 0;
for($i=0; $i < count($A); $i++)
{
if($A[$i] == $B[$i])
{
$flagTrue=$flagTrue+1;
}
else
{
$flagFalse=$flagFalse+1;
}
}
$var_is_greater_than_two = ($flagTrue >= $flagFalse) ? 1 : 0;
echo $var_is_greater_than_two;
?>
<?php
$a = array(3,4,7);
$b = array(2,4,7);
echo $a === $b ? 'TRUE' : 'FALSE';
echo PHP_EOL;
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
?>
Shows:
FALSE TRUE
UPD:
If you need to extract values from strings, then:
$strA = '3,4,7';
$strB = '2,4,7';
$a = explode(',', $strA);
$b = explode(',', $strB);
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
Should work.
<?
$A = array(2,3,7);
$B = array(4,3,7);
$isTrue=1;
for($i=1; $i < count($A); $i++) if($A[$i]!=$B[$i]) $isTrue=0;
echo $isTrue;
?>
EDIT:
If you want to return true if exactly two elements are the same, then the code would be:
$common=0;
for($i=0; $i < count($a); $i++) if($a[$i]==$b[$i]) $common++;
if($common==2) $isTrue=1;
function compareSets($a, $b) {
$result = TRUE;
$diffArray = array_diff($a, $b);
foreach ($diffArray as $key => $value) {
if ($key > 0) {
$result = FALSE;
break;
}
}
return $result;
}
<?php
$a = array(3,4,7);
$b = array(2,4,7);
for($i=0;$i<3;$i++)
{
if($a[$i] > $b[$i]
echo true;
}
?>
will give true if a is greater.
The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>
The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.
The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing