Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

cannot get values from a php array

This is my php code

 echo '<pre>';
 print_r($weekdays); echo '<br/>';

 foreach ($weekdays as $key => $day) {

     print_r($day); echo '<br/>';
     echo 'key - '. $key; echo '<br/>';
     echo 'val - '. $day['val']; die;      
 }

This is result of this

Array
(
[sunday] => Array
    (
        ['val'] => 1
        ['from'] => 6:00:00
        ['to'] => 6:00:00
    )

[monday] => Array
    (
        ['val'] => 1
        ['from'] => 6:00:00
        ['to'] => 6:00:00
    )

[tuesday] => Array
    (
        ['from'] => 7:00:00
        ['to'] => 0:00:00
    )

[wednesday] => Array
    (
        ['from'] => 0:00:00
        ['to'] => 0:00:00
    )

[thuesday] => Array
    (
        ['from'] => 0:00:00
        ['to'] => 0:00:00
    )

[friday] => Array
    (
        ['from'] => 0:00:00
        ['to'] => 0:00:00
    )

[saturday] => Array
    (
        ['from'] => 0:00:00
        ['to'] => 0:00:00
    )
)

Array
(
['val'] => 1
['from'] => 6:00:00
['to'] => 6:00:00
)

key - sunday
val - 

the problem is in my foreach i try to get $day['val'] but nothing shown . tried by using $day->val also . when i print_r($day) i get

Array
(
['val'] => 1
['from'] => 6:00:00
['to'] => 6:00:00
)

Please help me.

Upvotes: 1

Views: 2166

Answers (3)

RandomWhiteTrash
RandomWhiteTrash

Reputation: 4014

I suppose It would be best to make sure you do not name array keys with apostrophes. This might confuse you in the future or whoever works on this code.

Your print_r should look like this:

Array ( 
    [sunday] => Array
       (
          [val] => 1
          [from] => 6:00:00
          [to] => 6:00:00
       )

Correct the code that creates that array and you will be set.

Upvotes: 1

Tgr
Tgr

Reputation: 28160

It appears that your key is actually 'val', not val.

Upvotes: 1

trutheality
trutheality

Reputation: 23455

The problem is that the key isn't val it's 'val' (with the quotes).

echo 'val - '. $day["'val'"];

Will work.

Upvotes: 4

Related Questions