multi-d array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

multi-d array question

Post by rubberjohn »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm going to need more examples.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

lets say u have an array (1,2,3,4)
this then becomes 1(2(3(4)))
could you rewrite these as proper php array initalizations for me. I can't see how they should be structured from that alone.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Although I think this is a silly way of storing your hierarchy:

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;
}
That's so untested.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

rubberjohn wrote:ets say u have an array (1,2,3,4)
So you have:

Code: Select all

<?php
$my_array = array(1, 2, 3, 4);
?>
rubberjohn wrote:this then becomes 1=array(2 => array(3 => array(4)))
So now you have:

Code: Select all

<?php
$my_array = array(array(2 => array(3 => array(4))), 2, 3, 4);
?>
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
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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I am wondering what problem you are trying to solve with these arrays. I can't imagine that the road you are headed down is the right direction. Perhaps if you gave use the bigger picture?
(#10850)
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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)) )
Your array would be:

Code: Select all

<?php
$computing = array(
    'databases' = array(
        'mysql',
        'db2',
        'postgresql'
    ),
    'programming' = array(
        'mysql'
    )
);
?>
There has got to be a better way to do this. Using arrays like this can get difficult to manage or pass off to someone else later on. Is it possible to assign each possible user selected path an id then store the id in a database? That would make it easier to track, although I am not sure if it would help you in the other parts of your script.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

rubberjohn wrote: computing>>databases>>mysql
computing>>databases>>db2
computing>>databases>>postgresql
computing>>programming languages >>php
You might want to think about putting them into an object and serilaizing that. Then the interface could insulate you from changes in the future

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)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

add this array (1,2,5,9)

would make the multi-d array - 1=array(2 => array(3 => array(4) 5 => array(9) ))
OK I don't understand what's going on there but I get the first bit easy enough.
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))));
Anyway besides that bit here's the code for the first bit; tested. I would like to take this opportunity to state that untested code is useful to noone. Anyway:

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);
produces

Code: Select all

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [3] => Array
                        (
                            [0] => 4
                        )

                )

        )

)
Any good?
Post Reply