HELP - advanced array merge
Posted: Sat Sep 27, 2008 7:48 am
Hi all,
I'm currently working on a projekt that demands multidimensions array that are quite "deep". Now I need to merge many of theese in to one big multi dimesion array that hold the whole structure and data. And it is a litle tricky for me...
For example do I have:
$multi['users_collection'][0]['user'][0]['id'] = id;
$multi['users_collection'][0]['user'][0]['name'] = name;
$multi['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi['users_collection'][0]['books'][0]['book'][0]['name'] = name;
So far so good... I works no problem. But now I want to add a bok to the collection of book but with the same user as folow.The array looks like this:
$mult2i['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi2['users_collection'][0]['books'][0]['book'][0]['name'] = name;
And I whant it to become:
$multi['users_collection'][0]['user'][0]['id'] = id;
$multi['users_collection'][0]['user'][0]['name'] = name;
$multi['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi['users_collection'][0]['books'][0]['book'][0]['name'] = name;
$multi['users_collection'][0]['books'][1]['book'][0]['id'] = id; *
$multi['users_collection'][0]['books'][1]['book'][0]['name'] = name; *
*(note the number 1 after 'book').
As you see these two arrays is in two completly diffrent arrays I might add. I can't use:
$new_book['book'][0]['id'] = id;
array_push($multi['users_collection'][0]['books'], $new_book)
I have to merge the whole structure and make it react as I want it to react.
It becomes even more fun when I whant to add a whole new user collection as folows:
$multi3['users_collection'][1]['user'][0]['id'] = id;
$multi3['users_collection'][1]['user'][0]['name'] = name;
$multi3['users_collection'][1]['books'][0]['book'][0]['id'] = id;
$multi3['users_collection'][1]['books'][0]['book'][0]['name'] = name;
$multi3['users_collection'][1]['books'][1]['book'][0]['id'] = id;
$multi3['users_collection'][1]['books'][1]['book'][0]['name'] = name;
The arrays may contains diffrent keys form time to time so not all data will be in 'user_collection' some data may for example be in "root key" ['genral_collections'] or something completly diffrent. Then it has to create this whole new tree. This means also that I cant use the key names as constants values for if statements and so on because that they might be diffrent every time...
Both array_merge and array_merge_recursive give me the wrong result so it seems that I have to create my own merge function. Any tip with some do's and don'ts?
Gratefull for any tips...
//Seigan
I'm currently working on a projekt that demands multidimensions array that are quite "deep". Now I need to merge many of theese in to one big multi dimesion array that hold the whole structure and data. And it is a litle tricky for me...
For example do I have:
$multi['users_collection'][0]['user'][0]['id'] = id;
$multi['users_collection'][0]['user'][0]['name'] = name;
$multi['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi['users_collection'][0]['books'][0]['book'][0]['name'] = name;
So far so good... I works no problem. But now I want to add a bok to the collection of book but with the same user as folow.The array looks like this:
$mult2i['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi2['users_collection'][0]['books'][0]['book'][0]['name'] = name;
And I whant it to become:
$multi['users_collection'][0]['user'][0]['id'] = id;
$multi['users_collection'][0]['user'][0]['name'] = name;
$multi['users_collection'][0]['books'][0]['book'][0]['id'] = id;
$multi['users_collection'][0]['books'][0]['book'][0]['name'] = name;
$multi['users_collection'][0]['books'][1]['book'][0]['id'] = id; *
$multi['users_collection'][0]['books'][1]['book'][0]['name'] = name; *
*(note the number 1 after 'book').
As you see these two arrays is in two completly diffrent arrays I might add. I can't use:
$new_book['book'][0]['id'] = id;
array_push($multi['users_collection'][0]['books'], $new_book)
I have to merge the whole structure and make it react as I want it to react.
It becomes even more fun when I whant to add a whole new user collection as folows:
$multi3['users_collection'][1]['user'][0]['id'] = id;
$multi3['users_collection'][1]['user'][0]['name'] = name;
$multi3['users_collection'][1]['books'][0]['book'][0]['id'] = id;
$multi3['users_collection'][1]['books'][0]['book'][0]['name'] = name;
$multi3['users_collection'][1]['books'][1]['book'][0]['id'] = id;
$multi3['users_collection'][1]['books'][1]['book'][0]['name'] = name;
The arrays may contains diffrent keys form time to time so not all data will be in 'user_collection' some data may for example be in "root key" ['genral_collections'] or something completly diffrent. Then it has to create this whole new tree. This means also that I cant use the key names as constants values for if statements and so on because that they might be diffrent every time...
Both array_merge and array_merge_recursive give me the wrong result so it seems that I have to create my own merge function. Any tip with some do's and don'ts?
Gratefull for any tips...
//Seigan