tags:

views:

80

answers:

2

I have an array like so:

Array
(
    [level] => Array
        (
            [0] => Array
                (
                    [lvlName] => name
                    [lvlEnt] => ent
                )

        )

    [title] => Array
        (
            [0] => Array
                (
                    [title] => a title
                    [titleDesc] => desc here
                )

        )

    [navBar] => Products
    [pageContent] => About
)

Can someone please tell me how to get the pageContent?

Here is the code that I am using but I am getting undefined indexes. I have no idea where I am going wrong.

foreach($bigArr as $key=>$val)
{
  if($key['pageContent'] != null)
  {
    foreach($val as $fkey=>$fval)
    {
      echo $fkey['pageContent'];
    }
  }
}

The loops give me:

PHP Notice:  Undefined index:  pageContent
PHP Warning:  Invalid argument supplied for foreach()

Can someone please give me a hand with this? Thanks

EDIT: The invalid argument is what has me perplexed.

+2  A: 

You can access the value with $bigArr['pageContent'].

Gumbo
I just figured it out. WHen I just looked at the way the array was set up I could clearly see it. Thanks buddy!
+2  A: 

Your first call to foreach is giving you two pieces of data per iteration: key and value. The key is a string, not an array (which is what you're attempting to use it as on line 3.

So on the last iteration for that array, $key would contain 'pageContent' and $val would contain 'About'

In any case, the use of foreach for what you're doing seems unnecessary. You can directly check whether the 'pageContent' key is present in the array using array_key_exists() and then pull the value straight out.

if(array_key_exists('pageContent', $bigArr) && $bigArr['pageContent'] != null)
{
    echo $bigArr['pageContent'];
}
Steven Richards
Hey Steven, thanks very much for the example and the help. I really need to expand my function list and array_key_exists is good to have. Thank you once again!Tom