Page 1 of 1
How to loop through this array?
Posted: Wed Sep 28, 2005 1:33 am
by flokky
Hi, I'm kinda confused how to fetch the data in a complicated array. I've used the print_r($array) command to print out what's exactly in the array. This is the print-out:
Code: Select all
Array ( [0] => Array ( [name] => USERS [content] => [child] => Array ( [0] => Array ( [name] => USER [content] => [child] => Array ( [0] => Array ( [name] => UNIQUEID [content] => 30750021515151351ae5151515151861 ) [1] => Array ( [name] => UNIQUEID_CRM [content] => 5413F145454546844DB54F002654B ) [2] => Array ( [name] => USERID [content] => motorcrosser@gmail.com ) [3] => Array ( [name] => PASSWORD [content] => cc9854841515ddd5423f548f4fea ) [4] => Array ( [name] => PASSWORDCLEAR [content] => mypassword2000 ) [5] => Array ( [name] => ACCESS [content] => NO ) [6] => Array ( [name] => AUTHORITIES [content] => [child] => Array ( [0] => Array ( [name] => AUTHORITY_ENTRY [content] => jdk ) ) ) ) ) [1] => Array ( [name] => USER [content] => [child] => Array ( [0] => Array ( [name] => UNIQUEID [content] => 000000005454684535153138543813841 ) [1] => Array ( [name] => UNIQUEID_CRM [content] => 84684638438438413000848048484 ) [2] => Array ( [name] => USERID [content] => joske_vermeulen@hotmail.com ) [3] => Array ( [name] => PASSWORD [content] => 5468131501840dsfd84848d408 ) [4] => Array ( [name] => PASSWORDCLEAR [content] => joske982 ) [5] => Array ( [name] => ACCESS [content] => YES ) [6] => Array ( [name] => AUTHORITIES [content] => [child] => Array ( [0] => Array ( [name] => AUTHORITY_ENTRY [content] => customer-client ) ) ) ) ) ) ) )
How can I loop through it? Thanks a million!

Posted: Wed Sep 28, 2005 2:03 am
by ruchit
first you'll have to tell us how you created such an array....

Posted: Wed Sep 28, 2005 2:19 am
by flokky
I've used a script which I found on the net, which makes it possible to parse an XML-file. I want to iterate through every element and later on put those data in a mysql database. I want to be able to put every element in an variable before I insert it into the DB.
This is the script:
Code: Select all
<?php
$p =& new xmlParser();
$p->parse('sampleuser.xml');
print_r($p->output);
// how can I loop through the output array?
$numElements = count($p);
echo "number of elements: ". $numElements;
foreach($p as $uniqueid)
{
echo $p['PASSWORD'];
}
class xmlParser{
var $xml_obj = null;
var $output = array();
function xmlParser(){
$this->xml_obj = xml_parser_create();
xml_set_object($this->xml_obj,$this);
xml_set_character_data_handler($this->xml_obj, 'dataHandler');
xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
}
function parse($path){
if (!($fp = fopen($path, "r"))) {
die("Cannot open XML data file: $path");
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_obj, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_obj)),
xml_get_current_line_number($this->xml_obj)));
xml_parser_free($this->xml_obj);
}
}
return true;
}
function startHandler($parser, $name, $attribs){
$_content = array('name' => $name);
if(!empty($attribs))
$_content['attrs'] = $attribs;
array_push($this->output, $_content);
}
function dataHandler($parser, $data){
if(!empty($data)) {
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['content'] = $data;
}
}
function endHandler($parser, $name){
if(count($this->output) > 1) {
$_data = array_pop($this->output);
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['child'][] = $_data;
}
}
}
?>
Thank you so much for helping me.
Posted: Wed Sep 28, 2005 2:38 am
by ruchit
i am trying... i would also need you xml file... b'cos lines like this
[name] => USER [content] => [child] => Array
look problematic... might need tweaking the xml a little bit
Posted: Wed Sep 28, 2005 2:53 am
by flokky
Well, actually that's kinda my problem too.

This is an XML file which I receive from a customer. This is the way how he manages his users.
The XML-file: (which only has 2 users, but in the real file there are more than 800 users)
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<uniqueid>30750021515151351ae5151515151861</uniqueid>
<uniqueid_crm>5413F145454546844DB54F002654B</uniqueid_crm>
<userid>motorcrosser@gmail.com</userid>
<password>cc9854841515ddd5423f548f4fea</password>
<passwordclear>mypassword2000</passwordclear>
<access>NO</access>
<authorities>
<authority_entry>jdk</authority_entry>
</authorities>
</user>
<user>
<uniqueid>000000005454684535153138543813841</uniqueid>
<uniqueid_crm>84684638438438413000848048484</uniqueid_crm>
<userid>joske_vermeulen@hotmail.com</userid>
<password>5468131501840dsfd84848d408</password>
<passwordclear>joske982</passwordclear>
<access>YES</access>
<authorities>
<authority_entry>customer-client</authority_entry>
</authorities>
</user>
</users>
I simply can't figure it out

Posted: Wed Sep 28, 2005 3:13 am
by n00b Saibot
Post your DB structure, its not as complicated as you think. Once you get in depth, you won't take time to figure it out...
Posted: Wed Sep 28, 2005 3:19 am
by flokky
Hi n00b Saibot,
I don't have any DB structure. The only thing I've got is this XML file. I just need to figure out how to parse the elements out of the file, which I can place in a MySQL table.
At this point, I need to know how to loop through that array. (with the possibility to place the element data in a variable.
Posted: Wed Sep 28, 2005 4:19 am
by Jenk
Take a look at making a recursive function
Here is a quick example of a recursive function, which flattens arrays
Code: Select all
<?php
function recursive ($arr, $output = array()) {
foreach ($arr as $key) {
if (is_array($key)) {
$subarray = recursive($key);
foreach($subarray as $sub) {
$output[] = $sub;
}
}
else {
$output[] = "$key";
}
}
return $output;
}
$array = array("0", "1", array("2", "3", array("4", array("5", "6"), "7", "8")), "9", "10", array("11"), array("12","13"));
$guff = recursive($array);
var_dump($array);
echo "\n================\n";
var_dump($guff);
?>
Outputs:
Code: Select all
array(7) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
array(3) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
[2]=>
array(4) {
[0]=>
string(1) "4"
[1]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "6"
}
[2]=>
string(1) "7"
[3]=>
string(1) "8"
}
}
[3]=>
string(1) "9"
[4]=>
string(2) "10"
[5]=>
array(1) {
[0]=>
string(2) "11"
}
[6]=>
array(2) {
[0]=>
string(2) "12"
[1]=>
string(2) "13"
}
}
================
array(14) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
[4]=>
string(1) "4"
[5]=>
string(1) "5"
[6]=>
string(1) "6"
[7]=>
string(1) "7"
[8]=>
string(1) "8"
[9]=>
string(1) "9"
[10]=>
string(2) "10"
[11]=>
string(2) "11"
[12]=>
string(2) "12"
[13]=>
string(2) "13"
}
You could use the above once you have parsed the XML file into an array, and then loop though to do whatever it is you want to do with it

Posted: Wed Sep 28, 2005 5:58 am
by flokky
Hey thx Jenk! I'm going to try this.