0

I have multidimensional array like below.

Array
(
    [0] => stdClass Object
        (
            [LOCATIONNAME] => test warehouse
            [CATEGORYNAME] => Fruites
            [PRODUCTNAME] => 1 - apple
            [UNITS] => 8
        )

    [1] => stdClass Object
        (
            [LOCATIONNAME] => General
            [CATEGORYNAME] => Fruites
            [PRODUCTNAME] => 10003 - Grapes
            [UNITS] => 7
        )

    [2] => stdClass Object
        (
            [LOCATIONNAME] => test warehouse
            [CATEGORYNAME] => Fruites
            [PRODUCTNAME] => 10003 - Grapes
            [UNITS] => 12
        )

    [3] => stdClass Object
        (
            [LOCATIONNAME] => General
            [CATEGORYNAME] => Standard
            [PRODUCTNAME] => 10001 - Chicken Burger
            [UNITS] => 12
        )

    [4] => stdClass Object
        (
            [LOCATIONNAME] => test warehouse
            [CATEGORYNAME] => Standard
            [PRODUCTNAME] => 10001 - Chicken Burger
            [UNITS] => 17
        )

    [5] => stdClass Object
        (
            [LOCATIONNAME] => General
            [CATEGORYNAME] => Standard
            [PRODUCTNAME] => 10002 - Sandwitch
            [UNITS] => 5
        )
)

I want to group the above array with the below array elements;

Array
(
    [0] => CATEGORYNAME
    [1] => PRODUCTNAME
)

I want group the first array with the second array elements and summarize that( eg. sum each warehouse's quantity).I did this already but i cannot summarize the elements by the category

I want to print this array as below click to view

Is it possible to group an array with another array elements.It would be great if you can help me.

  • 1
    Can you let us know as to what format of array you are expecting ? the screenshot depicts only the UI but can you let us know as to how do you want your final array to look like ? – Kishen Nagaraju Jun 14 '18 at 08:58
  • Can you also let us known what you have tried so far. – Nigel Ren Jun 14 '18 at 09:01
  • @KishenNagaraju Thanks for the reply... I need to print it in a table format.I would like to know the code to integrate this into a table. – John Mathew Jun 14 '18 at 09:03
  • i have tried a code and it works but can;t get the total quantity of the items in each category – John Mathew Jun 14 '18 at 09:04
  • Possible duplicate of [Group a multidimensional array by a particular value?](https://stackoverflow.com/questions/2189626/group-a-multidimensional-array-by-a-particular-value) – Nigel Ren Jun 14 '18 at 09:10
  • @NigelRen I saw this post.But it is queit different – John Mathew Jun 14 '18 at 09:12

1 Answers1

0

Ok. By your requirement I am assuming that you want it in the below format:

$result = array(
    'Standard' => array(
        '10001 - Chicken Burger' => array(
            array(
                'LOCATIONNAME' => 'General',
                'UNITS' => 5
            ),
            array(
                'LOCATIONNAME' => 'test warehouse',
                'UNITS' => 17
            ),
        ),
        '10002 - Sandwitch' => array(
            array(
                'LOCATIONNAME' => 'General',
                'UNITS' => 5
            )
        )
    )
);

For this the code would be:

$result = array();
foreach ($objects as $obj) {
    if (!is_array($result[$obj->CATEGORYNAME])) {
        $result[$obj->CATEGORYNAME] = array();
    }

    if (!is_array($result[$obj->CATEGORYNAME][$obj->PRODUCTNAME])) {
        $result[$obj->CATEGORYNAME][$obj->PRODUCTNAME] = array();
    }

    $temp = array(
        'LOCATIONNAME' => $obj->LOCATIONNAME,
        'UNITS' => $obj->UNITS
    );

    array_push($result[$obj->CATEGORYNAME][$obj->PRODUCTNAME], $temp);
}

So Based on the above output you can print the table as follows:

<?php

$result = array(
    'Standard' => array(
        '10001 - Chicken Burger' => array(
            array(
                'LOCATIONNAME' => 'General',
                'UNITS' => 5
            ),
            array(
                'LOCATIONNAME' => 'test warehouse',
                'UNITS' => 17
            ),
        ),
        '10002 - Sandwitch' => array(
            array(
                'LOCATIONNAME' => 'General',
                'UNITS' => 5
            )
        )
    )
);

?>

<html>
    <head>
        <title>Test Table</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> 
    </script>
</head>

<body>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Location</th>
                <th>Units</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($result as $category => $catDetails) { ?>
            <tr>
                <td colspan="2"><?php echo $category; ?></td>
            </tr>
            <?php foreach ($catDetails as $product => $items) { ?>
            <tr>
                <td colspan="2"><?php echo $product; ?></td>
            </tr>
            <?php foreach ($items as $item) { ?>
            <tr>
                <td><?php echo $item['LOCATIONNAME']; ?></td>
                <td><?php echo $item['UNITS']; ?></td>
            </tr>
            <?php } } } ?>
        </tbody>
    </table>
</body>

Hope this helps.

Kishen Nagaraju
  • 2,062
  • 9
  • 17
  • thank you for the reply. I would like to group the array by the elements of the second array. – John Mathew Jun 14 '18 at 09:20
  • Are you saying that if you have 2 elements in the second array then you will be expecting 2 resultant group arrays ? – Kishen Nagaraju Jun 14 '18 at 09:22
  • no. my problem is whatever the second array elements ,i want group the first array with the second array elements and summarize that( means sum of quantity).I did this already but i cannot summarize the elements in the category – John Mathew Jun 14 '18 at 09:27
  • Yes. I understand that you want to group the first array by using the elements of second array. But you have to give a sample the array you are expecting. Can you give me the output array format based on the above requirement as I am not getting the grouping using the elements of second array part. – Kishen Nagaraju Jun 14 '18 at 09:32
  • I think it is not possible to set it to one single array. Could you please try to print this array in table. – John Mathew Jun 14 '18 at 09:35
  • Ok. Please give me some time. will update the answer with the printing of table. – Kishen Nagaraju Jun 14 '18 at 09:43
  • That would be great.:) – John Mathew Jun 14 '18 at 09:45
  • Edited the answer to include table printing. – Kishen Nagaraju Jun 14 '18 at 10:07