So my end result will be this (the end result will have 48 entries):
$theArray=array(
$theArray1,
$theArray2,
$theArray3,
$theArray4,
$theArray5,
$theArray6
);
I have tried a few things but I think this is the closest, but I'm still not there yet, any help appreciated.
$i = 0;
while ($i <= 48){
$theArray[]=${"theArray".$i.","}
$i++
}
$theArray[]=${"theArray".$i};
You missed ; at the end of line:
$i = 0;
while ($i <= 48){
$theArray[]=${"theArray".$i.","}; // missed ; here
$i++; // missed ; here
}
$theArray[]=${"theArray".$i};
You have to use the function compact for create an array containing variables. Try to read the documentation http://php.net/manual/en/function.compact.php
And here are some example http://www.w3schools.com/php/func_array_compact.asp
Keep it simple as below:
<?php
$i = 0;
$theArray = array();
while ($i <= 48){
array_push($theArray, '$theArray'.$i);
$i++;
}
echo '$arr = array('.implode(',', $theArray).');';
?>
Just run this on your end. Cheers!
Related
I'm currently facing a problem where I use an Array that contains other Arrays. Underneath is my current code. The problem however is that I execute 100 times the same code, only difference is the $anotherOtherIndex that I need for when I loop over an Array that is inside the Array I loop for.
It would help me a lot if somebody would have solution to reduce the amount of code needed, by eliminating the duplicate code.
for( $index = 0; $index < $endOfLoop; $index++ ) {
if(true) {
$myVariable = arrayWithArrays[$index][$anotherIndex]
x100...
}else{
$myVariable = arrayWithArrays[$index][$anotherIndex][$anotherOtherIndex]
x100...
}
Psuedo-code solution ( that would be perfect, and we all now perfect doesn't exist ;) ):
for( $index = 0; $index < $endOfLoop; $index++ ) {
$myVariable = arrayWithArrays[$index][$anotherIndex][?????]
}
You could set the main part and only go further into the array on the condition
for( $i = 0; $i < $endOfLoop; $i++ ) {
$myVariable = arrayWithArrays[$index][$anotherIndex]
if(false) {
$myVariable = $myVariable[$anotherOtherIndex]
}
}
I've this these array values :
$cart_item['addons'][0]['price'] = '52';
$cart_item['addons'][1]['price'] = '34';
$cart_item['addons'][2]['price'] = '12';
......
....
I want that each values are at 0 like :
$cart_item['addons'][0]['price'] = '0';
$cart_item['addons'][1]['price'] = '0';
$cart_item['addons'][2]['price'] = '0';
....
...
So I try this code :
for ($i=0; $i > 0 ; $i++) {
$cart_item['addons'][$i]['price'] = '0';
}
But it does not work. Thanks for your help !
Try this simple solution:
$count=count($cart_item['addons']);
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
If your array is big enough, putting count() function inside for loop is being a crazy coconut. It will be much, much slower. Please use the count outside the loop:
$count = count($cart_item['addons'])
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
You have to loop more often to achive this:
foreach($cart_item['addons'] as &$addons {
foreach($addons as &$addon) {
$addon['price'] = 0;
}
}
You can iterate over $cart_item['addons'], like so:
foreach ($cart_item['addons'] AS $key => &$value) {
$value['price'] = 0;
}
(Your code does not get executed, because $i is never changed and so is never > 0)
Also note that you need to use the reference (&$value) when changing the array in foreach loop.
There are three parts to your for loop: for(counter | test | action){}. It might be useful for you to look at this guide about for loops. You initialise your variable:
$i = 0;
then you do a logical check (the test part):
$i > 0;
If we substitute the the variable ($i) for the value it holds (0) we get:
0 > 0
which will never be true and thus you will never get to the final part (action) of the for loop:
$i++;
Instead you could make it loop until it has moved through your entire array like this:
$elementCount = count(cart_item['addons']);
for($i=0; $i < $elementCount; $i++){
$cart_item['addons'][$i]['price'] = '0';
}
Each time it loops we add one to $i until we reach the stopping condition where $i is no longer less than the number of items in the array.
It's also worth noting that PHP has a number of functions that help working with arrays.
I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).
$_POST[Var1]
$_POST[Var2]
$_POST[Var3]
How do I use a for loop to loop through and echo the values in each?
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . '$i'];
}
This does not seem to work.
Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . $i];
}
This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.
Check this. While using php varibles dont put upper commas for the variables.
<?php
$_POST["file1"];
$_POST["file2"];
$_POST["file3"];
for ($i = 1; $i <= 3; $i++){
echo $_POST['file'.$i];
}
Or this way:
$_POST["Var{$i}"];
I am running a for loop and attempting to enter data into an array.
When I run the print_r of the array, it's as if the for loop is only running once when it should be running multiple times!
for($i=0; $i<count($count); $i++){
$currentField = $array[$i];
$test = "".$field."[".$i."]";
$this->_postData[$test] = $currentField;
$this->_currentItems[$i] = $test;
}
print_r($this->_currentItems);
die();
If I echo $count before, it says 3 (for example) but still when I print the array, it only has 1 value! Any ideas?
Thanks.
Try
for($i=0; $i<$count; $i++)
i think count is not needed .
try
for($i=0; $i<$count; $i++){
I am trying this code:
for ($x = 0; $x < $numCol; $x++) {
for ($i = 0; $i < $numRows; $i++) {
$arr.$x[] = $todas[$i][$x]."\n"; //problem here
}
}
echo $arr0[0];
echo $arr1[0];
...
But i get this warning: Cannot use a scalar value as an array
and the echos do nothing. Why ? and what is the solution ?
Here's what you think you want to do. Replace your //problem here line with:
${'arr' . $x}[] = $todas[$x][$i]."\n";
But I would strongly recommend against doing that. Just use your bidimensional array.
I think you meant: ${'arr'.$x}[] instead of $arr.$x[].
$arr.$x[]
Will concatenate the string representation of $arr and $x together so you end up with something like 'Array0'[] = ... instead of $arr0[]
When you write $arr.$x[], it is equal to $arr[$x][]
Try replacing your echos by
echo $arr[0][0];
echo $arr[1][0];