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.
Related
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;
I'm looking for something like this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1);
$j = array_push($a, $var2);
echo $i;
echo $j;
The expected output would be:
0
1
I want to know the index of the object I just inserted, to be able to find it quickly afterwards. I think array_push gives me the size of the resulting array, not the index for the recently inserted element
array_push return new number of elements in the array, so decrement the return value by 1
Try this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1) - 1;
$j = array_push($a, $var2) - 1;
echo $i;
echo $j;
function my_push_array(&$array, $value){
$array[] = $value;
end($array);
return key($array);
}
$a = ['h','e','l','l'];
echo my_push_array($a, 'o'); //returns 4
How to create array from variable php using loop for ?
i have many variable php like
$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;
i want to create array like this
$ar = array('1','2','5','2','6');
but using loop for like
for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
Not a recomended way of doing things but:
$arr = array();
for($i=0;$i<5;$i++) {
$varName = 'number_'.$i;
$arr[] = $$varName;
}
for ($i = 1; $i <= 6; $i++)
$ar[] = $i;
for ($i=1;$i<7;$i++)
{
$ar[] = $i;
}
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...
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");