multi-d array question
Moderator: General Moderators
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
multi-d array question
if you have an array, of an indefinite size, how do you use the values to create a multidimensional array?
but not like root[ [sub_topic1] [sub_topic2] [leaf] ]
more like root[ [sub_topic1 [sub_topic2 [leaf] ] ]
thanks
rj
but not like root[ [sub_topic1] [sub_topic2] [leaf] ]
more like root[ [sub_topic1 [sub_topic2 [leaf] ] ]
thanks
rj
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
lets say u have an array (1,2,3,4)
this then becomes 1(2(3(4)))
add this array (1,2,5,9)
would make the multi-d array - 1( 2( 3 (4) ) (5,(9)) )
and adding (1,2,3,7)
would result in 1( 2( 3 (4,7) ) (5,(9)) )
im not sure if that makes it any clearer - maybe im going about this the wrong way - all i am trying to do is to combine a number of arrays so i can output them all in a clean way - for eg the multi-d array would produce
1
--2
----3 - 4
--------7
----5 - 9
i have looked at viewtopic.php?t=25708 but my implementation is slightly different so it wouldn't work.
thanks
rj
this then becomes 1(2(3(4)))
add this array (1,2,5,9)
would make the multi-d array - 1( 2( 3 (4) ) (5,(9)) )
and adding (1,2,3,7)
would result in 1( 2( 3 (4,7) ) (5,(9)) )
im not sure if that makes it any clearer - maybe im going about this the wrong way - all i am trying to do is to combine a number of arrays so i can output them all in a clean way - for eg the multi-d array would produce
1
--2
----3 - 4
--------7
----5 - 9
i have looked at viewtopic.php?t=25708 but my implementation is slightly different so it wouldn't work.
thanks
rj
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
ets say u have an array (1,2,3,4)
this then becomes 1=array(2 => array(3 => array(4)))
add this array (1,2,5,9)
would make the multi-d array - 1=array(2 => array(3 => array(4) 5 => array(9) ))
and adding (1,2,3,7)
would result in 1=array(2 => array(3 => array(4,7) 5 => array(9) ))
i think thats it - is that any clearer
rj
this then becomes 1=array(2 => array(3 => array(4)))
add this array (1,2,5,9)
would make the multi-d array - 1=array(2 => array(3 => array(4) 5 => array(9) ))
and adding (1,2,3,7)
would result in 1=array(2 => array(3 => array(4,7) 5 => array(9) ))
i think thats it - is that any clearer
rj
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Although I think this is a silly way of storing your hierarchy:That's so untested.
Code: Select all
function storeInto($a, &$b)
{
$val = array_pop($a);
$c =& $b;
foreach($a as $v)
{
if (!is_array($c[$v]))
{
$c[$v] = array();
}
$c =& $c[$v];
}
$c[] = $val;
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
So you have:rubberjohn wrote:ets say u have an array (1,2,3,4)
Code: Select all
<?php
$my_array = array(1, 2, 3, 4);
?>So now you have:rubberjohn wrote:this then becomes 1=array(2 => array(3 => array(4)))
Code: Select all
<?php
$my_array = array(array(2 => array(3 => array(4))), 2, 3, 4);
?>Not really. Are you trying to merge arrays? Or maybe combine arrays? Where are you getting the arrays? What do they look like in real life (are they really numerical values)? What is the desired effect with these after they are combined/merged? Please provide more details.rubberjohn wrote:add this array (1,2,5,9)
would make the multi-d array - 1=array(2 => array(3 => array(4) 5 => array(9) ))
and adding (1,2,3,7)
would result in 1=array(2 => array(3 => array(4,7) 5 => array(9) ))
i think thats it - is that any clearer
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
im in a real rush to get this done so i dont think i am making the best descisions about implementations - at this stage i just need this to work as i need a prototype ready for next week
the arrays are taken from a database, where they are serialized, they represent the selection tree a user has previously made.
for example computing>>databases>>mysql
this is why each subsequent value has to be inside the previous array - to represent its relationship to it
what i am trying to achieve is to tidy this up as there are many arrays each representing a choice that the user has made and stored
so
computing>>databases>>mysql
computing>>databases>>db2
computing>>databases>>postgresql
computing>>programming languages >>php
becomes
computing = array(databases=array(mysql,db2,postgresql) ) (programming = array(php)) )
im open to suggestions about making this better although at his point i cant really change my implementation too much.
thanks for your help
rj
the arrays are taken from a database, where they are serialized, they represent the selection tree a user has previously made.
for example computing>>databases>>mysql
this is why each subsequent value has to be inside the previous array - to represent its relationship to it
what i am trying to achieve is to tidy this up as there are many arrays each representing a choice that the user has made and stored
so
computing>>databases>>mysql
computing>>databases>>db2
computing>>databases>>postgresql
computing>>programming languages >>php
becomes
computing = array(databases=array(mysql,db2,postgresql) ) (programming = array(php)) )
im open to suggestions about making this better although at his point i cant really change my implementation too much.
thanks for your help
rj
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Your array would be:rubberjohn wrote:computing>>databases>>mysql
computing>>databases>>db2
computing>>databases>>postgresql
computing>>programming languages >>php
becomes
computing = array(databases=array(mysql,db2,postgresql) ) (programming = array(php)) )
Code: Select all
<?php
$computing = array(
'databases' = array(
'mysql',
'db2',
'postgresql'
),
'programming' = array(
'mysql'
)
);
?>-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
cheers for that but how could i make that dynamic because the user can have any number of these arrays in the database whilst at the same time have a variable number of items in the array
i would have thought there is a better way of doing this but cos im rushing things i cant get it perfect
each stored profile does have its own ID - tblPROFILE(profile_id, user_id, root, tree, leaf)
cheers
rj
i would have thought there is a better way of doing this but cos im rushing things i cant get it perfect
each stored profile does have its own ID - tblPROFILE(profile_id, user_id, root, tree, leaf)
cheers
rj
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
You might want to think about putting them into an object and serilaizing that. Then the interface could insulate you from changes in the futurerubberjohn wrote: computing>>databases>>mysql
computing>>databases>>db2
computing>>databases>>postgresql
computing>>programming languages >>php
Code: Select all
class Computing {
var $databases = array();
var $languages = array();
function addDatabase($name) {
$this->databases[] = $name;
}
function addLanguage($name) {
$this->languages[] = $name;
}
function getDatabases() {
return $this->databases;
}
function getLanguages() {
return $this->languages;
}
}(#10850)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
OK I don't understand what's going on there but I get the first bit easy enough.add this array (1,2,5,9)
would make the multi-d array - 1=array(2 => array(3 => array(4) 5 => array(9) ))
Again can I ask you to write this as PHP, and by that I mean code I can put in my script and will work. Anything less is ambigious.
Are you trying to say:
Code: Select all
$fromPrevious = array(1 => array(2 => array(3 => array(4))));
$add = array(1,2,5,9); // \/ comma here or another nested array()?
$result = array(1 => array(2 => array(3 => array(4), 5 => array(9))));Code: Select all
$in = array(1,2,3,4);
function storeInto($a) {
$aLast = count($a) - 1;
$result = array($a[$aLast]);
for($i=$aLast-1;$i>=0;$i--) {
$result = array($a[$i] => $result);
}
return $result;
}
print_r($in);
$out = storeInto($in);
print_r($out);Code: Select all
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array
(
[1] => Array
(
[2] => Array
(
[3] => Array
(
[0] => 4
)
)
)
)