Page 1 of 1

multi-d array question

Posted: Sat Apr 15, 2006 7:12 pm
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

Posted: Sat Apr 15, 2006 8:36 pm
by feyd
I'm going to need more examples.

Posted: Sun Apr 16, 2006 3:50 am
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

Posted: Sun Apr 16, 2006 12:32 pm
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.

Posted: Sun Apr 16, 2006 1:26 pm
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

Posted: Sun Apr 16, 2006 1:32 pm
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.

Posted: Sun Apr 16, 2006 1:41 pm
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.

Posted: Sun Apr 16, 2006 1:45 pm
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?

Posted: Sun Apr 16, 2006 2:10 pm
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

Posted: Sun Apr 16, 2006 2:52 pm
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.

Posted: Sun Apr 16, 2006 3:29 pm
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

Posted: Sun Apr 16, 2006 3:31 pm
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;
}

}

Posted: Tue Apr 18, 2006 3:01 am
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?