Reply to Re: Finding key level in mutidimensional array

Your name:

Reply:


Posted by Manish on 08/09/06 10:09

Thanks for the comment and suggesting the better approach. As I have
mentioned in my previous post, all the datas are in the database and
the first array I have is as you mentioned,

Array
(
[1003] => Array('parent' => null) // Super element
[1014] => Array('parent' => 1003) // Child of the super element
[1006] => Array('parent' => 1014) // Child of the child
[1018] => Array('parent' => 1006, 'value' => 0) // Last
[1015] => Array('parent' => 1014) // Child of the child
)

>From this array itself I have created a new multidimensional array, as
depicted in first message.


As mentioned in your example data,

[1018] => Array('parent' => 1006, 'value' => 0) // Last

value=>'0' is added by me, intentionally as just to set some value. It
will either be an array, for if any sub categories are there, and it is
at last, we can set it as null. It just shows that nothing is there
below that level.

So, this array will also work,

Array
(
[1003] => null // Super element
[1014] => 1003 // Child of the super element
[1006] => 1014 // Child of the child
[1018] => 1006 // Last
[1015] => 1014 // Child of the child
)

>From this array itself, new array was created, so that when user clicks
on 1006, both id 1018 and 1019 (table with id 1018, 1019) are to be
made visible/hidden.

[1006] => Array
(
[1018] => 0
[1019] => 0
)


Now, we have to restrict the category creation after say level 5, so I
want to find the current level, so that whether to enable/disable the
button to create more sub-category and display appropriate message.

Thanks.

Manish


Carl Vondrick wrote:
> > foreach($multilevelarr as $thiskey=>$thisvalue) {
> > if($thiskey == $key) {
> > return array(1, ($currlevel + 1));
> > }
> > }
> > foreach($multilevelarr as $thiskey=>$thisvalue) {
> > if(is_array($thisvalue)) {
> > list($found, $foundlevel) = KeyLevel($thisvalue, $key, $currlevel +
> > 1);
> > if($found) {
> > return array(1, $foundlevel);
> > }
> > }
> > }
>
> Your complexity here is O(2N), when it could just be O(N). These two
> loops are identical. My suggestion:
>
> As you loop through them, check BOTH if the key exists or if it's an
> array. If it's an array, start a recursion.
>
> I realize, however, that you may want to search for the key first, then
> sub-arrays (the best optimization here pivots on the context of the
> problem), but I would still have one master loop that stores the arrays
> in a hash.
>
> Alternatively, do you have to be using your array structure like that?
> May I suggest a more sane, but perhaps more complicated approach? Try
> using a parent-child system, as the keys are all identical anyways.
>
> So, for example:
> Array
> (
> [1003] => Array('parent' => null) // Super element
> [1014] => Array('parent' => 1003) // Child of the super element
> [1006] => Array('parent' => 1014) // Child of the child
> [1018] => Array('parent' => 1006, 'value' => 0) // Last
> [1015] => Array('parent' => 1014) // Child of the child
> )
>
> And so on. Then, your system is a lot simpler:
> 1) Find the base key
> 2) If parent is not null, then find the key of the parent, increase
> counter by 1, and repeat. If it is, return the counter.
>
> All the best,
> Carl

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация