I try to find a structure in php that will do this job:
$a = array(1,2);
$b = array(3,4);
$c = array();
$c[$a] = 100;
$c[$b] = 200;
$i = $c[$a] + $c[$b];
echo $i;
As you can see I have used arrays but this seems not to be correct because I get this:
Warning: Illegal offset type in C:\xampp\htdocs....... on line .....
What is the proper structure I should use. Can this job be done with arrays? Thanks in advance!
In PHP array keys can only be integers or strings. You could achieve what you want with a multi-dimensional array
$a = array(1,2);
$b = array(3,4);
$c = array();
$c[$a[0]][$a[1]] = 100;
$c[$b[0]][$b[1]] = 200;
$i = $c[$a[0]][$a[1]] + $c[$b[0]][$b[1]];
echo $i;
Related
I dont know if this is possible but can you create a variable loop?
I want to create many variables in an if condition
if($session[hello]){
$array1 = array("");
$array2 = array("");
$array3 = array("");
$array4 = array("");
$array5 = array("");
}
but since I have more I want to write it like this:
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array + $a = array("");
}
But its not working and I cant figure out how this might be possible.
is there an alternative?
thanks
You can try this -
if($session[hello]){
for($a = 1; $a <= 5; $a++){
${'array' . $a} = array("");
}
}
Don't why you want it this way but using an array would be better.
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array[$a] = array("");
}
}
And access it like $array[1] or $array[2] etc.
i have a simple code
data :
$data1 = array('1','2','3','4');
$data2 = array('1','2','3','4');
$data3 = array('1','2','3');
Logic :
for($a = 0; $a < count($data1); $a++){
for($b = 0; $b < count($data2); $b++){
for($c = 0; $c < count($data3); $c++){
echo $data1[$a].$data2[$b].$data3[$c].'<br>';
}
}
}
in this sample total data is 3, if i have 4 data how to build logic will generate automatically
Above code is static and fixed only for 3 array inputs if there is a condition and the $data would be more than 3 then it will not work, so how can I use the code for more or less than 3 data variables
For example, If the input data is like,
$data1 = array('1','2','3','4');
$data2 = array('1','2','3','4');
$data3 = array('1','2','3');
$data4 = array(5,6);
$data5 = array(7,8);
Then how to use the loops in that case.
What about having an array of those arrays instead and then you iterate through that array?
i.e.
$data = array(
array('1','2','3','4'),
array('1','2','3','4'),
array('1','2','3')
);
I guess you know how to do the rest...
$a = 100;
$b = 40;
$result = $a/$b;
echo $result;
for this the result is shown as 2.5 but i want it to be as 2
How to remove division result in this
you can give
$a = 100;
$b = 40;
echo floor($a/$b);
Try following code. In PHP, you need to use function intval() to convert float to integer.
$a = 100;
$b = 40;
$result = intval($a/$b);
echo $result;
Cast the result to an integer (natural number) by using the intval function.
$a = 100;
$b = 40;
$result = intval($a/$b);
echo $resulk;
Alternatively, you can use floor function http://www.php.net/manual/en/function.floor.php
$a = 100;
$b = 40;
$result = floor($a/$b);
echo $result;
For example:
$size = 0;
$array = $array;
$size = 1;
$array = $array[x];
$size = 5;
$array = $array[x][x][x][x][x];
I got a $config array that can either have 1 dimension or many. Depending on setting of the var $size the elements I need walk gonna be on that position. If size = 1, I will be looking for $config[1]. If size = 2 I will be looking for $config[1][1] ...
Thanks,
$foo = $array;
for($i=0;$i<$size;++$i) {
$foo = $foo[x];
}
$array = $array[x][x][x][x][x];
for ($x = 0; $x < 5; $x++) {
if (!is_array($array[1])) break;
$array = $array[1];
}
You can make infinite loop and reach end of array.
I have two arrays like this
array x [Firefox,IE,Chrome,Opera]
array y [40,30,25,5]
Required final [[Firefox,40],[IE,30],[Chrome,25],[Opera,5]]
I need this in PHP.I think I can run a for loop and do something like this .
final23 [0][i] = x[i];
final23 [1][i] = y[i];
Is there any better way or built in function in PHP ?
$result = array();
$size = max(count($x), count($y));
for ($i = 0; $i < $size; $i++) {
$result[] = array(
isset($x[$i]) ? $x[$i] : null,
isset($y[$i]) ? $y[$i] : null
);
}
$x = array("Mozzila","IE","Firefox","Opera");
$y = array(40,30,25,5);
$final = array();
$i = 0;
foreach($x as $a){
$final[] = array($a,$y[$i]);
$i++;
}
Learn about Associated array.
$arr = new Array("40"=>"FireFox","30"=>"IE","25"=>"Chrome","5"=>"Opera");