Jump to content

Simple Script help


m.jay.victor

Recommended Posts

<html>

<head></head>

<body>

My favourite bands are:

<ul>

 

<?php

 

// define arrays

$morebands = array('Desturbed', 'Anthrax');

$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands");

// loop over it

// print array elements

foreach ($artists as $a) {

    if ($a != 'Array'){

  echo '<li>'.$a;

}

  Else {

foreach ("${$a}" as $b){

echo '<li>'.$b;

}

}

}

 

?>

 

</ul>

</body>

</html>

 

I can not figure out why this will not work:(

I would like the foreach to run through the array as normal, but if it encounters a nested array, loop it as well.

 

I know this likely is not the right, or best way to do this, but I am just learning PHP through a tutorial and I learn best by doing... So I take the lessons, make them more complicated, then figure out how to make it happen (like so).

 

right now I am working on

http://devzone.zend.com/node/view/id/635

 

anyhow thanks for any help!

Link to comment
Share on other sites

Try the following...

<html>
<head></head>
<body>
My favourite bands are:
<ul>

<?php
  // define arrays
  $morebands = array('Desturbed', 'Anthrax');
  $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands);

  // echo each value
  foreach($artists AS $a) {
    if(gettype($a) == 'array') {
      foreach($a AS $b) {
        echo '<li>',$b;
      }
    } else {
      echo '<li>',$a;
    }
  }

?> 

</ul>
</body>
</html> 

 

Tell me how it goes bud :)

 

Regards, Paul.

Link to comment
Share on other sites

I'm wondering why you're putting the $morebands array as an array inside of the $artists array.  This seems more logical:

 

$morebands = array('Desturbed', 'Anthrax');
$artists = array_merge(array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'), $morebands);

foreach ($artists as $a) {
echo "<li>$a</li>";
}

Link to comment
Share on other sites

to learn how.

I started learning PHP about 2 days ago and I am following that tutorial I linked earlier. The next logical step for me was to see if I could nest the arrays.

 

Anyhow, I noticed part of what you fixed was the way I defined the artist array.

from

$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands");

to

$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands);

 

What i don't understand is why this was necessary. I know it was, because the code would output the word "array" on the last line if you did not remove the quotes.  Can you explain why this happened though?

Link to comment
Share on other sites

to learn how.

I started learning PHP about 2 days ago and I am following that tutorial I linked earlier. The next logical step for me was to see if I could nest the arrays.

 

Anyhow, I noticed part of what you fixed was the way I defined the artist array.

from

$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands");

to

$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands);

 

What i don't understand is why this was necessary. I know it was, because the code would output the word "array" on the last line if you did not remove the quotes.  Can you explain why this happened though?

 

Look more closely.  I used array_merge() to merge the two arrays.  Your way adds the $morebands array as a nested array into another element in the $artists array and looks like this:

 

Array
(
    [0] => Metallica
    [1] => Evanescence
    [2] => Linkin Park
    [3] => Guns n Roses
    [4] => Array
        (
            [0] => Desturbed
            [1] => Anthrax
        )

)

 

Mine looks like this:

 

Array
(
    [0] => Metallica
    [1] => Evanescence
    [2] => Linkin Park
    [3] => Guns n Roses
    [4] => Desturbed
    [5] => Anthrax
)

 

 

 

Link to comment
Share on other sites

Thank you Abra, I was however trying to extend my knowledge of foreach, and how I may nest arrays. Your way is by far functionally better of course, but I am just trying to extend what I got from that one lesson.

 

You did however teach me that it was possible to merge arrays though:)

Wich brings me to...

$artists = array_merge($artists, $morebands);

Would that have not been simpler?

Link to comment
Share on other sites

Thank you Abra, I was however trying to extend my knowledge of foreach, and how I may nest arrays. Your way is by far functionally better of course, but I am just trying to extend what I got from that one lesson.

 

You did however teach me that it was possible to merge arrays though:)

Wich brings me to...

$artists = array_merge($artists, $morebands);

Would that have not been simpler?

 

Oh O.K.  In that case you would use something like what PaulRyan posted.  I would however use in_array($a) instead of the gettype().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.