Something like this might work for a start:
<?php
$Data = array (
array (
'country' => array (
'code' => 'in',
'name' => 'India'
),
'language' => array (
'code' => 'en',
'name' => 'English'
)
),
array (
'country' => array (
'code' => 'in',
'name' => 'India'
),
'language' => array (
'code' => 'hi',
'name' => 'Hindu'
)
),
array (
'country' => array (
'code' => 'id',
'name' => 'Indonesia'
),
'language' => array (
'code' => 'id',
'name' => 'Indonesian'
)
),
array (
'country' => array (
'code' => 'it',
'name' => 'Italy',
),
'language' => array (
'code' => 'it',
'name' => 'Italian'
)
),
array (
'country' => array (
'code' => 'jp',
'name' => 'Japan'
),
'language' => array (
'code' => 'ja',
'name' => 'Japanese'
)
),
array (
'country' => array (
'code' => 'mx',
'name' => 'Mexico'
),
'language' => array (
'code' => 'es',
'name' => 'Spanish; Castilian'
)
),
array (
'country' => array (
'code' => 'nl',
'name' => 'Netherlands'
),
'language' => array (
'code' => 'nl',
'name' => 'Dutch'
)
),
array (
'country' => array (
'code' => 'ng',
'name' => 'Nigeria'
),
'language' => array (
'code' => 'en',
'name' => 'English'
)
),
array (
'country' => array (
'code' => 'no',
'name' => 'Norway'
),
'language' => array (
'code' => 'no',
'name' => 'Norwegian'
)
),
array (
'country' => array (
'code' => 'pk',
'name' => 'Pakistan'
),
'language' => array (
'code' => 'en',
'name' => 'English'
)
),
array (
'country' => array (
'code' => 'ph',
'name' => 'Philippines'
),
'language' => array (
'code' => 'en',
'name' => 'English'
)
),
array (
'country' => array (
'code' => 'nl',
'name' => 'Netherlands'
),
'language' => array (
'code' => 'en',
'name' => 'English'
)
)
);
function ProcessData ($Data) {
$Res = array ();
$m = array ();
foreach ($Data as $k => $v) {
$Country = $v ['country'];
if (in_array ($Country ['name'], $m)) {
continue;
}
$Lang = array ();
$Lang [] = $v ['language'];
for ($i = $k + 1, $l = count ($Data); $i < $l; $i++) {
if ($Data [$i] ['country'] ['name'] === $Country ['name']) {
$Lang [] = $Data [$i] ['language'];
}
}
$m [] = $Country ['name'];
$Res [] = array (
'country' => $v ['country'] ['name'],
'language' => $Lang
);
}
return $Res;
}
print_r (ProcessData ($Data));
?>
Giving:
Array
(
[0] => Array
(
[country] => India
[language] => Array
(
[0] => Array
(
[code] => en
[name] => English
)
[1] => Array
(
[code] => hi
[name] => Hindu
)
)
)
[1] => Array
(
[country] => Indonesia
[language] => Array
(
[0] => Array
(
[code] => id
[name] => Indonesian
)
)
)
[2] => Array
(
[country] => Italy
[language] => Array
(
[0] => Array
(
[code] => it
[name] => Italian
)
)
)
[3] => Array
(
[country] => Japan
[language] => Array
(
[0] => Array
(
[code] => ja
[name] => Japanese
)
)
)
[4] => Array
(
[country] => Mexico
[language] => Array
(
[0] => Array
(
[code] => es
[name] => Spanish; Castilian
)
)
)
[5] => Array
(
[country] => Netherlands
[language] => Array
(
[0] => Array
(
[code] => nl
[name] => Dutch
)
[1] => Array
(
[code] => en
[name] => English
)
)
)
[6] => Array
(
[country] => Nigeria
[language] => Array
(
[0] => Array
(
[code] => en
[name] => English
)
)
)
[7] => Array
(
[country] => Norway
[language] => Array
(
[0] => Array
(
[code] => no
[name] => Norwegian
)
)
)
[8] => Array
(
[country] => Pakistan
[language] => Array
(
[0] => Array
(
[code] => en
[name] => English
)
)
)
[9] => Array
(
[country] => Philippines
[language] => Array
(
[0] => Array
(
[code] => en
[name] => English
)
)
)
)
1
solved Recreate an array so that I get the result as I want