0

I get some data in an associated array, see image for data,

enter image description here

Here I want to group all elements with party value. Each array may have different party name but arrays with a similar party to be grouped and displayed in the page.

What I tried is, Here I can successfully separate golaRate and rate, but I need specific party name for golaRate and rate

@foreach($data as $bill_data)
        @if(!$bill_data['golaRate'])
            {{-- @foreach($bill_data['party'] as $party) --}} --> this the problem here

@foreach($data as $bill_data)
        @if(!$bill_data['rate'])
            {{-- @foreach($bill_data['party'] as $party) --}}

am using laravel framework.

Mann
  • 576
  • 1
  • 9
  • 33

1 Answers1

1

I am not sure this is your requirement or not, hope this may help

<?php
$array = array(
    array(
        "lotname"=>"456",
        "quantity"=>"456",
        "rate"=>"5",
        "golarate"=>null,
        "userId"=>null,
        "party"=>"fds",
        ),  
    array(
        "lotname"=>"456",
        "quantity"=>"456",
        "rate"=>"5",
        "golarate"=>null,
        "userId"=>null,
        "party"=>"fds",
        ),  
    array(
        "lotname"=>"456",
        "quantity"=>"456",
        "rate"=>"5",
        "golarate"=>null,
        "userId"=>null,
        "party"=>"bbb",
        ),  
    array(
        "lotname"=>"456",
        "quantity"=>"456",
        "rate"=>"5",
        "golarate"=>null,
        "userId"=>null,
        "party"=>"bbb",
        ),
    ); 

function groupParty($data)
{
    $grouped = [];
    foreach ($data as $value) {
        $grouped[$value['party']][] = $value;
    }
    return $grouped;
}

print_r(groupParty($array));
?>
Ananthakrishnan Baji
  • 1,254
  • 1
  • 9
  • 21
  • yes am getting as needed but I need to loop through the results, and here array has a name like `Array ( [dgdfg] => Array ( [0] => Array ( [lotName] => ssf3 [quantity] => 12 [rate] => 56 [golaRate] => [userId] => [party] => dgdfg ) ` and I cont lop them like `$array[0]` like that. – Mann Sep 05 '17 at 07:09
  • Use foreach to loop u will get each party group in each loop – Ananthakrishnan Baji Sep 05 '17 at 08:12
  • Or you can use array_values(groupParty($data)) – Ananthakrishnan Baji Sep 05 '17 at 10:09
  • Yea I used `array_values`, but finding trouble in showing them in a table properly, extended to another question please look at it. https://stackoverflow.com/questions/46053820/adjusting-the-view-of-a-html-table-while-getting-data-through-a-forloop – Mann Sep 05 '17 at 11:27