1

I have googled to no avail I am missing something so fundamental I can't see it.

I have the following array

$this->links = array (
  0 => 
  array (
    'url' => 'http://maps.doc.govt.nz/Viewer/Index.html?viewer=rwa',
    'text' => 'DOC maps: Discover the outdoors ',
  ),
  1 => 
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' ',
  ),
  2 => 
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' Book Great Walks, selected huts, campsites and more.  ',
  ),
  3 => 
  array (
    'url' => 'https://careers.doc.govt.nz/jobtools/jncustomsearch.searchResults?in_organid=18174&in_searchBut=Search',
    'text' => 'Jobs at DOC ',
  ),
  4 => 
  array (
    'url' => 'http://blog.doc.govt.nz/',
    'text' => ' Conservation blog ',
  )....

I am trying to loop through it with this

foreach ($this->links as $arrayId) 
            {
            print "Link {\n";
            foreach ($arrayId as $linktitle=>$linkdetail)
                {
                echo 'link: -'.$linktitle .'<br/> bit'.$linkdetail;
                }
            }

But I am getting a Warning: Invalid argument supplied for foreach() error. I really don't know why its not working, I have also tried every variation I can think of?? Still getting the same error?

UPDATE I dump the variable and $links is described as an array however when I add an is_array condition it returns false??????

        var_dump($links); 

- string 'array (
  0 => 
  array (
    'url' => 'http://www.iana.org/domains/example',
    'text' => 'More information... ',

if (is_array($links))
{
    echo "Its an array";

} - does not return 
3
  • $this is refer to object or class, where it is ? Commented Sep 20, 2013 at 5:38
  • $this is my class (a view). I have removed $this but still getting the same error Commented Sep 20, 2013 at 6:00
  • your variable $links is string not array Commented Sep 20, 2013 at 6:37

4 Answers 4

1

Your $this->links is a string. you need it to be an array. if you cannot change is to array where you are generating it you can try the following. I don't recommend the usage of eval(), and it must be used as a last resort only.

$alinks=array();
eval('$alinks='.$this->links.';');
foreach ($alinks as $arrayId) 
        {
        print "Link {\n";
        foreach ($arrayId as $linktitle=>$linkdetail)
            {
            echo 'link: -'.$linktitle .'<br/> bit'.$linkdetail;
            }
        }

Warning: The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

http://php.net/manual/en/function.eval.php

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

1 Comment

AAaaargh I using var_export to echo the dump and I completely forgot about it.
0

The code looks ok but it is possible that the code that generates the value for $this->links is doing so erroneously. Try doing a print_r or var_dump of $this->links just before the first foreach and then do the same for the $arrayId before the second foreach and it may show you what is causing the problem. The only thing I can think of is that $this->links is not actually an array because of something previously not generating the data for it or assigning it correctly.

1 Comment

Thanks but I can var_dump the array fine
0

Remove $this as egig has mentioned if you are not using a class and try the following:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);

$links = array (
  0 =>
  array (
    'url' => 'http://maps.doc.govt.nz/Viewer/Index.html?viewer=rwa',
    'text' => 'DOC maps: Discover the outdoors ',
  ),
  1 =>
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' ',
  ),
  2 =>
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' Book Great Walks, selected huts, campsites and more.  ',
  ),
  3 =>
  array (
    'url' => 'https://careers.doc.govt.nz/jobtools/jncustomsearch.searchResults?in_organid=18174&in_searchBut=Search',
    'text' => 'Jobs at DOC ',
  ),
  4 =>
  array (
    'url' => 'http://blog.doc.govt.nz/',
    'text' => ' Conservation blog ',
  )
);

foreach ($links as $arrayId)
{
        print "Link {\n";
        foreach ($arrayId as $linktitle=>$linkdetail)
        {
                echo 'link: -'.$linktitle .'<br/> bit'.$linkdetail;
        }
}
?>

2 Comments

Thanks but I have removed $this and added the error reporting but still getting the same error. I did a var_dump to get my print out of the array so it is rendering fine?
@redstubble: Can you please post the entire array?
0

try this

<?php
$this->links = array (
  0 => 
  array (
    'url' => 'http://maps.doc.govt.nz/Viewer/Index.html?viewer=rwa',
    'text' => 'DOC maps: Discover the outdoors ',
  ),
  1 => 
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' ',
  ),
  2 => 
  array (
    'url' => 'https://booking.doc.govt.nz/',
    'text' => ' Book Great Walks, selected huts, campsites and more.  ',
  ),
  3 => 
  array (
    'url' => 'https://careers.doc.govt.nz/jobtools/jncustomsearch.searchResults?in_organid=18174&in_searchBut=Search',
    'text' => 'Jobs at DOC '
  ));

echo "<pre>";

for($i=0;$i<=count($this->links)-1;$i++)
{
    foreach($this->links[$i] as $value)
    {
        print_r($value);
        echo "<br>";


    }
}
?>

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.