0

I am trying to produce a HTML list grouped by "groupName" from the array below.

array (size=30)
  0 => 
    array (size=4)
      'groupOrder' => string '1' (length=1)
      'groupName' => string 'Class' (length=11)
      'txt' => string '............' (length=32)
      'ID' => string '6' (length=1)
  1 => 
    array (size=4)
      'groupOrder' => string '1' (length=1)
      'groupName' => string 'Size' (length=11)
      'txt' => string '..................' (length=34)
      'ID' => string '6' (length=1)
  2 => 
      ...

So I'd like produce something like this pseudo list:

  • groupName
    • txt
    • txt
    • txt
  • next GroupName
    • txt
    • txt ...

So my code looks like this

foreach ($datafeed as $val => $row) {
       if (($datafeed[$val]['groupName']) == next($datafeed[$val]['groupName'])) {
           //same group - do nothing
       } else {
            echo $datafeed[$val]['groupName'];
       }

       echo "<li>".$row['txt']. "</li>";
}

But I'm getting errors about "next() expects parameter 1 to be array, string given".

I've tried all sorts of different syntax, but I'm not making much progress. How do I compare two values in a nested array?

3 Answers 3

1

You misinterpreted the meaning of the function next(): you cannot query it for an array element. It manipulates the internal array pointer tied with the array itself, not with some its element. From the PHP documentation:

Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array. 

and see also a description of next.

Since the foreach loop crucially depends on the value of the array pointer, messing with the array pointer will ruin the loop: you probably see every second element in the loop, because at every iteration once next() is called by your foreach loop and then once by yourself.

The simplest thing for you is probably to use the old good for loop:

for ($i = 0; $i < length ($array); $i++)
{
    if ($i == 0 || $array[$i] != $array[$i - 1])
        echo "New group!\n";

    echo $array[$i];
}
Sign up to request clarification or add additional context in comments.

Comments

1

This doesn't answer your question directly, but you'd be better off restructuring your array so that they're grouped, and you don't need to perform any logic within the loop to check the groupName. Currently, you're relying on the next groupName to match the current groupName, but if they're not sequential, they won't be grouped.

You could do something like this:

$output = array();
foreach ($datafeed as $feed) {
    $output[$feed['groupName']][] = $feed;
}

Here's a demo

1 Comment

Good point, although in this case I take care of the grouping in the SQL Query that supplies the data.
0

You should not use next inside a foreach loop anyway, since you'll get conflicting array pointer movements. Simply store the last value:

$last = null;
foreach (...) {
    if ($last != $row['current']) {
        // new group
    }
    $last = $row['current'];
    ...
}

Comments

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.