Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Adjusting an array...

Below is an array.  I'm trying to build a select a menu based on it.  

I want the select menu to look like this:

<select name = "size">
  <option value="X879">small / medium </option>
  <option value="7ABC">small / large </option>
</select>

I can't figure out how to "group" them that way... :(
Array
(
    [0] => Array
        (
            [item_id] => 32
            [oid] => X879
            [name] => size
 	    [value] => small
            [id] => 2
            [aid] => 10
        )
 
    [1] => Array
        (
            [item_id] => 32
            [oid] => X879
            [name] => size
	    [value] => medium
            [id] => 2
            [aid] => 14
        )
 
    [2] => Array
        (
            [item_id] => 32
            [oid] => 7ABC
            [name] => size
	    [value] => small
            [id] => 2
            [aid] => 10
        )
 
    [3] => Array
        (
            [item_id] => 32
            [oid] => 7ABC
            [name] => size
	    [value] => large
            [id] => 2
            [aid] => 130
        )
 
)

Open in new window

0
n00b0101
Asked:
n00b0101
  • 5
1 Solution
 
ctyler86Commented:
Try a for loop on it, and use the key of the array. Example:

$count_of_array = count($array);
echo '<select name="size">';
for($i=0; $i<$count_of_array; $i++){
  echo '<option value="'.$array[1]."'>'.$array[3].'</option>';
}
echo '</select>';

0
 
n00b0101Author Commented:
Actually, the item_id and name will change, depending on what's retrieved from the database.  I tried:



$array = array();
foreach($options as $key => $value) {
  $name = $value['name'];
  unset($value['name']);
  if(isset($array[$name])) $array[$name][] = $value;
  else $array[$name] = array($value);
}


Which kinda redoes the array so that it's "grouped" by size, but I don't know how to then group it by oid.  I thought if I could get that far, that I could build the select accordingly.
0
 
n00b0101Author Commented:
$array = array();
foreach($options as $key => $value) {
  $name = $value['name'];
  $optname = $value['oid'];
  unset($value['name']);
  unset($value['oid']);
 
   
    if(isset($array[$name][$optname])) {
        $array[$name][$optname] = $value;
    } else {
        $array[$name][$optname] = array($value);
    }
}
0
Independent Software Vendors: 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!

 
n00b0101Author Commented:
Oops... The code in my last post almost gets me the array I need, except that it only prints out one group per oid, when it should be printing out two.
0
 
n00b0101Author Commented:
Ok, so I have the array grouped the way I want, but can't figure out how to build the select menu based on it:

foreach($options as $key => $value) {
	$name = $value['name'];
	$optname = $value['oid'];
	unset($value['name']);
	unset($value['oid']);
	 
		if(isset($array[$name][$optname])) {
			$array[$name][$optname][] = $value;
		} else {
			$array[$name][$optname] = array($value);
		}
}

Open in new window

0
 
n00b0101Author Commented:
Thanks for this.  I've made progress towards what I wanted, and now have a related, but different question to ask.  So, I'm going to open up another thread. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now