When I run the code given below, it outputs nothing in the browser.
What Am I doing wrong? Please help.
$ages = [0, 4, 8, 12, 16, 20];
while($age = current($ages)){
echo $age.",";
next($ages);
}
To fix your current problem, when you use current($ages) in the first iteration of the loop, this will return the first item in the list - which is 0. As the while loop only continues whilst the value is not false (which 0 also counts) the loop will not be run, so...
while(($age = current($ages)) !== false){
echo $age.",";
next($ages);
}
alternatively, use a foreach loop...
foreach ( $ages as $age) {
echo $age.",";
}
The first test made is while (0), because the first value is 0. So, the loop is never executed.
You can use a foreach() loop instead
$ages = [0, 4, 8, 12, 16, 20];
foreach ($ages as $age) {
echo $age . ',';
}
Or just implode() for a better result (because it will not have a trailing ,).
echo implode(',', $ages);
Related
I'm having trouble understanding how the foreach loop works in this code. My understanding is that $Score is assigned the arrays of $Die1 + $Die2, but how does $Score gain the value of 1,2,3,4,5,6,5,4,3,2,1? Shouldn't it be 1,2,3,4,5,6,1,2,3,4,5,6 in a sequence since that's how the values are listed in the $FaceValues array? Is there something that's happening in the foreach statements that I'm missing? Can anyone could elaborate on the double foreach statements here?
$FaceValues = array(1, 2, 3, 4, 5, 6);
$ScoreCount = array();
for($PossibleRolls = 2; $PossibleRolls <= 12; ++$PossibleRolls){
$ScoreCount[$PossibleRolls] = 0;
}
foreach ($FaceValues as $Die1) {
foreach ($FaceValues as $Die2) {
// all possible combinations
++$RollCount;
// increment RollCount
$Score = $Die1 + $Die2;
// 1,2,3,4,5,6
// 6,5,4,3,2,1
++$ScoreCount[$Score];
}
}
foreach ($ScoreCount as $ScoreValue => $ScoreTimes){
echo "<p> A combined value of $ScoreValue occured $ScoreTimes of $RollCount times. </p>";
}
On the first iteration $Die1 is 1 and $Die2 is 2. So it begins at 2 and goes up to 7. Then the inner loop is finished and the outer loop proceeds to set $Die1 to 2. You should then see the number 3 through 8.
You can use echo or var_dump() and exit() to print the values at each step and stop if you're having trouble following-along.
I want to echo one element of the array like sum1. But I only get one letter like s. Please solve this problem.
$nums = array("sum1", 100, 200);
foreach ($nums as $value) {
echo $value[0];
echo $value[1];
}
If you want to just echo 1 item from that array, you should to it like this:
echo $nums[0];
If you want to loop through all of them, and show each, do it like so:
$nums = array("sum1", 100, 200);
foreach ($nums as $value) {
echo $value."<br>";
}
What you did wrong
You had already looped through the array, so you had a string. You can select the first letter from a string like in this example:
$string = "A string";
echo $string[0];
Will return A, as it's the first index of that string. That is essentially what you did in your loop.
You made your String an array, and it showed the index's you selected to be shown. You can read this where the questions asks how to do this. I hope this gives some more clearity.
If you want every element of array,
then,
For your array,
$nums = array("sum1", 100, 200);
$nums[0] will be sum1
$nums[1] will be 100
$nums[2] will be 200,
Now your loop,
foreach ($nums as $value) {
// here echo $value values are like 'sum1', 100, 200 will be printed.
// by default string will be considered as array,
// if you print $value[0], $value[1], $value[2], $value[3] for sum1, it will return, s, u, m, 1 respectively.
// and integers will be considered as pure value, which you will get in $value only, not in $value[0], ....
}
I hope I explained your concern.
Thanks.
Is there a way to slice 2d array when all column are all zeros? TIA
$cars = array(
array('Cars', 0, 18, 2, 4, 0, 3, 0, 8),
array('BMW', 0, 13, 2, 4, 0, 3, 0, 8),
array('Saab', 0, 29, 2, 4, 0, 3, 0, 8),
array('Land Rover', 0, 15, 2, 4, 0, 3, 0, 8),
);
echo '<table border="1">';
foreach ($cars as $car) {
echo '<tr>';
foreach ($car as $key) {
echo '<td>'.$key.'</td>';
}
echo '</tr>';
}
echo '</table>';
The following code will remove the columns if every value in the column has the value 0. If you had other plans such as slicing you still can use the code to help you identify what you want.
I do this before actually printing out the array as I do not simply know of a function to do this, so you'd have to create one.
The function array_column() is used to retrieve the columns and I compare it against array_intersect() that returns the rows with the same value and compare that against the amount of rows in your table.
$cols = count($cars[0]);
$rows = count($cars);
$filter = [0]; // add more unwanted values if you please
for($col=1; $col<$cols; $col++){ // skip index 0
if(count(array_intersect(array_column($cars, $col), $filter))) == $rows){
// We found a column "$col" where every value is "0".
foreach($cars as $k => $arr){
// Looping over the main array.
unset($arr[$col]); // unset the cloned array.
$cars[$k] = $arr; // update the original array with new value.
}
}
}
Now you can print your table with the same code you had.
I need to generate a row with some numbers like ", 0, 5, 21, 68, 2" (I will use them for some stats). Anyway, I get the numbers from a MySQL database and I process them with a foreach like this:
$stats= '';
foreach($rows as $row) {
$stats.= ', '.$row['total'];
}
The problem if that sometimes I don't have 5 rows, I have only 3 for example. What can I do to auto complete foreach with 0, 'till five numbers are generated, something like ", 0, 5, 21, 0, 0"? I have no ideea how to do that. Thank you!
You can simply loop through and add remaining zeros into array, and use implode with delimiter , to get desired result.
$stats = array();
foreach($rows as $row) {
$stats[] = $row['total'];
}
$count = count($stats);
for($i=$count; $i <= 5; $i++){
$stats[] = 0;
}
echo implode(',', $stats);
The values in this array are inserted by pulling XML values (using the simplexml_load_file method) and a foreach loop.
$skuArray(2, 4, 3, 7, 7, 4, 1, 7, 9);
After populating the array, I then need to check to see if any duplicate values exist in the array (IE, 7 and 4 in this case). Product->sku contains the skuArray value (from an XML file) in the foreach loop below. The code below isn't working. Any advice? Thanks!
foreach($XMLproducts->product as $Product) {
if (in_array($Product->sku, $skuArray, > 1) {
// execute code
}
}
Use array_count_values() to get the number of times a value occurs and then check to see if it is more than one
$skuArray = array(2, 4, 3, 7, 7, 4, 1, 7, 9);
$dupes = array_count_values($skuArray);
foreach($XMLproducts->product as $Product) {
if ($dupes[$Product->sku] > 1) {
// execute code
}
}
If you need to remove the duplicates then you can use array_unique:
<?php
$input = array(4, 4, 3, 4, 3, 3);
$result = array_unique($input);
// $result = array(4, 3)
?>
If you need only check if there are duplicates then you can do it using array_count_values:
<?php
$input = array(2, 4, 3, 7, 7, 4, 1, 7, 9);
$counts = array_count_values($input);
$duplicates = array();
foreach($counts as $v => $count){
if($count > 1)
$duplicates[] = $v;
}
Then you will have an array $duplicates with the duplicated values.
Source: Php check duplicate values in an array
Your code has typo:
if (in_array($Product->sku, $skuArray, > 1) {
in_array expect the first parameter the needle, but you mentioned "Product->sku contains the skuArray value ", anyway, it should be like this:
if (in_array($Product->sku, $skuArray)) {