PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
flokky
Forum Newbie
Posts: 5 Joined: Wed Sep 28, 2005 1:29 am
Post
by flokky » Wed Sep 28, 2005 1:33 am
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!
ruchit
Forum Commoner
Posts: 53 Joined: Mon Sep 26, 2005 6:03 am
Post
by ruchit » Wed Sep 28, 2005 2:03 am
first you'll have to tell us how you created such an array....
flokky
Forum Newbie
Posts: 5 Joined: Wed Sep 28, 2005 1:29 am
Post
by flokky » Wed Sep 28, 2005 2:19 am
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.
ruchit
Forum Commoner
Posts: 53 Joined: Mon Sep 26, 2005 6:03 am
Post
by ruchit » Wed Sep 28, 2005 2:38 am
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
flokky
Forum Newbie
Posts: 5 Joined: Wed Sep 28, 2005 1:29 am
Post
by flokky » Wed Sep 28, 2005 2:53 am
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
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Wed Sep 28, 2005 3:13 am
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...
flokky
Forum Newbie
Posts: 5 Joined: Wed Sep 28, 2005 1:29 am
Post
by flokky » Wed Sep 28, 2005 3:19 am
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.
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Wed Sep 28, 2005 4:19 am
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
flokky
Forum Newbie
Posts: 5 Joined: Wed Sep 28, 2005 1:29 am
Post
by flokky » Wed Sep 28, 2005 5:58 am
Hey thx Jenk! I'm going to try this.