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
https://forums.phpfreaks.com/topic/216393-simple-script-help/
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124505
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124556
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124590
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124640
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124834
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
https://forums.phpfreaks.com/topic/216393-simple-script-help/#findComment-1124858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.