?
Solved

PHP using array variable as index to an array?

Posted on 2012-08-20
6
Medium Priority
?
353 Views
Last Modified: 2012-08-21
Upon a form post I do this:

$trimmed = array_map('trim', $_POST);

Open in new window


I do this to write back to the form upon a form error:

<input type="text" name="title" id="title" size="40" required value="<?php if (isset($trimmed['title']) echo $trimmed['title']; ?>">

Open in new window


BUT, I have an array of checkboxes that I originally create using php and  I can't quite figure out how to write back to them the same way I did with the title.  My checkboxes are  initially written to the page this way:

foreach($genre_list as $genre) { 									
echo '<label for="' . $genre['id'] . '" ><input type="checkbox" name="genre[]" id="' . $genre['id'] . '" value="' . $genre['id'] . '" '  >' . $genre['genre'] . '</label><br />';
 }

Open in new window


Can I do something like:
if ($trimmed['genre[$genre['id']']] == $genre['id']) echo checked="checked"

The part I'm questioning is those nested arrays: $trimmed['genre[$genre['id']']]

Thanks
0
Comment
Question by:christamcc
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38314929
this should do:
 $trimmed['genre[' .$genre['id']. ']']

Open in new window

0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38315817
I usually access nested arrays like this:

$valueOf1stCheckedCheckbox = $_POST["checkboxes"][0];
$valueOf2ndCheckedCheckbox = $_POST["checkboxes"][1];


If you try to run a trim() on a $_POST containing an array value like that, then you're going to get an error and lose your checkbox values. You need to remove the nested array first:

$genrePOST = $_POST["genre"];
unset($_POST["genre"]);

// Then trim the array
$trimmed = array_map('trim', $_POST);

// Then trim $genre if needed
$trimmedGenres = array_map('trim', $genrePOST);

// And add it back into the $_POST
$_POST["genres"] = $trimmedGenres;

If you want a more generic function that will recursively trim all values in an array, regardless of how many nested arrays you have, you can do this:

function trimArray($arr)
{
   foreach($arr as $var => $val)
   {
     if(is_array($val))
     {
       $arr[$var] = trimArray($val);
     }
     else
     {
       $arr[$var] = trim($val);
     }
   }
   return $arr;
}

$trimmed = trimArray($_POST);
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38315831
A condensed version of the same function:

function trimArray($a) { foreach($a as $x => $y) if(is_array($y)) $a[$x] = trimArray($y); else $a[$x] = trim($y); return $a; }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:christamcc
ID: 38317239
Thanks for all that info!  No more errors, but it's not not returning the previously check value.  I'm using gr8s function above and the checkbox like this:

foreach($genre_list as $genre) { 									
			echo '<label for="' . $genre['id'] . '" ><input type="checkbox" name="genre[]" id="' . $genre['id'] . '" value="' . $genre['id'] . '" ';
			echo (isset($trimmed['genre[' .$genre['id']. ']']) ? ' checked="checked"' : '');
		        echo ' >' . $genre['genre'] . '</label><br />';
			//added for testing: echo 't:' . (isset($trimmed['genre[' .$genre['id']. ']']));
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38317286
Change:
echo (isset($trimmed['genre[' .$genre['id']. ']']) ? ' checked="checked"' : '');

To:
echo (isset($trimmed['genre'][$genre['id']]) ? ' checked="checked"' : '');
0
 

Author Closing Comment

by:christamcc
ID: 38318555
Thanks for also letting me know about the array issue!  I would have been pulling my hair out over that one.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question