Jump to content

Recommended Posts

I need help writing a basic foreach loop. Say I have this data:

(
    [file1.ext] => Array
        (
            [0] => element0
            [1] => element1
            [2] => element2
            [3] => element3
        )

    [file2.ext] => Array
        (
            [0] => element0
            [1] => element1
            [2] => element2
            [3] => element3
        )

    [file3.ext] => Array
        (
            [0] => element0
            [1] => element1
            [2] => element2
            [3] => element3
        )
)

I want to write a foreach loop so that the output will be:

file1.ext element1 element2 element3
file2.ext element1 element2 element3
file3.ext element1 element2 element3

 

Thanks for any help!

 

Link to comment
https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/
Share on other sites

noticing the extra ( ) around the outside of the other print_r's you probably have a multidimensional array..

 

so you'd do like

 

// loop through indexes of $mainArray and fill KEY to $k and VALUE to $v
foreach ($mainArray as $k => $v) {
  // output the KEY
  echo $k;
  // loop through the value of $v, which would be an array in your example.
  // $va will now hold the content of each element.
  foreach ($v as $va) {
    // echo out $va with a prepended space to keep them from attaching to
    // eachother.
    echo ' '.$va;
  }
  // echo new line after the values loop, and try to find the next index of our array
  echo "\n";
}

 

but note there is simpler ways of achieving this

 

foreach ($mainArray as $k => $v) {
  echo $k.implode(" ",$v)."\n";
}

 

Nice. Thank you for your quick response. I just plugged in the implode bit that you posted there but it did the same thing that my current foreach loop is doing. The output looks like this (I'm substituting integers to represent the key and value for the entire array for each file to keep it simple:

1
12
123
1234
12345
etc...

Kind of like a pyramid. I want the output to be:

1
2
3
4
5
etc...

I'm sure that there's an easy fix for this. I'm probably just making a common mistake.

 

Here's a small chunk of the output when I do a print_r on the array that I'm working with...

Array
(
    [black_Crowes-Thorn_in_My_Pride-The_Southern_Harmony_and_Musical_Companion.ptb] => Array
        (
            [0] => ptab
            [1] => Thorn in My Pride
            [2] => The Black Crowes
            [3] => The Southern Harmony And Musical Companion
        )

    [Guns_N_Roses-November_Rain-Use_Your_Illusion_I.ptb] => Array
        (
            [0] => ptab
            [1] => November Rain Solo
            [2] => Guns N' Roses
            [3] => Use Your Illusion I
        )

    [Guns_N_Roses-Sweet_Child_O_Mine_Solo-Appetite_for_Distruction.ptb] => Array
        (
            [0] => ptab
            [1] => Sweet Child O' Mine Solo
            [2] => Guns N' Roses
            [3] => Appetite For Distruction
        )
)

I just tried your first example and I got the same pyramid output. There must be something else somewhere in the script that's causing this. What I don't get is when I do the print_r on the array the output is normal but when I try to loop it with a foreach I get that pyramid output.

http://lovinglori.com/myjpg.JPG

 

code used:

 

<?php
$arr = array();
$arr['hey'] = array("omg1","lmao1","lol1");
$arr['howdy'] = array("omg2","lmao2","lol2");
foreach ($arr as $k => $v) {
	echo $k;
	foreach ($v as $va) {
		echo " ".$va;
	}
	echo "\n";
}
?>

Oh my God... I found my mistake. I was placing the loop before a closing brace "}" in the script. I moved it outside of the brace and the problem was resolved. That's all it was.

 

I'd like to think that you helped me get there though. And you showed me a different way to bust a foreach loop. Thanks again!

 

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.