-2

I have the following code:

<?php
$dates = array('2014-12-01','2014-12-02','2014-12-08','2014-12-09','2014-12-10','2014-12-11');
$values = array(5,3,7,8,9,2);

foreach ($dates as $date){
 //Array of the regarded days names is generated
  $days[] = strtolower(date('l', strtotime($date)))."\n";
}

for ($i = 0; $i < count($days); $i++){
 $day = $days[$i];    
 $$day = $values[$i];
}    
echo $monday;
?>

echo $monday does not print any value, I expect it to print 8, because in the last loop I had a variable named with the value of $day and the last setting for that value should be 8. So why it does not set correctly?!

This is a demo: http://codepad.org/VDIyBuq3

2
  • 4
    Deja vu?? stackoverflow.com/questions/27452960/… Commented Dec 12, 2014 at 23:03
  • I don't ask about the regarded question itself. I just ask about other implementation. Commented Dec 13, 2014 at 0:43

1 Answer 1

4

This is your problem:

$days[] = strtolower(date('l', strtotime($date)))."\n";
                                                 ^^^^^ here

You are adding a new-line character to the end of your value, so your value will not be monday but monday\n.

Just remove that:

$days[] = strtolower(date('l', strtotime($date)));
Sign up to request clarification or add additional context in comments.

2 Comments

@Fred-ii- A bit bored I guess ;-)
Hahaha!! You know me ;) Always the perfectionist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.