Solved

Adjusting an array...

Posted on 2008-10-18
6
193 Views
Last Modified: 2013-12-13
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
Comment
Question by:n00b0101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 1

Accepted Solution

by:
ctyler86 earned 500 total points
ID: 22748430
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
 

Author Comment

by:n00b0101
ID: 22748473
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
 

Author Comment

by:n00b0101
ID: 22748524
$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
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:n00b0101
ID: 22748530
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
 

Author Comment

by:n00b0101
ID: 22748546
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
 

Author Closing Comment

by:n00b0101
ID: 31507417
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php error 27 60
MySQL Persistent Connections 10 35
Difference between PHPClasse.php and SimplXLS.php 5 25
How do I Enable submit button only if listbox has items 4 35
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

726 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