Displaying array contents / multi lists

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 28, 2022   (RSS)

By rez - March 26, 2022 - edited: March 26, 2022

						
<?php $tagsByNum = array_groupBy($tagsRecords, 'num'); // group all tags by num to check for project matches later
  //showme( $tagsByNum ); ?>
<?php foreach ($imageRecords as $record): // already filtered in header to contain only this project's record ?>
  <?php	$assignedTags = listValues_unpack($record['tags']); 	// create arrays of tags that were assigned with checkboxes for this record. Each array contains all tag record info
    //showme($assignedTags);?>

    <?php foreach ($assignedTags as $key => $value): //loop through each tag array for current image?>
  	<?php if (array_key_exists($value, $tagsByNum)) : ?>
  	    <?php echo $tagsByNum [$value] ['title']; ?>
 	 <?php endif ?>
     <?php endforeach ?>
    <?php endforeach ?>

Through many var_dump and showme's, I have it working. I actually have the filters working now as well. I still get confused looking back.

1. I don't understand the $key => $value though. What does this say in English? When I look at what is inside these variables, they are the same. Can you explain using this line and why? This foreach is creating ANOTHER array?

2. The if statement is checking if the $value exists in tagsByNum? Why? All of the tags are in tagsByNum, so we know it's in there. Therefore, I eliminated this if statement and it still works fine. I must have misunderstood the post where I got this? When would I use array_search or array_key_exists? 

3. I see in other posts where you are wrapping this foreach in isset(). I don't seem to be getting offset errors. When would I use isset()? I am thinking I can skip it because i require the tags by admins. Therefore, there must not be any empty variables in the array. Is that why I don't need it here or when do you use it?

By daniel - March 28, 2022

Hi rez,

Congrats on getting it working yourself! I can answer your specific questions:

1. I don't understand the $key => $value though. What does this say in English? When I look at what is inside these variables, they are the same. Can you explain using this line and why? This foreach is creating ANOTHER array?

foreach($array as $key => $value) is a way to, for lack of a better way to put it, access both the key and value from an array. The other common syntax - e.g. foreach($records as $record) - is essentially the same but only accessing the value portion. You'll see them variably throughout the code depending on which data is needed. Here's some documentation on arrays and foreach loops that will be a lot more thorough than I can be:

In your specific case, the values are the same because this is how the list was set up; for a CMSB listValues array the $key refers to the "saved" value, and $value refers to the "display" label. They may be the same, but they may also be different, depending on the list setup.

2. The if statement is checking if the $value exists in tagsByNum? Why? All of the tags are in tagsByNum, so we know it's in there. Therefore, I eliminated this if statement and it still works fine. I must have misunderstood the post where I got this? When would I use array_search or array_key_exists? 

If the list field's options are changed after records have been created, it can be possible for a record to have a value assigned that no longer "exists" in the $tagsByNum array - which is what this is checking for.

3. I see in other posts where you are wrapping this foreach in isset(). I don't seem to be getting offset errors. When would I use isset()? I am thinking I can skip it because i require the tags by admins. Therefore, there must not be any empty variables in the array. Is that why I don't need it here or when do you use it?

It's entirely dependent on your code, but typically these types of checks are added to safeguard against edge cases and future code changes. Related to the previous answer, it's good practice to assume that any data or structures not explicitly defined in the current file could change at any point. If the check isn't causing any issues, I'd recommend leaving it in place.

Hope that helps!

Daniel
Technical Lead
interactivetools.com