1

I've a week off work so I'm keeping busy trying to learn php.

I want to check the contents of my array by printing out whats contrained in them.

    foreach ($articleList as $item)
    {
        $this->articles[] = array(
                                    'url' => $this->domain . $item->getElementsByTagName('a')->item(0)->getAttribute('href'),
                'title' => $item->getElementsByTagName('a')->item(0)->nodeValue,
    //                              'author' => $item->getElementsByTagName('em')->item(0)->getElementsByTagName('a')->item(0)->nodeValue,
                'description' => $item->getElementsByTagName('div')->item(0)->nodeValue,
                'article' => ''
                                );
}

    foreach ($this->articles as $x)
    {
        echo $x;
    }

The code at the bottom is where I am trying to print out each piece of the array and see what contents it holds, So i can understand what the code above it is doing.

But its just blank even though the array size is 30.

How can I go about doing this?

Many Thanks, -Code

5 Answers 5

2

You can use print_r() to output an array to check its contents.

Try just doing:

print_r($this);

...instead of the second loop.

It also shows the position of items in an array.

Sign up to request clarification or add additional context in comments.

Comments

1

In your foreach loop, each element is itself an array.

Try print_r($x); instead of echo $x;

Comments

1

Quick and easy is just to do a var_dump. var_dump($x);

Comments

0

Each item in your articles array is also an array, so you're likely seeing Array when you echo out each one.

You will need to dive into each array in your articles array:

foreach ($this->articles as $x)
{
   print_r($x);
}

Comments

0
foreach ($articleList as $item)
{
    $this->articles[] = array(
                                'url' => $this->domain . $item->getElementsByTagName('a')->item(0)->getAttribute('href'),
            'title' => $item->getElementsByTagName('a')->item(0)->nodeValue,
//                              'author' => $item->getElementsByTagName('em')->item(0)->getElementsByTagName('a')->item(0)->nodeValue,
            'description' => $item->getElementsByTagName('div')->item(0)->nodeValue,
            'article' => ''
                            );
}

print_r($this->articles);

or

var_dump($this->articles)

Comments

Your Answer

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