By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

array of...array....

P: n/a
I've this structure in a XML file:
Type
Model
Detail
Datas
Datas
Datas
Datas
/Detail
Detail
Datas
Datas
Datas
/Detail
/Model
/Type

I've parsed all the stuff, by putting everything in an array.

Now, I've a problem as I must use datas in "Detail" tag every time they are
processed, but I must first process datas in Model, so I have to wait until
Model tag is closed.

For now I put everything in an array called $datas (where I store the couple
key/value). How can I do to create, at a certain point, a new array for
storing the Detail array (in order to have an array for every Detail that
may occur, as this isn't a fixed number of Details ?)

Thanks for helping.
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Bob Bedford" <be******@YouKnowWhatToDoHerehotmail.com> wrote in message
news:40***********************@news.sunrise.ch...
I've this structure in a XML file:
Type
Model
Detail
Datas
Datas
Datas
Datas
/Detail
Detail
Datas
Datas
Datas
/Detail
/Model
/Type

I've parsed all the stuff, by putting everything in an array.

Now, I've a problem as I must use datas in "Detail" tag every time they are processed, but I must first process datas in Model, so I have to wait until Model tag is closed.

For now I put everything in an array called $datas (where I store the couple key/value). How can I do to create, at a certain point, a new array for
storing the Detail array (in order to have an array for every Detail that
may occur, as this isn't a fixed number of Details ?)

Thanks for helping.


Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are your
friend.

Garp
Jul 17 '05 #2

P: n/a
Just create a new array for an array data :

like this

$data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");

Savut

"Bob Bedford" <be******@YouKnowWhatToDoHerehotmail.com> wrote in message
news:40***********************@news.sunrise.ch...
I've this structure in a XML file:
Type
Model
Detail
Datas
Datas
Datas
Datas
/Detail
Detail
Datas
Datas
Datas
/Detail
/Model
/Type

I've parsed all the stuff, by putting everything in an array.

Now, I've a problem as I must use datas in "Detail" tag every time they
are
processed, but I must first process datas in Model, so I have to wait
until
Model tag is closed.

For now I put everything in an array called $datas (where I store the
couple
key/value). How can I do to create, at a certain point, a new array for
storing the Detail array (in order to have an array for every Detail that
may occur, as this isn't a fixed number of Details ?)

Thanks for helping.


Jul 17 '05 #3

P: n/a
> Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are your
friend.


It's what I've done until now. My message tell that I've already passed this
stage. I have already parsed the XML file (with the help of examples from
php.net). The problem comes while using arrays.

Thanks anyway for your help.
Jul 17 '05 #4

P: n/a
"Savut" <we***@hotmail.com> a écrit dans le message de
news:wB**************@news20.bellglobal.com...
Just create a new array for an array data :

like this

$data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");

Savut

Thanks, Savut, exactly the help I wanted !

One more question:
As you can guess, I'm not so confortable with arrays....so, how can I then
show all datas since sometimes Detail1 can be a value and other it can be an
array:

Like in pseudo-code
for every value of $data
if $data[value] is an array
for every Avalue of $data[value]
echo("$data[value][AValue][Key] -
$data[value][AValue][Value]<br>");
else
echo(echo("$data[value][Key] - $data[value][Value]<br>");

How can I do this ?

BoB
Jul 17 '05 #5

P: n/a
here come is_array($data[x][y]);

Savut
http://www.savut.com

"Bob Bedford" <be******@YouKnowWhatToDoHerehotmail.com> wrote in message
news:40***********************@news.sunrise.ch...
"Savut" <we***@hotmail.com> a écrit dans le message de
news:wB**************@news20.bellglobal.com...
Just create a new array for an array data :

like this

$data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");

Savut

Thanks, Savut, exactly the help I wanted !

One more question:
As you can guess, I'm not so confortable with arrays....so, how can I then
show all datas since sometimes Detail1 can be a value and other it can be
an
array:

Like in pseudo-code
for every value of $data
if $data[value] is an array
for every Avalue of $data[value]
echo("$data[value][AValue][Key] -
$data[value][AValue][Value]<br>");
else
echo(echo("$data[value][Key] - $data[value][Value]<br>");

How can I do this ?

BoB


Jul 17 '05 #6

P: n/a
Thanks Savut,

In fact I'm stuck before this. I'll tell you the tree and then maybe you can
help me:
Take back the example:
/**************************/
Type
SomeDatas
SomeDatas
Model (array)
Mod0Datas
Mod1Datas
Detail (array)
Datas
Datas
Datas
Datas
/Detail
Detail (array)
Datas
Datas
Datas
/Detail
/Model
Brand (array)
Brand0Datas
Brand1Datas
Detail (array)
Datas
Datas
/Detail
Detail (array)
Datas
Datas
Datas
/Detail
/Brand
/Type
/**************************/
So I create the $data array for storing everyting in Type.
function startElement($parser, $name, $attrs)
....
switch($name){
case 'Type':
$datas["TAG"] = $name;
break;
case 'Model': //or 'Brand'
$actualarray = $name;
$datas[$actualarray] = array(); //create an array for Model or Brand
elements
break;
case 'Datas':
$datasindex = count($datas[$actualarray]);
$datas[$actualarray][$datasindex] = $name;

here I'm stuck !!!!
- $datas is the "Mother" array, that stores everything
- when I encounter the word "Model" or "Brand" in the XML file, I've to
create an array (empty for now).
- Until I encounter the closing /Model or /Brand, I've to put everything in
the $datas["Model"] array as long as they are datas.
in the $datas["Model"] or $datas["Brand"] array, I've to create an other
array as soon as I encounter the word "Detail" for storing the Detail's
datas, and then fill the $datas["Model"]["Detail"] until I encounter the
/Detail word.

My question is: How do I create such arrays on the fly (look the
startElement for what I mean) and what should I keep for pointers...I've to
keep indexes, as I will create an array of array of array, and I will then
be able to walk trough each element. (this code would also be greately
appreciated...)
I've never worked with PHP arrays before, this is the first time I need
them, and it's quite difficult as a starting point !!!!

Thank you !!!!

Best regards.

BoB
Jul 17 '05 #7

P: n/a

"Bob Bedford" <be******@YouKnowWhatToDoHerehotmail.com> wrote in message
news:40***********************@news.sunrise.ch...
Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are your friend.
It's what I've done until now. My message tell that I've already passed

this stage. I have already parsed the XML file (with the help of examples from
php.net). The problem comes while using arrays.

Thanks anyway for your help.


Ah, I didn't know that's what you meant by parsed. Glad you got the help you
wanted in the end though.

Garp
Jul 17 '05 #8

P: n/a
Type
SomeDatas
SomeDatas
Model (array)
Mod0Datas
Mod1Datas
Detail (array)
Datas
Datas
Datas
Datas
/Detail
Detail (array)
Datas
Datas
Datas
/Detail
/Model
Brand (array)
Brand0Datas
Brand1Datas
Detail (array) --> $BrandDetail[0]
Datas
Datas
/Detail
Detail (array) --> $BrandDetail[1]
Datas
Datas
Datas
/Detail
/Brand
/Type
I suggest you to use xpath so you can navigate inside an xml document more
easily :
Here a simple preview of what he can do.
$domxml = new DomDocument("yourxmlfile.xml");
$xpath = new DomXPath($domxml);
$BrandDetail = $xpath->query("/Type/Brand/Detail");
foreach($BrandDetail as $value) {
$datas["Brand"]["Detail"][] = $value; //[] is used for auto increment
the array
}

But this work only on PHP5 as I know
here some exemple http://www.zend.com/php5/articles/php5-xmlphp.php

If not, then use domxml then navigate inside with has_child_nodes() and
node_name() and node_value() all with foreach() loop.
You can check this for exemple and tutorial, http://ca3.php.net/domxml.

This is very long but worth to learn it.

Hope this help

Savut
http://www.savut.com
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.