Forum Moderators: coopster

Message Too Old, No Replies

Array problem

I need a solution for array conversion from script.aculo.us returned string

         

asantos

3:33 pm on May 24, 2006 (gmt 0)

10+ Year Member



Hi,
Im using Script.Aculo.Us' "List Tree component" (in javascript) to build a website's structure (each 'div' is called a placeholder in this CMS). After ordening the tree with the component, it returns a string like this:

$string = 'template[0]=1&template[0][0]=2&template[0][1]=3&template[0][1][0]=5&template[0][1][1]=7&template[0][1][2]=6&template[0][1][3]=8&template[0][2]=4';

That string holds information about each placeholder of the website. BTW, a specific placeholder can be the parent of another placeholder, and all placeholders that are nested in a parent-placeholder have an specific order (starting form 0).

If I do this <?$arr = explode('&',$string)?>, i'll get this:
$arr[0] = "template[0]=7"
$arr[1] = "template[1]=5"
$arr[2] = "template[2]=1"
$arr[3] = "template[2][0]=2"
$arr[4] = "template[2][0][0]=6"
$arr[5] = "template[2][1]=3"
$arr[6] = "template[2][2]=8"
$arr[7] = "template[3]=4"

The placeholder's ID's are only contained after the "=" .. the pseudo-array in the string only contains ordered information of a nested order structure.

I need a way to manipulate that string so i know the placeholder's parent and order.
For example, it would be great to get something like this as a result:

# Page
$placeholder[1] = array(0,2);
# Sidebar
$placeholder[2] = array(1,0);
...
# Footer
$placeholder[4] = array(0,3);
...you get the point

In other words:
$placeholder[X] = array($parent,$order);

WHERE:
X = ID of placeholder
$parent = ID of placeholder's parent
$order = order of placeholder relative to other placeholders nested in the same parent

Any ideas on that?

Salute,
Andres S.

eelixduppy

11:04 am on May 25, 2006 (gmt 0)



You could use a variety of string functions [us3.php.net] but personally i would try to find another way of returning the information from the function to make it easier to manipulate. Maybe return a multi-dimensional array instead of a string?

asantos

3:55 pm on May 25, 2006 (gmt 0)

10+ Year Member



eelixduppy, the thing is, i dont need a multidimensional array as a result, i only need to know the id of the placeholder, its father and its order. i came up with this already working solution yesterday:

foreach(explode('&',str_replace('plantilla','','plantilla[0]=1&plantilla[0][0]=2&plantilla[0][1]=3&plantilla[0][1][0]=5&plantilla[0][1][1]=6&plantilla[0][1][2]=7&plantilla[0][1][3]=8&plantilla[1]=4')) as $id) {
$item = explode('=',$id);
$items[$item[1]] = $item[0];
}
foreach($items as $k=>$item) {
$padre = substr($item,0,strrpos($item,'['));
$orden = substr($item,strrpos($item,'[')+1,strrpos($item,']')-strrpos($item,'[')-1);
$placeholders[$k] = array($padre,$orden);
}