0

I am looking for a little assistance. Please have a look at the array below. In the array that I have, the same data has been attached to 3 items and a separate item has its own unique data.

Array
(
[0] => Array
    (
        [item] => 116
        [stockcode] => UPGRADE
        [qty] => 1
        [nett] => 35.3
        [vat] => 20
        [gross] => 42.36
    )
[1] => Array
    (
        [item] => 117
        [stockcode] => UPGRADE
        [qty] => 1
        [nett] => 35.3
        [vat] => 20
        [gross] => 42.36
    )
[6] => Array
    (
        [item] => 118
        [stockcode] => UPGRADE
        [qty] => 1
        [nett] => 35.3
        [vat] => 20
        [gross] => 42.36
    )
)

Array
(
[0] => Array
    (
        [item] => 086
        [stockcode] => INS VISIT
        [qty] => 1
        [nett] => 60
        [vat] => 0
        [gross] => 60
    )
)

In my code, I have the following to try and display the data:

foreach ($section['lines'] as $l) {

    //if (count($plots) > 1) :

    //echo count($l);

    if ($l['is_stockcode']) {
        $l['stockcode'] = '<a href="javascript:void(0);" class="stockcode-link" data-id="' . $l['stockcode'] . '" >' . $l['stockcode'] . '</a> ';
    }
    ?>
    <tr>
        <td class="text-right"><?php echo $l['qty']; ?></td>
        <td><?php echo $l['stockcode']; ?></td>                          
        <td class="text-right"><?php echo number_format($l['nett'], 2); ?></td>
        <td class="text-right"><?php echo $l['vat']; ?>%</td>
        <td class="text-right"><?php echo number_format($l['gross'], 2); ?></td>
    </tr>
    <?php
    $total_nett += $l['nett'];
    $total_gross += $l['gross'];
};

What I am trying to achieve is this, seeing as the data from the first array is identical, I only want it to be displayed once. I have done a php join to show the item ids in the format of "116, 117, 118". But rather than having the data repeated 3 times, I would like to have it shown once. Is that possible? I have tried using array_unique, but its stripping out a lot of the data itself. So any help would be appreciated.

Thanks

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
user1297396
  • 119
  • 1
  • 2
  • 10
  • In first array field item is not unique. Do you care about keeping this field? – Mateusz Nowak Sep 12 '14 at 13:28
  • See http://stackoverflow.com/questions/2442230/php-getting-unique-values-of-a-multidimensional-array – Mateusz Nowak Sep 12 '14 at 13:29
  • You should take a look at [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming). Can easily write a method to compare the values and remove duplicates if applicable. – MC Emperor Sep 12 '14 at 13:29
  • 1
    This is partly a data generation issue -- if you have numerous items of the same type, there should be a description of the object type (stockcode, quantity, gross, nett, vat) and then a list of the items of that type. Are you getting this data out of a database? A better DB query could eliminate this problem. – i alarmed alien Sep 12 '14 at 13:32
  • i agree with alien, a better query would be much okay, if not, you could group that batch by stockcode, so you'll only get one, and push inside the item ids, by they way, are these on a parent array, or each stockcode are separately declared? – Kevin Sep 12 '14 at 13:37

2 Answers2

0

You can filter them out first, before displaying. There is probably better solution, but first thought which came to my head is making json_encode on subarrays and then use array_unique and decode data. E.g.

    $data = array(
        array('a' => 1, 'b' => 'st', 'c' => 3),
        array('a' => 1, 'b' => 'st', 'c' => 3),
        array('a' => 3, 'b' => 'st', 'c' => 1),
        array('a' => 1, 'b' => 'st', 'c' => 3),
    );

    $tmpData = array();
    foreach ($data as $record) {
        $tmpData[] = json_encode($record);
    }

    $tmpData = array_unique($tmpData);

    $result = array();
    foreach ($tmpData as $record) {
        $result[] = json_decode($record, true);
    }
marian0
  • 3,336
  • 3
  • 27
  • 37
0

You could try using the hashes of the data sets as keys. Add something like this to your code before your foreach loop...

// array of unique results...
$unique_array = [];

// for earch set of data in your array...
foreach ($your_array as $your_array_data) {
    // remove ids - since those ARE unique...
    unset($your_array_data['item']);

    // create a unique hash sum of the data as the new array key
    // this hash sum will be the same for identical data sets
    $unique_array[md5(implode('', $your_array_data))] = $your_array_data;
}

See PHP's md5 and implode docs for more info.

chaseisabelle
  • 162
  • 2
  • 18