Jump to content

Help getting XML data into an array.


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

 

I'm working on a project and I've run into a problem and I can't seem to figure out how to fix it or even what is wrong no matter how much I google.  I have data in an xml file that I need to read and use to create an array.

 

Here is the XML (food.xml):

 

   <?xml version="1.0" encoding="UTF-8"?>
      <Foods>
         <Food type='fruit' color='yellow'>banana</Food>
         <Food type='vegetable' color='green'>lettuce</Food>
         <Food type='meat' color='red'>beef</Food>
      </Foods>
 
Here is my code to work with the XML (xml_data.php)
<?php

$file = 'food.xml';
$xml = simplexml_load_file($file);
$food = array();

// Example variables
$type = 'grain';
$name = 'corn';

// Adds example variables to array
$food->key = $type;
$food[$type] = $name;

// Displays XML data and add to array
for($i=0;$i<sizeof($xml);$i++){
	
	// Assigns XML data to variables
	$type = $xml->Food[$i][type];
	$name = $xml->Food[$i];
	
	// Prints variables to see that XML data is actually read
	echo '(Inside for loop) '.$type.'...'.$name.'<br>';
	
	// Assigns variables to array
	$food->key = $type;
	$food[$type] = $name;
}

echo '<br>Size of array is '.sizeof($food).'<br><br>';

// Prints each entry in the array
foreach($food as $key=>$value){
	echo '(Inside foreach) Key is '.$key.'<br>';
	echo '.....and Value is '.$value.'<br>';
}
?>

The example variables when placed into the array work but when I use the same method to place the XML variables into the array it does not.

 

Why does this not work?  How can I get it to work?

 

I'm attaching files as well, just in case.  Both files are saved in the same directory.

 

Thanks for any help.

food.xml

xml_data.php

Link to comment
https://forums.phpfreaks.com/topic/290454-help-getting-xml-data-into-an-array/
Share on other sites

  • Solution

You need to change these lines 

	$type = $xml->Food[$i][type];
	$name = $xml->Food[$i];

so the values are returned as strings not objects

	$type = (string) $xml->Food[$i]['type'];
	$name = (string) $xml->Food[$i];

Also remove this from lines 12 & 25

$food->key = $type;

$food is an array not an object so you cant set properties to an array. Remove them it is not needed.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.