user440096
user440096

Reputation:

Debugging Arrays with echo (Php)

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

Upvotes: 1

Views: 122

Answers (5)

John
John

Reputation: 721

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

Upvotes: 1

Patrick Desjardins
Patrick Desjardins

Reputation: 140903

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)

Upvotes: 0

Brian Driscoll
Brian Driscoll

Reputation: 19635

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

Upvotes: 0

Andrew
Andrew

Reputation: 1357

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

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

Upvotes: 1

SW4
SW4

Reputation: 71170

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.

Upvotes: 2

Related Questions