0

$result is an array that looks like this:

Array ( 
  [0] => stdClass Object (
    [Key_1] => a 
    [Key_2] => 10 
  )
  [1] => stdClass Object (
    [Key_1] => b
    [Key_2] => 10 
  ) 
  [2] => stdClass Object ( 
    [Key_1] => c
    [Key_2] => 20
   ) 
)

How can I echo $result in a foreach loop grouped by [Key_2] wrapped in a div like

<div class="new_Key_2">
  Key_2: 10
  ------------
  Key_1: a
  Key_1: b
</div>

<div class="new_Key_2">
  Key_2: 20
  ------------
  Key_1: c
</div>

I know how to open the div by checking if [Key_2] has changed, but not how to close it before a new [Key_2] comes along.

4 Answers 4

3

PHP code that you need, you just need to play with it to match your HTML output needs.

<?php

$result = array();
foreach ($array as $object)
{
    $result[$object->key_2][] = $object->key_1;
}

foreach ($result as $key_2 => $keys)
{
    echo '<h1>'.$key_2.'</h1>';
    echo '<p>';
    echo implode('<br>', $keys);
    echo '</p>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can Group your array with array_reduce

$stdA = new stdClass();
$stdA->Key_1 = "a";
$stdA->Key_2 = 10;

$stdB = new stdClass();
$stdB->Key_1 = "b";
$stdB->Key_2 = 10;

$stdC = new stdClass();
$stdC->Key_1 = "a";
$stdC->Key_2 = 20;


# Rebuilding your array
$array = Array("0" => $stdA,"1" => $stdB,"2" => $stdC);


# Group Array
$array = array_reduce($array, function ($a, $b) {$a[$b->Key_2][] = $b;return $a;});

#Output Array
foreach ( $array as $key => $value ) {
    echo '<div class="new_Key_2">';
    echo "<h3> Key_2 : $key </h3>";
    foreach ( $value as $object ) 
        echo "<p>Key_1 : $object->Key_1</p>";
    echo '</div>';
}

Output

<div class="new_Key_2">
    <h3>Key_2 : 10</h3>
    <p>Key_1 : a</p>
    <p>Key_1 : b</p>
</div>
<div class="new_Key_2">
    <h3>Key_2 : 20</h3>
    <p>Key_1 : a</p>
</div>

4 Comments

Parse error: syntax error, unexpected T_FUNCTION in line $array = array_reduce($array, ...
What version of PHP are you using ??? See Live Demo : codepad.viper-7.com/xGiSXR
Hi, I don't understand why but with your code I receive an error "Parse error>: syntax error, unexpected T_FUNCTION in ..." relatively to the array_reduce function.
@Pennywise83 what version of PHP are you using ?
0

Assuming that your initial array is $my_array

// Generating a new array with the groupped results
$new_array = array();
foreach ($my_array as $element) 
{
    $new_array[$element->Key_2][] = $element->Key_1;
}

And then in your view layer you can echo each div/element in turn

<?php foreach ($new_array as $key => $items) { ?>

<div class="new_Key_2">
    Key_2 : <?php echo $key; ?><br />
    ---------------------------<br />
    <?php echo implode('<br />', $items); ?>
</div>

<?php } ?>

Comments

0

Just loop through the array of objects, and hold a separate group variable to the last group. No need to loop the array twice and generate a new array.

$group = false;
foreach($array as $object) {

  if($group !== false)
     echo '</div>';

  if($group != $object->Key_2) {
    echo '<div class="new_key_2">';
  }
  $group = $object->Key_2;
  // do stuff
}

if($group !== false)
  echo '</div>';

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.