I have the following array:
$moods = array("neutral", "happy", "sad");
$newarray= "";
I want to use an if-loop that goes through the array $mood in sequence and gives a different output based on what $moods value is selected
for ($x = 1; $x <= 7; $x++) {
[insert code for sequencing through $moods here]
if ($moods == "neutral") {
$output1 = "1";
$newarray.= $output1
}
else {
$output2 = "0";
$newarray.= $output2
}
with the desired output being that $newarray is filled with $output1 and $output2 values such that
$newarray= "1001001";
I have tried using array_rand:
for ($x = 1; $x <= 7; $x++) {
$mood= array_rand($moods,1);
if ($mood == "neutral") {
$output1 = "1";
$newarray .= $output1
}
else {
$output2 = "0";
$newarray.= $output2
}
but the problem with this is that it selects a random variable from the $moods array, instead of going through it in sequence.
2
Answers
Try this.
Update based on updated question
You could refer to an element of the array using the subscript (
[]
) operator: