0

My array which is dynamic, formed as

$exam = explode(',',$row['exam']);

For example result is:

$exam = array("First-Term","Second-Term", "Third-Term");

We can get index and value as this

foreach (array_values($exam) as $i => $value) {
  echo "$i: $valuen";
  echo "//And its mark";
}

But, how can I break loop to each index. I have to fetch as follow

First-Term
//And its mark

Second-Term
// And its mark

Third-Term
// And its mark

But while using foreach loop, I am getting

First-Term
Second-Term
Third-Term
//And its mark
//And its mark
//And its mark

How to break loop to each index, after that we use same code in every index. I'm simply try to assign each marks to each term

$exam[0]{
    //here is $value
    //rest of the code, same code to every index
}

$exam[1]{
    //here is $value
    //rest of the code, same code to every index
}

$exam[2]{
    //here is $value
    //rest of the code, same code to every index
}

$exam[...]{
    //
}
6
  • Read for loop php.net/manual/en/control-structures.for.php Commented Aug 31, 2018 at 6:18
  • Can you give a little more background to what you want the code to do? I don't understand why you would want to beak out of the loop at each index, if you want to execute the same piece of code for each item in the $exam array. Commented Aug 31, 2018 at 6:21
  • ...wait, do you mean that there is some code that is unique for each term, and some code that is the same for all terms? Commented Aug 31, 2018 at 6:24
  • @MarijnvanVliet, same code for every term. Please understand as this. I have to fetch First term -> Math, Second term-> Math, But I am getting First term Second Term -> Math Math Commented Aug 31, 2018 at 6:29
  • @AnandHmt, can you give some real example instead of // code,// here? From a first look, it just looks like echoing info with extra line breaks. Commented Aug 31, 2018 at 6:35

3 Answers 3

1

You should make two arrays for team and marks and then parse these two arrays in the same foreach loop like this

$team = array('a', 'b', 'c', 'd' );
$marks = array('1', '2', '3', '4' );

foreach(array_combine($team, $marks) as $t => $m) {
    echo $t . "<br>" .$m . "<br><br>"; //$t for team and m for marks
    echo "<br/>";
}

In this way you can parse your different data in a paralell way

Sign up to request clarification or add additional context in comments.

8 Comments

careful! if($i = 0) will assign 0 to $i. Did you mean if($i == 0)?
@MarijnvanVliet Ohh. Sorry, It was a typo error. I was in rush
@ZainFarooq, array is dynamic, so it is not possible to apply rule to each index. how to address this issue ?
But in question you made a static array
but now $i == $counter will always evaluate to TRUE. Why have the if-statement in the first place?
|
1

From the looks of what you are doing, you are using two foreach loops with different output...

This one:

$exam = array("First-Term","Second-Term", "Third-Term");

foreach (array_values($exam) as $i => $value) {
    echo "{$value}<br>";
    echo "// And its mark<br><br>";
}

Will output this:

First-Term
//And its mark

Second-Term
// And its mark

Third-Term
// And its mark

While this loop:

$exam = array("First-Term","Second-Term", "Third-Term");

foreach (array_values($exam) as $i => $value) {
    echo "{$value}<br>";
}
foreach (array_values($exam) as $i => $value) {
    echo "// And its mark<br>";
}

Will output this:

First-Term
Second-Term
Third-Term
// And its mark
// And its mark
// And its mark

Will update this answer once I have more details. Code tested: https://3v4l.org/RDvYN

Comments

-1

I would build an array with everything in place and then start to display it:

$exam = array("First-Term","Second-Term", "Third-Term");
$marks = array(1, 2, 3);

$result = array();
foreach (array_values($exam) as $idx => $value) {
    $result[] = array(
         "exam" => $value,
         "mark" => $marks[$idx]
    );
}

foreach ($result as $value) {
    echo $value['exam'] . "\n";
    echo $value['mark'] . "\n\n";
}

3 Comments

Click on "Run code snippet". This tool is designed for HTML/CSS/Javascript statements only.
OK, of course it won't work that way. Just run it on your machine with php.
If the code is not supposed to work in a code snippet, maybe it should not be in a code snippet, maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.