TransWikia.com

PHP why doesn´t the foreach function work?

Stack Overflow Asked by PaperPerson on February 25, 2021

Posting this again, because I am new and didn´t know how the questions should be formatted correctly.

Onto the question, I am supposed to do this, as in this shoud be written out in a window, not in the code.

This is the full code (the variables were for a different excercise essentialy):

 $all_pics = glob("pics/*.*");
      $all_pics_clear = str_replace(["pics/",".jpg",".png"],"",$all_pics);
      $all_pics_clear = str_replace(["_","-"], " ",$all_pics_clear);
    
   

     for($i = 0; $i < count($all_pics); $i++){
        echo "{ type: 'quiz', title: '"
                .$all_pics_clear[$i]
                ."', pic: '"
                .str_replace("pics-new/","",$all_pics[$i])
                ."' },<br>";
    }

Why doesn’t the foreach function work with this?

foreach ($all_pics as $filename) {
        echo "{ type: 'quiz', title: '"
            .$all_pics_clear
            ."', pic: '"
            .str_replace("pics-new/","",$all_pics)
            ."' },<br>";
    }

It kept telling me "array to string conversion error".

Notice: Array to string conversion in C:laragonwwwukol8ukolDukolD.php on line 38 { type: 'quiz', title: 'Array', pic: 'Array' },

And why the for function does work with this?

And because I am a beginner, I would appreciate an idiot-proof explanation, a lot of the programming concepts are just beyond me, I even had to look up the for function solution, because that would literally never occur to me to do.

edit: updated the code with the for loop, as that one works, the fact that glob returns arrays was helpful..i dont know the proper way around php programming and stackoverflow, so it might take me a while to wrap my head around the comments.cheers guys

edit2: im sorry, i really dont know how to post the array here, the array looks like this

  • so far i was able to use foreach loop only in this way, which obviously just writes out the value at the beginning but 20 times. how do i make it loop through all of the array items, and write all of them out?
 foreach ($all_pics_clear as $value) {
            echo "{ type: 'quiz', title: '"
                .$all_pics_clear[0]
                ."', pic: '"
                .str_replace("pics-new/","",$all_pics[0])
                ."' },<br>";
        }

One Answer

foreach ($all_pics as $filename) {

here $all_pics is the array, while $filename is the content of each item, which you want to use inside the loop.

You're referencing $all_pics and $all_pics_clear inside the loop. Both are arrays, that why you are getting that warning when trying to concatenate those with strings.

Also, if you want to get the key of an item inside the loop you may use ... as $key => $value construct:

foreach ($all_pics as $i => $filename) {
        echo "{ type: 'quiz', title: '"
            .$all_pics_clear[$i] // use $i-th item from $all_pics_clear array
            ."', pic: '"
            .str_replace("pics-new/","",$filename) // use $filename (which is the $i-th item from $all_pics)
            ."' },<br>";
    }

Correct answer by AterLux on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP