Dows
Dows

Reputation: 57

Combine four foreach into one foreach?

I am trying to add 4 foreach into one. I know how to add 2 foreach into one like this :

foreach (array_combine($images, $covers) as $image => $cover) { 

But i want to add more two foreach $titles as $title and $albums as $album. I am not sure want like this :

foreach (array_combine($images, $covers) as $image => $cover) {
 foreach (array_combine($titles, $albums) as $title => $album) {
                               echo "$image-$cover-$title-$album"

It show me duplicate of every output.I mean output is

demo-demo1-demo2-demo3demo-demo1-demo2-demo3

Need output only

demo-demo1-demo2-demo3

Upvotes: 0

Views: 81

Answers (2)

Nathan Robb
Nathan Robb

Reputation: 193

I think you are approaching this problem from the wrong angle. From what I can tell, you are trying to output the properties of something. I think what you want to do is approach this from an object-oriented approach by creating a class and using a method to output the contents of your object.

Something like this:

class MyAlbumThing {
    protected $image;
    protected $cover;
    protected $title;
    protected $album;

    public __construct($image, $cover, $title, $album) {
        $this->image = $image;
        $this->cover = $cover;
        $this->title = $title;
        $this->album = $album;
    }

    public getImage() {
        return $this->image;
    }

    public getCover() {
        return $this->cover;
    }

    public getTitle() {
        return $this->title;
    }

    public getAlbum() {
        return $this->album;
    }

    public getString() {
        return $this->image . '-' . 
            $this->cover . '-' . 
            $this->title . '-' . 
            $this->album;
    }
}

Then, you can instantiate this class and print your properties:

MyAlbumThing album = new MyAlbumThing("demo", "demo1", "demo2", "demo3");
echo $album->getString();

Would output:

demo-demo1-demo2-demo3

Also, if you hav a lot of these things, then you would use a foreach, like so:

$myAlbumArray = new array();
$myAlbumArray[] = new MyAlbumThing("demo", "demo1", "demo2", "demo3");
$myAlbumArray[] = new MyAlbumThing("anotherdemo", "anotherdemo1", "anotherdemo2", "anotherdemo3");

$lengthOfArray = sizeof(myAlbumArray);
for ($i = 0; $i < $lengthOfArray; $i++) {
    echo $myAlbumArray[$i]->getString();
}

Sorry for any errors in my syntax, I wrote that in the browser without the help of my IDE.

I highly recommend learning more about object-oriented PHP programming. I found this article especially helpful while I was learning: http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762

EDIT: Please mark this answer as correct if you did indeed find this helpful for your problem.

Upvotes: 1

rardoz
rardoz

Reputation: 122

Put the for each statement in a function. Then create a loop that calls it.

public function loopMe($images, $covers)
{
   foreach (array_combine($images, $covers) as $image => $cover) { 
      $this->loopMe($image,$cover); 
   }
}

It looks like your second for loop is being called multiple times per item in the first for loop. So you want to make sure that you are only calling the second for loop once per image cover. or set an index cap on the second for loop. For instance if you are tyring to map the first item in the first for loop to the first item in the second for loop you should use an index.

public function loopMe($images)
{

   for ($i = 0; $i < count($images); $i++) { 
      echo $images[$i] . '-'. $title[$i] . '-' . $cover[$i] . '-'. $album[$i];
   }
}

Upvotes: 1

Related Questions