Thanks Sfullman 🙂
I tried that and it came up with some strange results. I think I have too much trash lying around in the matches for it too work properly. Luckily I received help and was given this:
<?php
$lines = array(
'<item><1>abc1</1><2>abc2</2><3>abc3</3><4>abc4</4></item>',
'<item><1>abc1</1><3>abc3</3><5>abc5</5><6>abc6</6></item>',
'<item><1>abc1</1><6>abc6</6><2>abc2</2><3>abc3</3></item>'
);
$data = array();
$num = 1;
foreach ($lines as $line) {
preg_match_all('%<(\d+)>(.+?)</\1>%', $line, $matches);
foreach ($matches[1] as $key => $value) {
$data[$num][$value] = $matches[2][$key];
}
++$num;
}
foreach ($data as &$line) {
for ($key = 1; $key <= 6; $key++) {
if (!array_key_exists($key, $line)) {
$line[$key] = 'none';
}
}
}
print_r($data);
?>
Which worked extremely well based on the way my tags are formed. Now the question is how to get the array in to the database. The previous work I have done with preg match and arrays into database has always had all of the same values in one array (i.e. everything between tag <1>(.+)</1> in $matches[1]) which has allowed me to use a simple loop
foreach ($matches[1] as $row=>$one)
and then insert each item to the database.
The array I have from the above is in this format:
Array
(
[1] => Array
(
[1] => Description1
[4] => Name1
[6] => Date1
[8] => URL1
[2] => none
[3] => none
[5] => none
[7] => none
[9] => none
[10] => none
)
[2] => Array
(
[1] => Description2
[4] => Name2
[6] =>Date2
[8] => URL2
[9] => none
[10] => Picture2
[2] => none
[3] => none
[5] => none
[7] => none
)
[3] => Array
(
[1] => Description3
[2] => Homepage3
[3] => City3
[4] => Name3
[6] => Date3
[7] => Year3
[8] => none
[5] => none
[9] => none
[10] => Picture3
)
[4] => Array
(
[1] => Description4
[2] => Homepage4
[3] => City4
[4] => Name4
[5] => Product4
[6] => none
[7] => Year4
[8] => none
[9] => none
[10] => none
)
)
I am searching high and low to find an answer or at least a good tutorial on how to insert this type of array in to my table where each part of each array represents a different field. Any ideas or pointers?
Thanks
Pat