2

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>";
}
1
  • That looks about right. Doesn't that work? (Try to use double-quotes rather than single-quotes for your tag attributes, but that's another thing). Commented Nov 17, 2011 at 10:27

5 Answers 5

1

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';
Sign up to request clarification or add additional context in comments.

Comments

0

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++;
}

Comments

0
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.

Comments

0

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>";


?>

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.