Saber
Saber

Reputation: 617

php get duplicate data once in array object

I have php array object and I need display once duplicate field_header in my code. I have html table and want disply once duplicate field_header in table thead and other data in td my array object data this is:

    Array
    (
     [filed_p_1] => stdClass Object
    (
        [field_id] => 1
        [field_table] => product
        [field_realname] => field one
        [field_namekey] => filed_p_1
        [field_header] => number_one
        [field_type] => text
    )

[filed_p_2] => stdClass Object
    (
        [field_id] => 2
        [field_table] => product
        [field_realname] => field two
        [field_namekey] => filed_p_2
        [field_header] => number_two
        [field_type] => text
    )

[filed_p_3] => stdClass Object
    (
        [field_id] => 3
        [field_table] => product
        [field_realname] => field three
        [field_namekey] => filed_p_3
        [field_header] => number_three
        [field_type] => text

    )

[filed_p_1_2] => stdClass Object
    (
        [field_id] => 19
        [field_table] => product
        [field_realname] => field four
        [field_namekey] => filed_p_1_2
        [field_header] => number_one
        [field_type] => text
    )

  )

I want display my result this format:

number_one
field one
field four

number_two
field two

number_three
field three

I'm using foreach loop and display data but my result this is:

  foreach ($fields as $fieldName => $oneExtraField) {

?> <thead><tr><th> <?php echo $oneExtraField->field_header; ?></th></tr></thead>
    <tr>
        <td>
           <?php echo $oneExtraField->field_realname ?>
        </td>
    </tr>
<?php

}

number_one
field one

number_two
field two

number_three
field three

number_one
field four

Upvotes: 0

Views: 240

Answers (1)

tbedner
tbedner

Reputation: 323

You can have a nested loop to get the data that you want. Here is what I did:

<?php
// Create array of objects
$fields = array
    (
     "filed_p_1" => (object) [
        "field_id" => 1,
        "field_table" => "product",
        "field_realname" => "field one",
        "field_namekey" => "filed_p_1",
        "field_header" => "number_one",
        "field_type" => "text"
    ],

"filed_p_2" => (object) [
        "field_id" => 2,
        "field_table" => "product",
        "field_realname" => "field two",
        "field_namekey" => "filed_p_2",
        "field_header" => "number_two",
        "field_type" => "text"
    ],

"filed_p_3" => (object) [
        "field_id" => 3,
        "field_table" => "product",
        "field_realname" => "field three",
        "field_namekey" => "filed_p_3",
        "field_header" => "number_three",
        "field_type" => "text"

    ],

"filed_p_1_2" => (object) [
        "field_id" => 19,
        "field_table" => "product",
        "field_realname" => "field four",
        "field_namekey" => "filed_p_1_2",
        "field_header" => "number_one",
        "field_type" => "text"
    ]

  );

  // Create array for headers
  $header_array = array();

  // Loop through each array object
  foreach ($fields as $key => $value) {
    // Set header value
    $header = $value->field_header;
    // Check if header value has already been used, proceed if not
    if(!in_array($header,$header_array)) {
      // Add header value to $header_array and echo header
      $header_array[] = $header;
      echo $header;
      echo "<br />";
      // Loop through $fields again and echo every field_realname with that header
      foreach ($fields as $fieldName => $oneExtraField) {
        if($header == $oneExtraField->field_header) {
          echo $oneExtraField->field_realname;
          echo "<br />";
        }
      }
      echo "<br />";
    }
  }
?>

This outputs:

number_one
field one
field four

number_two
field two

number_three
field three

Upvotes: 2

Related Questions