LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2010, 11:49 AM   #1
gatorpower
LQ Newbie
 
Registered: Feb 2009
Posts: 15

Rep: Reputation: 0
(PHP, XML) need help sorting through a multi-dimensional array


Hello,

I am parsing through XML documents with a PHP script and creating an array because, as far as I know, I can manipulate arrays better than the features DOM gives me. That part is not as important, though.

The array it produces though is multi-dimensional, but somewhat inconsistent.

If I have the following XML:

PHP Code:
<domains>
   <
domain>
      <
title>google.com</title>
      <
bookmarks>
         <
bookmark>maps.google.com</bookmark>
         <
bookmark>news.google.com</bookmark>
         <
bookmark>mail.google.com</bookmark>
      </
bookmarks>
   </
domain>
   <
domain>
      <
title>bankaccount.com</title>
      <
bookmarks>
         <
bookmark>login.bankaccount.com</bookmark>
      </
bookmarks>
   </
domain>
</
domains
To call the first bookmark from the first domain, it would be:

PHP Code:
$array['domains']['domain'][0]['bookmarks']['bookmark'][0
To call the first bookmark from the second domain, it would be:

PHP Code:
$array['domains']['domain'][1]['bookmarks']['bookmark'
The problem is, I want to simply call bookmarks through a simply 'foreach()' or 'for()' function and the lack of consistency might be a problem, but the fact that some levels have a '0' array key and others do not is causing a problem.
 
Old 09-17-2010, 12:34 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,450

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

I don't like the DOM XML parser either. But take a look at SimpleXML:

http://www.php.net/manual/en/simplex...ples-basic.php

To iterate the bookmarks, you can do something like this:
PHP Code:
$xml = new SimpleXMLElement($xmlString);
foreach (
$xml->xpath('//bookmark') as $bookmark) {
  echo 
$bookmark."\n";

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi dimensional > 3 array in Python disruptive Programming 2 07-28-2010 12:24 PM
php Sort a Multi Dimensional Array action_owl Programming 3 04-30-2010 03:04 PM
Two dimensional array som_kurian Programming 2 01-01-2008 03:27 AM
two dimensional array sorting in C/C++ George2 Programming 10 11-19-2006 08:29 PM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 02:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration