...

View Full Version : Processing Arrays



poyntesm
02-23-2010, 09:33 PM
Hi,

I need a bit of help processing an array structure.


$array = array(
"ITEM1" => array(
"SUBITEM1",
"SUBITEM2" => array(
"SUBSUBITEM1",
"SUBSUBITEM2",
),
"SUBITEM3",
),
"ITEM2" => array(
"SUBITEM4",
"SUBITEM5",
),
);

I want to process in a loop each item, then subitem and if existing any subsubitem. Thanks for any pointers.

Fumigator
02-23-2010, 09:43 PM
Maybe array_walk_recursive()?

http://us2.php.net/manual/en/function.array-walk-recursive.php

poyntesm
02-23-2010, 09:44 PM
Sorry should also say this will be used to build a insert SQL statement. Each item will be assigned to a id, the sub item will know this id and the subsubitem will now its subitem id.

If that makes senses, SQL it would produce is below.



INSERT INTO `table` (`item_id`, `item`) VALUES ('1', 'ITEM1');
INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('1', '1', 'SUBITEM1');
INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('2', '1', 'SUBITEM2');
INSERT INTO `table3` (`subsub_item_id`, `sub_item_id`, `subsub_item`) VALUES ('1', '1', 'SUBSUBITEM1');
INSERT INTO `table3` (`subsub_item_id`, `sub_item_id`, `subsub_item`) VALUES ('2', '1', 'SUBSUBITEM2');
INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('3', '1', 'SUBITEM3');
INSERT INTO `table` (`item_id`, `item`) VALUES ('2', 'ITEM2');
INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('4', '1', 'SUBITEM4');
INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('5', '1', 'SUBITEM5');

poyntesm
02-23-2010, 10:29 PM
Found a solution, might be better options, but this works :)


$i=1;
$j=1;
$k=1;
foreach ($array as $key => $value )
{
echo "INSERT into table 1 (item_id, item) VALUES ( $i, $key) "."<br>";
foreach ($array[$key] as $key2 => $value2 )
{
if (is_array($array[$key][$key2]))
{
echo "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, $key2) "."<br>";
foreach ($array[$key][$key2] as $key3 => $value3 )
{
echo "INSERT into table3 (subsubitem_id, sub_item_id, subsub_item) VALUES ( $k, $j, {$array[$key][$key2][$key3]}) "."<br>";
$k++;
}
}
else
{
echo "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, {$array[$key][$key2]}) "."<br>";
}
$j++;
}
$i++;
}

Fou-Lu
02-23-2010, 10:35 PM
Found a solution, might be better options, but this works :)


$i=1;
$j=1;
$k=1;
foreach ($array as $key => $value )
{
echo "INSERT into table 1 (item_id, item) VALUES ( $i, $key) "."<br>";
foreach ($array[$key] as $key2 => $value2 )
{
if (is_array($array[$key][$key2]))
{
echo "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, $key2) "."<br>";
foreach ($array[$key][$key2] as $key3 => $value3 )
{
echo "INSERT into table3 (subsubitem_id, sub_item_id, subsub_item) VALUES ( $k, $j, {$array[$key][$key2][$key3]}) "."<br>";
$k++;
}
}
else
{
echo "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, {$array[$key][$key2]}) "."<br>";
}
$j++;
}
$i++;
}

How much depth can you're array have for its dimensions? I'm kinda under the impression that you can go with an infinite depth.
If so, look into converting this to a recursive function instead, that way you can just call it over and over within the stack and process as you see fit. Logic coverage will be about the same, but you'll need only one loop, a recursion call and a bit of control on it. This one shouldn't be that bad. If you're max is 3 deep like you have in you're example, than you're perfect to stay with what you have.

I'd actually look at doing some sorting first, and then parsing to the SQL. This will let you execute multiple insertions against a single insert command so that will keep you're requesting down to hopefully speed things up.


Sorry I should really mention, I'm about to leave work too and don't have my net set up at home yet, so I won't be able to help you writing the function. Only thing I can suggest is that you have two parameters, one for the collection itself (which base case will be a scalar value), and one for its 'parent' (null by default from the looks of it).

poyntesm
02-23-2010, 10:41 PM
Max is 3 levels deep but i will look to make it recursive for the fun.

The array will only be processed most likely once as its a initial data load situation. After that all entries will be well controlled via a page and all info known to build SQL.

But for the initial load I did not want to hardcode values and then spot I missed a entry and have to adjust all linked values.Now I just populate the array and SQL will be handled.

My PHP is a bit rusty so was interested in some help, seems I should have just let my brain warm up a bit more ;)

Thanks all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum