On a Drupal 7 site, I'm trying to build an array (using PHP 5.5) with select fields pulled from another, very large array ("raw_wrap"). The fields from which I'm pulling data are named "field_hours_1," "field_hours_2," "field_hours_3," etc. so I thought I could increment those names with each pass (rather than placing them all separately). Unfortunately, I'm getting the same value in all fields when the array is printed out.
Here's the code as written:
$nid = arg(1);
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
$raw_wrap = $wrapper->raw();
$days_array = array();
for( $x=1; $x<5; $x++ ) {
$ho = "raw_wrap->field_hours_" . $x . "['und'][0]['value']";
$days_array[$x]['hours_open'] = date('H:i', strtotime($ho));
$hc = "raw_wrap->field_hours_" . $x . "['und'][0]['value2']";
$days_array[$x]['hours_close'] = date('H:i', strtotime($hc));
}
Printing out the new array then gives me:
Array
(
[1] => Array
(
[hours_open] => 18:00
[hours_close] => 18:00
)
[2] => Array
(
[hours_open] => 18:00
[hours_close] => 18:00
)
[3] => Array
(
[hours_open] => 18:00
[hours_close] => 18:00
)
[4] => Array
(
[hours_open] => 18:00
[hours_close] => 18:00
)
)
Note also that the number ("18:00" in this case) doesn't change. How the heck do I get that second number to update - and for both numbers (opening and closing times) to be different for each of the four buckets?
Here's how the fields layout in the larger array ("$raw_wrap"):
[field_hours_1] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 0000-01-01 12:50:36
[value2] => 0000-01-01 23:50:36
[timezone] => America/Chicago
[timezone_db] => UTC
[date_type] => datetime
)
)
[field_hours_2] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 0000-01-01 12:05:36
[value2] => 0000-01-01 22:05:36
[timezone] => America/Chicago
[timezone_db] => UTC
[date_type] => datetime
)
[field_hours_3] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 0000-01-01 13:35:36
[value2] => 0000-01-01 22:35:36
[timezone] => America/Chicago
[timezone_db] => UTC
[date_type] => datetime
)
)
)
And so on....
Edit: What this seems to come down to is this: I can add to my new array with the following, but I'd like to do this with a loop.
$days_array[]['hour_open'] = $raw_wrap->field_hours_1['und'][0]['value'];
$days_array[]['hour_open'] = $raw_wrap->field_hours_2['und'][0]['value'];
$days_array[]['hour_open'] = $raw_wrap->field_hours_3['und'][0]['value'];
I just can't figure out a way to increment that "field_hours_" field. I'm having no luck replacing the 1, 2 & 3 for instance with "$x."
I've tried building a string beforehand, for instance:
$hc = "raw_wrap->field_hours_" . $x . "['und'][0]['value2']";
$days_array[]['hours_close'] = date('H:i', strtotime($hc));
But that doesn't work. (It gives a syntax error.)
And I've tried building a compound variable:
${"raw_wrap->field_hours_" . $x . "['und'][0]['value2']"}
And I've tried simply placing the variable into the string, like so:
$raw_wrap->field_hours_$x['und'][0]['value2']
And that gets yet another error. I'm beginning to believe that you can't add these fields by changing their names using a variable in a loop. Maybe it simply can't be done?
It's the syntax. Use this in your loop:
$ho = $raw_wrap->{"field_hours_$x"}['und'][0]['value'];
$days_array[$x]['hours_open'] = date('H:i', strtotime($ho));
$hc = $raw_wrap->{"field_hours_$x"}['und'][0]['value2'];
$days_array[$x]['hours_close'] = date('H:i', strtotime($hc));
Here's a full test code on phpFiddle: http://phpfiddle.org/main/code/gwme-zg8p