Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I group same array value

Example my array

$options = array(
    array("brand" => "Puma","code" => "p01","name" => "Puma One"),
    array("brand" => "Puma","code" => "p02","name" => "Puma Two"),
    array("brand" => "Puma","code" => "p03","name" => "Puma Three"),
    array("brand" => "Nike","code" => "n01","name" => "Nike One"),
    array("brand" => "Nike","code" => "n02","name" => "Nike Two"),
    array("brand" => "Nike","code" => "n03","name" => "Nike Three"),
    array("brand" => "Nike","code" => "n04","name" => "Nike Four"),
    array("brand" => "Adidas","code" => "a01","name" => "Adidas One"),
    array("brand" => "Adidas","code" => "a02","name" => "Adidas Two"),
    array("brand" => "Adidas","code" => "a03","name" => "Adidas Three"),
    array("brand" => "Adidas","code" => "a04","name" => "Adidas Four"),
    array("brand" => "Adidas","code" => "a05","name" => "Adidas Five"),
    array("brand" => "Adidas","code" => "a06","name" => "Adidas Six")
);

How to generate this array to be

<select name="products" id="products">
  <optgroup label="Puma">
  <option value="p01">Puma One</option>
  <option value="p02">Puma Two</option>
  <option value="p03">Puma Three</option>
  </optgroup>
  .......
  <optgroup label="Adidas">
  <option value="a01">Adidas One</option>
  <option value="a02">Adidas Two</option>
  <option value="a03">Adidas Three</option>
  .......
  </optgroup>
</select>

Or you can suggestion better array according to my select option output. Let me know.

like image 255
Unknown Error Avatar asked Feb 23 '11 02:02

Unknown Error


People also ask

How do you group the same numbers in an array?

Simple Solution is to use nested loops. The outer loop traverses array elements one by one. The inner loop checks if this is first occurrence, if yes, then the inner loop prints it and all other occurrences.

How do you group values in an array?

The group() method groups the elements of the calling array according to the string values returned by a provided testing function. The returned object has separate properties for each group, containing arrays with the elements in the group. This method should be used when group names can be represented by strings.

How do you create an array with the same value?

To create an array with N elements containing the same value: Use the Array() constructor to create an array of N elements. Use the fill() method to fill the array with a specific value. The fill method changes all elements in the array to the supplied value.

How do you check if an array has the same element?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.


1 Answers

You can create another array keyed by the brand:

$newOptions = array();
foreach ($options as $option) {
  $brand = $option['brand'];
  $code = $option['code'];
  $name = $option['name'];

  $newOptions[$brand][$code] = $name;
}

This will produce an array like this:

$newOptions = array(
  'Puma' => array('p01' => 'Puma One', 'p02' => 'Puma Two'),
  'Nike' => array('n01' => 'Nike One', 'n02' => 'Nike Two'),
  ...
);

If you can directly format your array like this, you can skip the first step.

Then iterate over this new array and output the options:

foreach ($newOptions as $brand => $list) {
  echo "<optgroup label=\"$brand\">\n";
  foreach ($list as $code => $name)
    echo "<option value=\"$code\">$name</option>\n";
  echo "</optgroup>\n";
}
like image 150
casablanca Avatar answered Oct 12 '22 22:10

casablanca