0

I have very little experience with PHP, but I'm taking a class that has PHP review exercises. One of them is to create a function that uses a loop to return all values of an array except the first value in an unordered list. I'm assuming there's a way to do this using a foreach loop but cannot figure out how. This is what I had but I feel like I am far off:

<?php    
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');

$myName = $array['myName'];
$favColor = $array['favColor'];
$favMovie = $array['favMovie'];
$favBook = $array['favBook'];
$favWeb = $array['favWeb'];

echo '<h1>' . $myName . '</h1>';

function my_function() {
foreach($array == $myName){
   echo '<ul>'
   . '<li>' . $favColor . '</li>'
   . '<li>' . $favMovie . '</li>'
   . '<li>' . $favBook . '</li>'
   . '<li>' . $favWeb . '</li>'

   . '</ul>';
 }
}

my_function();
 ?>
2
  • could you post your error/output ? Commented Jan 21, 2018 at 6:41
  • syntax error, unexpected ')' Commented Jan 21, 2018 at 6:43

2 Answers 2

1

The correct syntax of foreach is

foreach (array_expression as $key => $value)

instead of

foreach($array == $myName){

function that uses a loop to return all values of an array except the first value

I'm not sure, what exactly you mean by except the first value. If you are trying to remove first element from the array. Then you could have used array_shift

If you are supposed to use loop then

$count = 0;
foreach ($array as $key => $value)
{
   if ($count!=0)
   {
     // your code
   } 
   $count++;
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is not really answer of this question. Yes this is one of the problem, but not an answer.
@Pedram I'm yet to update answer. Also, please note, I answered as per currently available info.
Also mention this, can't use $array inside a function without global
@Pedram I have no objection, if you update my answer until, it is valid. ;-)
This can't be an edit, it just a note, I would not prefer to edit people's answer for such reason :) It's kinda reply.
|
0

Change the code to following

<?php    
      $array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');

$myName = $array['myName'];

echo '<h1>' . $myName . '</h1>';

 function my_function($array)
    {
       $count = 0;
       echo "<ul>";
       foreach($array as $key => $value)
       {
           if($key != "myName")
           {
               echo "<li>".$value."</li>";
           }

        }
        echo "</ul>"; 



    }    


my_function($array);

1 Comment

Thanks a lot! This definitely helped. I ended up doing the function/loop part a little differently: function myFunction($array) { foreach ($array as $key => $value) { if($key != "name") { echo "<li>" . $value . "</li>";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.