How to show all values of an array in Drupal 7 - php

I'm working on a Drupal 7 site with an event listing. I've added a date field so the user can specify the date or dates for the event. Now I'm trying to get the date(s) to show in the template. I've tried using this:
<?php print $node->field_event_date['und'][0]['value']; ?>
That works ok, but it only shows one event from the array. I could just repeat that line 10 times and replace the array item number in each one, but I figure there has to be a way to show all the items in an array, whether it's one or ten. Can this be done with PHP or do I need to make a View?

In PHP terms you're probably looking for a foreach loop, to iterate over the array and print the values.
In Drupal terms, you'll want to use that with the field_get_items() function:
$items = field_get_items('node', $node, 'field_event_date');
foreach ($items as $item) {
print $item['value'];
}
For bonus fun, check out EntityMetadataWrappers

Related

How to write a code for each array within an array

I am new to this so please do not judge.
I have transformed a .csv file into an array([0]=>array([0]=>string(), [1]=>string())[1]=>array([0]=>string(), [1]=>string()) etc. So I can access it numerically i.e. $the_big_array[1][1]
Now I want the program to write a couple of lines of code for each array within an array.
Basically what I am doing is creating a table like this that will be encoded in json.
$request['AddPrice'][0][variable1] = $the_big_array[1][2]
$request['AddPrice'][0][variable2] = $the_big_array[1][3]
and I want different values loaded for and from each line of .csv file/$the_big_array
$request['AddPrice'][1][variable1] = $the_big_array[1][2]
$request['AddPrice'][1][variable2] = $the_big_array[1][3]
I am stuck at foreach function as I cannot grasp how to make it execute certain action for each array within an array.
You have to use a foreach loop for every array dimension.
If You have 2 dimensions, like in $the_big_array[1][1], go through the first dimension with your first loop. Inside this loop, do another foreach to go through your second dimension.
so I have fixed this problem. Perhaps the question was not clear enough.
What I did was to create arrays with maximum occupancy for each of my rooms and add nested foreachloop for each occupancy, so it has to repeat until maximum occupancy is reached and only then it can iterate to the next key in the first foreachloop.

Looping through a recordset twice

I'm developping a Moodle 2.9.1 pluggin and I need to loop through a small recordset twice on the same page.
I'm using
$rs = $DB->get_recordset_sql($sql, array());
to get the data from mysql.
I would need a function like mysql data_seek(0) to work on the recordset again but I cannot find anything related to this in the moodle API or forums.
If you know the data is not going to be excessively huge, then you can use get_records_sql() instead. This will return an array, indexed by the first field in the SELECT. You can then do what you want with this array (loop through it multiple times, split, pop, shift, etc).
Just loop twice through $rs ??
foreach ($rs as $data){
}
foreach ($rs as $data){
}
Or to do it clean make a function loop_thorught and call it twice ?

Comparing like elements in arrays and echoing out rest of the arrays

I have created multiple arrays from text documents that are uploaded monthly. One of the Arrays item numbers, description and picture. the other array is item numbers, price and quantity.
What I am trying to do is if the item number is the same then be able to Echo out all the information that I need. Example would be
if($array1[0] ==$ array2[4]){
echo $array1{4];
echo $array2[6];
}
this doesn't work and having issues with getting array_intersect to work with it also.
I don't really understand how there are so many variable inside an array. Item's number, description, and picture inside an array? I assuming you are looking for matching item's number inside both arrays so..
Good luck trying:
foreach($array1 as $a){
foreach($array2 as $b){
if($a==$b){
echo $a;
}
}
}
Let me know how it works for you soon.
this didnt work for what I was trying to do but I did figure it out. I needed to use strpos() in a if statement to get the items I was looking for.

Nested foreach inside a while loop, array is not an array after first while

I have a while loop with a nested foreach loop, after the first while loop is run the array inside the foreach loop is being unset (or at least ceases to be an array).
Where I'm headed is an array of data for a bar chart which shows every day since a start date and the number of clicks by type (8 types) on each day. So I'm calculating the number of days from the start date to now, and for each day it checks the first array to see how many, if any, and what types of clicks there were on that day.
Here's the relevant bit of code...
$i=0;
while($i<=$numdays)
{
echo $date2->format('Y-m-d') . "<br>";
foreach($clicklist as $key => $clicklist) {
if ( $clicklist[clickdate] === $date2 ) {
echo $clicklist[clicks]." clicks on ".$clicklist[type]." on that date<br>";
}
}
$date2->modify('+1 day');
$i++;
echo is_array($clicklist) ? 'Array<br>' : 'not an Array<br>';
}
$numdays is the number of days from the startdate to now (needed for one of the chart variables, $date2 is the startdate and $clicklist is the array of clicks/dates/types from the db. All the random echos are just so I can see what's going on - or not as the case may be.
The while loop works fine is isolation, the foreach loop also works fine outside the while loop (using a static date in place of the variable), but of course that's just a one time run.
From the manual, foreach resets the pointer back to the start automatically, so that's not an issue.
I'm missing something obvious I'm sure.. any guidance much appreciated.
foreach($clicklist as $key => $clicklist)
Is where your problem is. Do not reuse the name, change it for something such as
foreach($clicklist as $key => $cl)
otherwise by the end of your loop, $clicklist will be overwritten as the last element that was iterated.
Edit: on a related note, avoid accessing your array without quotes, such as in $clicklist[clickdate]. This could later on turn into a bug if you ever encounter a constant that has been defined with that same name. Use $clicklist['clickdate'] instead.

PHP scope question

I'm trying to look through an array of records (staff members), in this loop, I call a function which returns another array of records (appointments for each staff member).
foreach($staffmembers as $staffmember)
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
This is working OK, however, later on in the script, I need to loop through the records again, this time making use of the appointment arrays, however they are unavailable.
foreach ($staffmembers as $staffmember)
{
//do some other stuff
//print_r($staffmember['appointments'] no longer does anything
}
Normally, I would perform the function from the first loop, within the second, however this loop is already nested within two others, which would cause the same sql query to be run 168 times.
Can anyone suggest a workaround?
Any advice would be greatly appreciated.
Thanks
foreach iterates over a copy of the array. If you want to change the value, you need to reference it:
foreach($staffmembers as &$staffmember) // <-- note the &
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
From the documentation:
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
and
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

Categories