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++){
Related
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!
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 am trying to use a for loop where it looks through an array and tries to make sure the same element is not used twice. For example, if $r or the random variable is assigned the number "3", my final array list will find the value associated with wordList[3] and add it. When the loop runs again, I don't want $r to use 3 again. Example output: 122234, where I would want something along the lines of 132456. Thanks in advance for the help.
for($i = 0; $i < $numWords; $i++){
$r = rand(0, $numWords);
$arrayTrack[$i] == $r;
$wordList[$r] = $finalArray[$i];
for($j = 0; $j <= $i; $j++){
if($arrayTrack[$j] == $r){
# Not sure what to do here. If $r is 9 once, I do not want it to be 9 again.
# I wrote this so that $r will never repeat itself
break;
}
}
Edited for clarity.
Pretty sure you are over complicating things. Try this, using array_rand():
$final_array = array();
$rand_keys = array_rand($wordList, $numWords);
foreach ($rand_keys as $key) {
$final_array[] = $wordList[$key];
}
If $numWords is 9, this will give you 9 random, unique elements from $wordList.
See demo
$range = range(0, $numWords - 1); // may be without -1, it depends..
shuffle($range);
for($i = 0; $i < $numWords; $i++) {
$r = array_pop($range);
$wordList[$r] = $finalArray[$i];
}
I do not know why you want it.. may be it is easier to shuffle($finalArray);??
So ideally "abcdefghi" in some random order.
$letters = str_split('abcdefghi');
shuffle($letters);
var_dump($letters);
ps: if you have hardcoded array $wordList and you want to take first $n elements of it and shuffle then (if this is not an associative array and you do not care about the keys)
$newArray = array_slice($wordList, 0, $n);
shuffle($newArray);
var_dump($newArray);
You can try array_rand and unset
For example:
$array = array('one','two','free','four','five');
$count = count($array);
for($i=0;$i<$count;$i++)
{
$b = array_rand($array);
echo $array[$b].'<br />';
unset($array[$b]);
}
after you have brought the data in the array, you purify and simultaneously removing the memory array
Ok... I have NO idea why you are trying to use so many variables with this.
I certainly, have no clue what you were using $arrayTrack for.
There is a very good chance I am mis-understanding all of this though.
<?php
$numWords=10;
$wordList=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$finalArray=array();
for ($i=0; $i<$numWords; $i++) {
start:
$r=rand(0,$numWords);
$wordChoice=$wordList[$r];
foreach ($finalArray as $word) {
if ($word==$wordChoice) goto start;
}
$finalArray[]=$wordChoice;
}
echo "Result: ".implode(',',$finalArray)."\n";
I am reading a text file and putting the contents which are file id's into an array.
I then have a new array with the same id's but one extra.
My aim is to compare these two arrays and print out the new file id or file id's
The problem I have is that When I use the function array_diff, it prints everything and not the actual difference between the two arrays.
$results = array_diff($pNewList, $pSavedList);
$resultsCount = count($results);
for($x = 0; $x < $resultsCount; $x++){
echo $results[$x].'<br/>';
}
my output is printing every line even though the contents are the same in both arrays
Try this
$results = array_diff($pNewList, $pSavedList, TRUE);
$resultsCount = count($results);
for($x = 0; $x < $resultsCount; $x++){
echo $results[$x].'<br/>';
}
I have written a multiple curl and it outputs two different arrays
I'm using curl_multi_getcontent.
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent ( $curl_arr[$i] );
$results = json_decode($results,true);
/*echo "<pre>";
print_r($results);
echo "</pre>";*/
}
This outputs two arrays. if I wanted to get data from one of the arrays, how do I call the first array? or the second array?
Is there something like $results[0] or something for the first array
foreach($results[0] as $result){
echo $result['pagination'];
}
Yes, as of right now, the $result variables are overwritting each other through the for loop. Try this:
for($i = 0; $i < $node_count; $i++)
{
$results[$i] = curl_multi_getcontent ( $curl_arr[$i] );
$results[$i] = json_decode($results,true);
}
print_r($results);
You should now have two elements in your array that you can call via $results[0] or $results[1] etc...
PHP's documentation has an example using curl_multi_exec . Basically, it creates two cURL handles, adds them to the curl_multi_add_handle interface, and then runs them in parallel.