Howdy_McGee
Howdy_McGee

Reputation: 10635

PHP Multidimensional Arrays

I'm trying to make a universal script that adds keywords to my individual pages (since header is in an include file) so I am getting the end of the url (multi.php) and retrieving the desc etc. from it's array. For some reason instead of returning keywords or descriptions it instead just returns "m" . . . it's kind of random and has me scratching my head. Here's what I got

<html>
<head>
    <title>Multi-Demensional Array</title>
<?php

$path = pathinfo($_SERVER['PHP_SELF']);
$allyourbase = $path['basename'];

$pages = array
(
    "multi.php" => array
    (
        "keywords"  => "index, home, test, etc",
        "desc"      => "This is the INDEX page",
        "style"     => "index.css"
    ),

    "header.php" => array
    (
        "keywords"  => "showcase, movies, vidya, etc",
        "desc"      => "SHOWCASE page is where we view vidya.",
        "style"     => "showcase.css"
    )
);



?>
</head>
<body>

<?php 

foreach($pages as $key => $value)
{
    if($key == $allyourbase)
    {
        echo $key['desc'];
    }
}

?> 

</body>
</html>

Upvotes: 0

Views: 2018

Answers (4)

webjawns.com
webjawns.com

Reputation: 2300

Other people have provided some great solutions, but it's important that you understand exactly what is happening here, so you don't make the same mistake again. Pay careful attention to the comments, and you will be on your way to successful coding!

Here's what is happening:

foreach ($pages as $key => $value) {
    if ($key == $allyourbase) {
        // At this point: $key = 'multi.php'
        // Also: $value = array( ... );

        // Keep in mind: $key['desc'] = $key[0] = 'm';
        // You are grabbing the first letter of the 'multi.php' string.

        // When dealing with strings, PHP sees $key['desc'] as $key[0],
        // which is another way to grab the very first character of 'multi.php'

        echo $key['desc'];

        // You really want $pages[$key]['desc'], but below 
        // is a better way to do it, without the overhead of
        // the loop.
    }
}

If you kept the loop, which is really unnecessary, it would look like this:

foreach ($pages as $key => $value) {
    if ($key == $allyourbase) {
        echo $value['desc'];
    }
}

The best solution is to replace the loop with the following code:

if (isset($pages[$allyourbase])) {
    echo $pages[$allyourbase]['desc'];
} else {
    // error handling
}

Upvotes: 1

RobertPitt
RobertPitt

Reputation: 57258

The reason why this is happening is because in PHP if I had the following code:

$hello = 'world';

and I attempted to do the following:

echo $hello[0];

PHP Would treat the string as an array and return me whatever is in position 0, which would result in w, when your using a foreach your asking PHP to set the key of the array to $key, and it's value to $value.

you then echo echo $key['desc'];, as the value is a string, php sees it as an integer based index, so it will ignore your call for desc and then return the first index, if you were to change echo $key['desc'] to echo $value['desc'] which is a hash based array it will return the desired results.

You should just be able to do this:

if(isset($pages[$allyourbase]))
{
    echo $pages[$allyourbase]['desc'];
}

No need for the loop

Upvotes: 2

Nahydrin
Nahydrin

Reputation: 13507

If I'm reading this right, echo $key['desc']; should be echo $value['desc'];.

Upvotes: 0

genesis
genesis

Reputation: 50966

try

 echo $key['desc'];

replace with

echo $value['desc']; 

Upvotes: 1

Related Questions