Skip to content

Instantly share code, notes, and snippets.

@mamchenkov
Created February 12, 2014 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamchenkov/8952194 to your computer and use it in GitHub Desktop.
Save mamchenkov/8952194 to your computer and use it in GitHub Desktop.
<?php
$data = array(
'Collections' => array(
'id' => 1,
'name' => 'Countries',
),
// sorted by group,name from the SQL
'CollectionItems' => array(
array( 'id' => 1, 'collection_id' => 1, 'name' => 'CountryName', 'value' => 'Cyprus', 'group' => 1,),
array( 'id' => 2, 'collection_id' => 1, 'name' => 'CountryCode', 'value' => 'CY', 'group' => 1,),
array( 'id' => 3, 'collection_id' => 1, 'name' => 'PhoneCode', 'value' => '357', 'group' => 1,),
array( 'id' => 10, 'collection_id' => 1, 'name' => 'Home', 'value' => true, 'group' => 1,),
array( 'id' => 4, 'collection_id' => 1, 'name' => 'CountryName', 'value' => 'Russia', 'group' => 4,),
array( 'id' => 5, 'collection_id' => 1, 'name' => 'CountryCode', 'value' => 'RU', 'group' => 4,),
array( 'id' => 6, 'collection_id' => 1, 'name' => 'PhoneCode', 'value' => '7', 'group' => 4,),
),
);
$wanted = array(
array('id' => 1, 'CountryName' => 'Cyprus', 'CountryCode' => 'CY', 'PhoneCode' => '357'),
array('id' => 4, 'CountryName' => 'Russia', 'CountryCode' => 'RU', 'PhoneCode' => '7'),
);
$actual = array();
$last_group = null;
$current_item = array();
foreach ($data['CollectionItems'] as $item) {
if (!is_null($last_group) && ($last_group <> $item['group'])) {
$actual[] = $current_item;
$current_item = array();
}
$current_item[ $item['name'] ] = $item['value'];
$last_group = $item['group'];
}
$actual[] = $current_item;
print_r($actual);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment