Waqas Cheema
Waqas Cheema

Reputation: 27

XML Data into PHP Array

I am getting data from XML files that I need to make into an array in PHP. Can any one tell me how to fill PHP array when count is unknown?

function getFeed($feed_url) 
{
  $content = file_get_contents($feed_url);
  $x = new SimpleXmlElement($content);

  echo "<ul>";

  foreach($x->channel->item as $entry) 
  {
    echo "
    <li>
    <a href='$entry->link' title='$entry->title' target='_new'> " . $entry->title . "</a>
    </li>";
  }

  echo "</ul>";
}

Upvotes: 2

Views: 1810

Answers (5)

tobia.zanarella
tobia.zanarella

Reputation: 1266

I can see you print an HTML list while navigating through XML nodes. So if I got your question, you also need to fill an array (in the meanwhile) with the data taken from the XML, actually converting your XML into an array... is that what you want?

Then, you just need to declare an empty array before the foreach statement: $arr = array(); and then, inside your foreach, add new fields to the array this way: $arr[] = $entry; If you need to specify more fields, you can do something like that:

$arr[] = array(
    'field' => $entry->fieldvalue
);`

Hope this helps!

Upvotes: 0

Utku Yıldırım
Utku Yıldırım

Reputation: 2277

You can always use array_push() or basic array method. http://php.net/manual/en/function.array-push.php

Array Push

$list = array();
array_push($list,$element);
or
$list[] = $element;

Full Code

<?php
function getFeed($feed_url) {
  $content = file_get_contents($feed_url);
  $x = new SimpleXmlElement($content);
  $list = array();

  foreach($x->channel->item as $entry) {  
    array_push($list,$entry);
  }
  return $list;
}

$list = getFeed("url");
echo "<ul>";
foreach($list as $entry) {
  echo "
  <li>
    <a href='$entry->link' title='$entry->title' target='_new'> " . $entry->title . "</a>
  </li>";
}
echo "</ul>";


?>

Upvotes: 0

Ross
Ross

Reputation: 17987

function getFeed($feed_url) {
    $content = file_get_contents($feed_url);

    $x = new SimpleXmlElement($content);

    $count = 0;
    $data = array();

    foreach($x->channel->item as $entry) {
        $data[$count]['link'] = $entry->link;
        $data[$count]['title'] = $entry->title;
        $count++;        
    }

    return $data;
}

might be a better solution. You can then manipulate the data in a more flexible way; keeping the data from presentation separate. Simply loop through $data and output it as you require.

Upvotes: 0

BudwiseЯ
BudwiseЯ

Reputation: 1826

Hope this works for you:

$c = 0;
$entries = Array();
foreach($x->channel->item as $entry) {
   echo "
      <li>
      <a href='$entry->link' title='$entry->title' target='_new'> " . $entry->title . "  </a>
      </li>";
      $entries[$c] = $entry->title;
      $c++;
}

Upvotes: 0

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35156

Question: can any one tell me how to fill php array when count is unknown

Declare the array and add items to it like so or use array_push

$something = array();
$something[] = 'first item';
$something[] = 'second item';

Upvotes: 1

Related Questions