Suppose I have 2d array, where only some rows actually have some values in the columns, whereas other rows don't have anything.
For example: only rows 5 and 9 have some data, other rows are empty. Number of columns is fixed - 6
I have not declared any size for the 2D array.
I want to find all fields whose value is 10.
for ($t1=0; $t1 < count($array); $t1++) {
for ($t2=0; $t2 < 6; $t2++) {
if($array[$t1][$t2] == 10) {
// do something
}
}
}
Now this code won't work because count($array) will be 2, so it will never iterate for rows 5 and 9.
I also need to get the index at which I found a match.
How can I write code to make it work in this case?
You can effectively use foreach in this case:
foreach($array as $s => $arrayElement) {
for($t=0; $t<6; $t++) if ($arrayElement[$t] == 10) {
// do something - $s is the row index, $t is the column index
}
}
I think , you have a problem in defining array. May be , you can use this script.
<?php
$cars = array(
array(
"Volvo",
22,
18
),
array(
"BMW",
15,
13
),
array(
"Saab",
5,
2
),
array(
"Land Rover",
17,
15
),
array(
"Mercedes",
22,
19
)
);
$length = count($cars);
for ($i = 0; $i < $length; $i++) {
for ($j = 0; $j < 3; $j++) {
echo $cars[$i][$j] . "</br>";
}
}
?>
I hope , it will solve your problem.
I tried to test a new thing and just saw an unusual output I am unable to get what exactly is happening within the loop can anyone explain me what happens if we take an array and assign that array to a variable we run a for loop and we provide $array and index of $array with the inititlizer for forloop something like this $array[$array[$i]] so I mean I am confused to totally explain but can you review the code and let me know what exactly is happening
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]];
}
echo $sum ;
The output 78 as it added the value but if i remove $sum+= and write down like this
echo $array[$array[$i]] . "<br />";
so now what here is I will be getting output like this
2
3
5
13
55
I am unable to get what exactly hapend within this loop
You are using the value of the array to access the key.
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]] . "<br />";
}
echo $sum ;
Loop 1:
$i = 0;
$sum += $array[$array[0]];
$sum += $array[1];
$sum += 2;
Loop 2:
$i = 1;
$sum += $array[$array[1]];
$sum += $array[2];
$sum += 3;
Loop 3:
$i = 2;
$sum += $array[$array[2]];
$sum += $array[3];
$sum += 5;
Loop 4:
$i = 3;
$sum += $array[$array[3]];
$sum += $array[5];
$sum += 13;
Loop 5:
$i = 4;
$sum += $array[$array[4]];
$sum += $array[8];
$sum += 55;
So...
$sum = 2 + 3 + 5 + 13 + 55 = 80
For $i = 0
what is $array[$i] -> $array[0] -> 1.
what is $array[$array[$i]] -> $array[1] -> 2.
And so on.
First iteration ($i = 0)
$array[$i] = $array[0] = 1
and
$array[1] = 2
Second iteration ($i = 1)
$array[$i] = $array[1] = 2
and
$array[2] = 3
Third iteration ($i = 2)
$array[$i] = $array[2] = 3
and
$array[3] = 5
Fourth iteration ($i = 3)
$array[$i] = $array[3] = 5
and
$array[5] = 13
Fifth iteration ($i = 4)
$array[$i] = $array[4] = 8
and
$array[8] = 55
When $i = 0 $array[$array[$i]] becomes $array[$array[0]] which is $array[1] since $array[0] = 1. So $array[1] evaluates to 2.
Keep thinking the same way.
For example, for $i = 4 $array[$array[$i]] becomes $array[$array[4]] which is $array[8] since $array[4] = 8. So $array[8] evaluates to 55.
Think step by step.
for example i have array like this
$x = array(
array(
array(1, 2, 3, 4),
array(5, 6, 7, 8)
)
)
and then i loop the array like this
$j = 0;
for($i = 0; $i < count($x[0][$j]); $i++){
}
if some condition occured
can i increment $j and continue/reset loop $i from 0? so $x[0][$j][0] = 5. how to do that?
currently, i tried this
$j = 0;
for($i = 0; $i < count($x[0][$j]); $i++){
if(/*somecondition*/){
$j++;
continue;
}
}
If you just want continue with $i = 0 when a condition is true you can set it to -1 when the condition is true so at the next iteration it's 0!
Like this:
if(/*somecondition*/){
$j++;
$i = -1; //So in the next iteration when it gets incrementet by 1 it's 0
}
I am having trouble trying to figure out how to get data ordered like below. The total numbers don't matter; it would follow the same pattern from any number in the logical order of 0, 1, 2, 3, 4, 5, 6, etc. So essentially, starting at 0, 2, 3, 4, etc. where 1 would be placed after the maximum number, and where 0 can be a variable I set statically. I am having issues with progressing all the way to max number and then continuing, e.g.
..., 97, 98, 99, 100, 1, 2, ...
and then progressing with the order,
..., 98, 99, 100, 1, 2, 3, ...
and so on until 1, 2, 3, 4, 5, 6, ...
and store this all into the multidimensional array below.
$set = array(
array('0','0','0','0','0','0','0','0','0','0','0'),
array('0','2','3','4','5','6','7','8','9','10','1'),
array('0','3','4','5','6','7','8','9','10','1','2'),
array('0','4','5','6','7','8','9','10','1','2','3'),
array('0','5','6','7','8','9','10','1','2','3','4'),
array('0','6','7','8','9','10','1','2','3','4','5'),
array('0','7','8','9','10','1','2','3','4','5','6'),
array('0','8','9','10','1','2','3','4','5','6','7'),
array('0','9','10','1','2','3','4','5','6','7','8'),
array('0','10','1','2','3','4','5','6','7','8','9'),
array('0','1','2','3','4','5','6','7','8','9','10'),
);
I did the above because I couldn't figure out a looping pattern; if I could figure that out I wouldn't need to enter in the data manually and could create a form by which any number could be chosen, following this pattern.
Notice that other than the first row and column, each row is just the previous shifted left, with the next value added on:
$max = 10;
// First row (full of 0)
$set = array(array_fill(0, $max + 1, 0));
$row = array();
for($i = 1; $i <= $max; $i++)
$row[] = $i;
$row[] = 1; // $row = [2,3,4,...,$max,1]
for($i = 0; $i < $max; $i++){
$set[] = array_merge(array(0), $row);
$row = array_map(function($x) use ($max){ // Requires PHP 5.3
$result = ($x + 1) % $max;
return 0 === $result ? $max : $result;
}, $row);
}
Codepad
It's of course fairly trivial to make this store strings instead of integers if you require that.
$array = array();
$max = 10;
for ($i = 0; $i < $max; $i++)
{
$num = $i + 2;
$array[$i][] = 0;
for ($j = 0; $j < $max; $j++)
{
if ($num == $max + 1)
$num = 1;
$array[$i][] = $num;
$num++;
}
}
var_dump($array);
Newbie here in PHP programming. I have some question regarding arrays. How can I get the difference between adjacent values in an array?
$value = array(2, 5, 9, 10, 19);
How do I get the difference between the 2 and 5? Then 9 and 5? Then 10 and 9, then 19 and 10?
The reason I want to get the difference is to draw a stack graph. And each stack will depend on each difference.
Update:
Hi, sorry guys, totally lost on this one. It seems that I can't do it the way I want. Please click here to see the similar stacked graph I want. How do I create a scale for my arrays so that when I plot the stacked graph it will automatically adjust to its value?
Here is the code that I am trying to use.
<?php
$img_width=200;
$img_height=425;
$img=imagecreatetruecolor($img_width,$img_height);
$bar_color=imagecolorallocate($img,0,64,128);
$line_color=imagecolorallocate($img,220,220,220);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
$value = array(116,160,210,269,325,425);
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
imageline($img,0,$value[$i]-$value[$i-1],$img_width,$value[$i]-$value[$i-1],$line_color);
}
header("Content-type:image/png");
imagepng($img);
?>
I appreciate all your help.
Just use a simple for loop:
$diffs = array();
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}
#Gumbo you added $diffs = array(); it has to be $value
<?php
$value = array(2,5,9,10,19);
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}
echo "<pre>";
print_r($diffs);
echo "</pre>";
?>