Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:24:12+00:00 2026-05-23T03:24:12+00:00

I use PHP and Simple XML. I use a loop that does not work

  • 0

I use PHP and Simple XML.

I use a loop that does not work like expected:

foreach($item->Image->attributes()->source as $key => $value)
{
    echo $value;
}

In the foreach I try to tell that I want to get the “source” of the image which is listed in the attributes.

$item above is created with a loop around my code above foreach($xml_content->Section->Item as $item {}, (if you need to know where it came from)

My object looks like this:

object(SimpleXMLElement)#36 (4) {
    ["Text"]=>
        string(15) "Vinbergs socken"
    ["Description"]=>
        string(73) "Vinbergs socken ingick i Faurås härad och ligger i Falkenbergs kommun.
    "
    ["Url"]=>
        string(44) "http://sv.wikipedia.org/wiki/Vinbergs_socken"
    ["Image"]=>
         object(SimpleXMLElement)#38 (1) {
             ["@attributes"]=>
                  array(3) {
                      ["source"]=>
                            string(113) "http://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Faur%C3%A5s_Vinberg.svg/50px-Faur%C3%A5s_Vinberg.svg.png"
                      ["width"]=>
                          string(2) "50"
                      ["height"]=>
                            string(2) "41"
                      }
             }
     }

What is wrong with my loop in the beginning of my post?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:24:13+00:00Added an answer on May 23, 2026 at 3:24 am

    Your are trying to iterate a string, not an array

    $item->Image->attributes()->source
    

    To iterate all the attributes of the Image element, use

    foreach ($item->Image->attributes() as $attributeName => $attributeValue) {
    

    If you just want to output the value of the source attribute, do not iterate but use the shorthand

    echo $item->Image['source']
    

    See this demo and the SimpleXml Basic Usage Examples in the PHP Manual

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the php simple HTML DOM parser to grab the image,
Is it possible to use PHP's SimpleXML functions to create an XML object from
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
This question is simple. What function would I use in a PHP script to
I'd like to be able to use php search an array (or better yet,
My code does not pull the proper link for the item. Instead it's getting
I'm planning to use PHP for a simple requirement. I need to download a
I have some simple XML which I would like to parse into an array
(please excuse that I didn't use aliases). I would like my query output to
I would like to create a very simple PHP page for a site, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.