I have this array as follow:
Array (
[pageid_01_page_name] => first Page
[pageid_01_Style] => full
[thePageID_1] => pageid_01
[theElementID_1] => 1
[source_type_1] => slideshow_box
[pageid_01_1_slideshow_box] => Array ( [layout] => one
[content_id] => Array ( [0] => 856 )
[slideshow_timeout] => 8
[slideshow_height] => 400
[image_resize] => on
[image_crop] => on
[list_orderby] => date
[list_order] => DESC
[slideshow_buttons] => on )
[thePageID_2] => pageid_01
[theElementID_2] => 2
[source_type_2] => banner_box
[pageid_01_2_banner_box] => Array ( [layout] => one
[text] => test
[button_text] => Button Text
[button_link] => a link )
)
I need to build another array out of the one above, with this structure:
Array (
[pages] => Array ( [pageid_01] => stdClass Object
( [pageName] => first Page
[style] => full
[pageID] => pageid_001
[contents] => Array (
[2] => stdClass Object (
[element_id] => 1
[content_type] => slideshow_box
[layout] => one
[content_id] => Array ( [0] => 856 )
[slideshow_timeout] => 8
[slideshow_height] => 400
[image_resize] => on
[image_crop] => on
[list_orderby] => date
[list_order] => DESC
[slideshow_buttons] => on
)
[3] => stdClass Object (
[element_id] => 2
[content_type] => banner_box
[layout] => one
[text] => test
[button_text] =>Button text
[button_link] => a link
)
)
Updated with more details:
The way i'm building the second array right now is like this:
$pages = new \stdClass();
$i=0;
$page=0;
$thepagename = '_page_name';
if(isset($thepagename))
{
if(in_array($thepagename, $AllPages))
{ foreach($AllPages as $k => $v) {
$page = str_replace('_page_name','',$k);
#$pages->pages[$page]->pageID = $page;
#$pages->pages[$page]->pageName = $v;
if(stristr($k, 'theElementID_') == true) {
$element_id = $v;
}
if(stristr($k, '_slideshow_box') == true) {
$i++;
#$pages->pages[$page]->contents[$i]->element_id = $element_id;
$pages->pages[$page]->contents[$i]->content_type = 'slideshow_box';
$pages->pages[$page]->contents[$i]->layout = $v["layout"];
$pages->pages[$page]->contents[$i]->content_id = #$v["content_id"];
$pages->pages[$page]->contents[$i]->slideshow_timeout = $v["slideshow_timeout"];
$pages->pages[$page]->contents[$i]->slideshow_height = $v["slideshow_height"];
$pages->pages[$page]->contents[$i]->image_resize = $v["image_resize"];
$pages->pages[$page]->contents[$i]->image_crop = $v["image_crop"];
$pages->pages[$page]->contents[$i]->list_orderby = $v["slideshow_orderby"];
$pages->pages[$page]->contents[$i]->list_order = $v["slideshow_order"];
$pages->pages[$page]->contents[$i]->slideshow_buttons = $v["slideshow_buttons"];
}
}
}
}
and so on....
but this way takes a lot of coding while the contents can be much much more...
First of all,you can loop your array, get all values and keys, The second,built another array, push all key and values into the new array. just like
$new_array = ($new_array,values1,values2,.......);
hope it can help you
Related
code
$result = [];
foreach ($getAllAgent as $rkey => $rvalue) {
$found = -1;
for ($i = 0; $i < count($result); $i++) {
if ($result[$i]["brokerid"] == $rvalue["brokerid"]) {
$found = $i;
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details']; //here to combine
break;
}
}
// if not found, create new
if ($found == -1) {
$result[] = $rvalue;
}
results
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
[0] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
)
)
I have one set of an array, and I loop it and restructure match the data based on ID. After that I will try to merge the same data into one array, I able add into one array. But it will not combine as one. The correct result should be as below.
[get_agent_details] => Array
(
[0] => Array(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
),
[1] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
Your problem is in this line:
$result[] = $rvalue;
Consider the case where you only have one item; this will result in:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
But to be consistent, you need get_agent_details to be a list of items, that happens to have one entry:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[0] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
)
So you need to re-arrange your data, for instance by writing:
$rvalue['get_agent_details'] = [0 => $rvalue['get_agent_details']];
$result[] = $rvalue;
Then, since get_agent_details will already be a list when you encounter a second matching item, your existing code in the inner loop will do the right thing:
// Add an item to the list
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details'];
This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)
hi I have array like this
Array
(
[0] => stdClass Object
(
[name] => text_input
[value] => kalpit
)
[1] => stdClass Object
(
[name] => wpc_chkbox[]
[value] => Option two
)
[2] => stdClass Object
(
[name] => wpc_chkboxasdf[]
[value] => Option one
)
[3] => stdClass Object
(
[name] => wpc_chkboxasdf[]
[value] => Option two
)
[4] => stdClass Object
(
[name] => wpc_inline_chkbox[]
[value] => 1
)
[5] => stdClass Object
(
[name] => wpc_inline_chkbox[]
[value] => 2
)
[6] => stdClass Object
(
[name] => wpc_inline_chkbox[]
[value] => 3
)
[7] => stdClass Object
(
[name] => wpc_radios
[value] => Option one
)
)
now my question is how to combine same name value in onc place, here in above array I have wpc_inline_checkbox[] is repeating 3 times so I want to make it .. I can use array_uniqe() but I want value of other duplicate index...
[4] => stdClass Object
(
[name] => wpc_inline_chkbox[]
[value] => 1,2,3
)
can anyone help me to solve this
Thanks in advance
This is assuming both name/value are strings
<?php
$objects; // This is your array
$sorted = array(); // This is the sorted array
// Loop over your array
foreach($objects as $object)
{
// Check if object exists in sorted array
if( array_key_exists($object->name, $sorted) )
{
// Object with this name is already in sorted array simply add to it
$obj = $sorted[$object->name];
// Update the values
$obj->value = $obj->value . ',' . $object->value;
} else {
// Object is not in the sorted array add it now
$sorted[$object->name] = $object;
}
}
?>
Sorry, your case is not ordinary, so you will need to do it by hand
$properArray = array();
foreach ($originArray as $value) {
if ( ! isset($properArray[$value['name']])) {
$properArray[$value['name']] = array(
'name' = $value['name'],
'value' = ''
);
}
if (isset($properArray[$value['name']]['value'])) {
$properArray[$value['name']]['value'] = $properArray[$value['name']]['value'] . ',' .$value['value']; //better to use array in this place
} else {
$properArray[$value['name']]['value'] = $value['value']
}
}
$originArray = array_values($properArray);
regards,
You have an array of object. You will have to loop through and assign them. Something like
$newArray = [];
foreach ($array as $obj) {
if (!isset($newArray[$obj->name])) {
$newArray[$obj->name] = $obj;
} else {
$newArray[$obj->name]->value .= ",{$obj->value}";
}
}
The new array should look like
["wpc_inline_chkbox[]"] => stdClass Object
(
[name] => wpc_inline_chkbox[]
[value] => "1,2,3"
)
But you should change [value] from string to array.
## Use this ##
$arr = array(
array(
'name' => 'hi'
,'value' => 1
)
,array(
'name' => 'hi'
,'value' => 2
)
, array(
'name' => 'hi2'
,'value' => 1
)
, array(
'name' => 'hi4'
,'value' => 1
)
,array(
'name' => 'hi0'
,'value' => 4
)
, array(
'name' => 'hi0'
,'value' => 3
)
, array(
'name' => 'hi1'
,'value' => 10
)
);
print_r($arr);
$arrTracker = array();
for ($i=0; $i <sizeof($arr) ;$i++) {
for($j = $i+1; $j<sizeof($arr);$j++){
if($arr[$i]['name'] == $arr[$j]['name']){
$arr[$i]['value'] .= ','.$arr[$j]['value'];
$arrTracker[$j] = $j;
}
}
}
// if you want to unset duplicate name and corresponding value, use below forloop, otherwise it's unnecessary
foreach($arrTracker as $tracker){
unset($arr[$tracker]);
}
$arr = array_values($arr);
print_r($arr);
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);
I'm trying to create a "plugin" like script for adding to a "menu array".... I'll go straight in..
Lets say I initiate a nav item like so:
$sections->add_section('dashboard');
$DashBoardSection = $sections->load_section('dashboard');
$DashBoardSection->set_role(NULL);
$DashBoardSection->set_nav_item(array(
'identifier' => 'dashboard',
'text' => 'Dashboard',
'url' => NULL,
'position' => NULL
));
starts off by creating a new section and the getting the instance of.
We are then setting a "role" in which we will test against to see if the user is authenticated as being verified to view.
The set nav item simply stores the array. identifier is a reference to the item ( its for when we want to add sub items), all standard apart from "position" which states where it is to sit in the nav i.e. NULL is top level, array('topnav','subnav') will be topnav->subnav->dashboard.
as a test, this could be stored as follows:
Array
(
[0] => Array
(
[identifier] => dashboard
[text] => Dashboard
[url] =>
[position] =>
)
[1] => Array
(
[identifier] => dashboard2
[text] => Dashboard2
[url] =>
[position] => Array
(
[0] => dashboard
)
)
)
my question being how I turn that into the following structure:
Array
(
[0] => Array
(
[identifier] => dashboard
[text] => Dashboard
[url] =>
[position] =>
[children] => Array
(
[0] => Array
(
[identifier] => dashboard2
[text] => Dashboard2
[url] =>
)
)
)
)
pulling my hair out over this one, any help would be very much appreciated.
regards
I currently have
public function build_navigation( $role ){
$role = (int)$role;
$nav = array();
foreach( $this->sections as $section ) {
if( $section->get_role() === NULL || $section->get_role() === $role ) {
$nav_array = $section->get_nav();
foreach( $nav_array as $key => $nav_item ) {
if( $nav_item['position'] === NULL ) {
$nav[$nav_item['identifier']] = $nav_item;
}elseif( is_array( $nav_item['position'] ) ){
#...#
}
}
}
}
return $nav;
}
EDIT
Imagine this is the array given (it can be in any order)
Array
(
[0] => Array
(
[identifier] => dashboard_child2
[text] => Dashboard Child 2
[url] =>
[position] => Array
(
[0] => dashboard
)
)
[1] => Array
(
[identifier] => dashboard_child_child_1
[text] => Dashboard Child Child 1
[url] =>
[position] => Array
(
[0] => dashboard
[1] => dashboard_child1
)
)
[2] => Array
(
[identifier] => dashboard_child1
[text] => Dashboard Child 1
[url] =>
[position] => Array
(
[0] => dashboard
)
)
[3] => Array
(
[identifier] => dashboard
[text] => Dashboard
[url] =>
[position] =>
)
[4] => Array
(
[identifier] => dashboard2
[text] => Dashboard2
[url] =>
[position] => Array
(
[0] => dashboard
)
)
)
Which needs to be formatted as:
Array
(
[dashboard] => Array
(
[text] => Dashboard
[url] =>
[children] => Array
(
[dashboard_child2] => Array
(
[text] => Dashboard Child 2
[url] =>
)
[dashboard_child1] => Array
(
[text] => Dashboard Child 1
[url] =>
[children] => Array
(
[dashboard_child_child_1] => Array
(
[text] => Dashboard Child Child 1
[url] =>
)
)
)
[dashboard2] => Array
(
[text] => Dashboard2
[url] =>
)
)
)
)
Here's my take on the problem, solved with recursion.
You can use multiple positions (i guess this is why it's an array), it will ignore missing positions if at least on of the positions is found, but will complain if every position is missing.
function translate($in) {
$out = array();
// first pass, move root nodes to output
foreach ($in as $k => $row) {
if (!$row['position']) {
$out[$row['identifier']] = $row;
unset($in[$k]);
}
}
// while we have input
do {
$elements_placed = 0;
// step trough input
foreach ($in as $row_index => $row) {
foreach ($row['position'] as $pos) {
// build context for the node placing
$data = array(
'row' => $row,
'in' => &$in,
'row_index' => $row_index,
'elements_placed' => &$elements_placed,
'pos' => $pos,
);
// kick of recursion
walker($out, $data);
}
}
} while ($elements_placed != 0);
if (count($in)) {
trigger_error("Error in user data, can't place every item");
}
return $out;
}
function walker(&$out, $data) {
foreach ($out as &$row) {
// it looks like a node array
if (is_array($row) && isset($row['identifier'])) {
// if it has children recurse in there too
if (isset($row['children'])) {
walker($row['children'], $data);
}
// it looks like a node array that we are looking for place the row
if ($row['identifier'] == $data['pos']) {
if (!isset($row['children'])) {
$row['children'] = array($data['row']['identifier'] => $data['row']);
} else {
$row['children'][$data['row']['identifier']] = $data['row'];
}
// report back to the solver that we found a place
++$data['elements_placed'];
// remove the row from the $in array
unset($data['in'][$data['row_index']]);
}
}
}
}
$in = array (
array (
'identifier' => 'secondlevelchild2',
'text' => 'secondlevelchild2',
'url' => '',
'position' => array (
'dashboard2',
),
),
array (
'identifier' => 'secondlevelchild',
'text' => 'secondlevelchild',
'url' => '',
'position' => array (
'dashboard2',
),
),
array (
'identifier' => 'dashboard',
'text' => 'Dashboard',
'url' => '',
'position' => '',
),
array (
'identifier' => 'dashboard2',
'text' => 'Dashboard2',
'url' => '',
'position' => array (
'dashboard', 'home',
),
),
array (
'identifier' => 'thirdlevelchild',
'text' => 'thirdlevelchild',
'url' => '',
'position' => array (
'secondlevelchild2',
),
),
);
$out = translate($in);
var_export($out);
In it's current form it doesn't remove the identifier or position keys from the node arrays once they are placed.
You need a temporary array that maps the identifier to the children array of it so that you can add it there.
If I see that right, you have something like that already here when you add the parent:
$nav[$nav_item['identifier']] = $nav_item;
Edit: Adding the parent needs some caution as pointed out in the comment:
$node = &$nav[$nav_item['identifier']];
$children = isset($node['children']) ? $node['children'] : array();
$node = $nav_item;
$node['children'] = $children;
unset($node);
Just add to the children then:
foreach($nav_item['position'] as $identifier)
{
$nav[$identifier]['children'][] = $nav_item;
}
Also you could use objects, not arrays, so that you do not duplicate data that much (or you can change it later), however, just thinking, this must not be necessary to solve your problem so probably something for later.