Change an array

I have

foreach($requestArray as $request) {
  echo "<tr>";
  echo "\r\n";
  echo "<td scope='row'><a href='show_request.php?id=".$request['request_id']."'>".$request['user']."</a></td>";
  echo "\r\n";
  echo "<td><a href='show_request.php?id=".$request['request_id']."'>".$request['item']."</a></td>";
  echo "\r\n";
  echo "</tr>";
}

which produces


<tr>
<td scope='row'><a href='show_request.php?id=1'>Maya</a></td>
<td><a href='show_request.php?id=1'>Pen</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=2'>Maya</a></td>
<td><a href='show_request.php?id=2'>Clear Tape</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=3'>Maya</a></td>
<td><a href='show_request.php?id=3'>Marker</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=7'>Maya</a></td>
<td><a href='show_request.php?id=7'>Scanner</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=4'>Kie</a></td>
<td><a href='show_request.php?id=4'>Printer</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=8'>John</a></td>
<td><a href='show_request.php?id=8'>Clear Tape</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=9'>John</a></td>
<td><a href='show_request.php?id=9'>Marker</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=11'>John</a></td>
<td><a href='show_request.php?id=11'>Standing Table</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=10'>Smith</a></td>
<td><a href='show_request.php?id=10'>Shredder</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=5'>Ron</a></td>
<td><a href='show_request.php?id=5'>Marker</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=6'>Ron</a></td>
<td><a href='show_request.php?id=6'>Thumbtack</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=12'>Lily</a></td>
<td><a href='show_request.php?id=12'>Paper Clip</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=13'>Lily</a></td>
<td><a href='show_request.php?id=13'>Notebook</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=14'>Lily</a></td>
<td><a href='show_request.php?id=14'>Standing Table</a></td>
</tr><tr>
<td scope='row'><a href='show_request.php?id=15'>Lily</a></td>
<td><a href='show_request.php?id=15'>Chair</a></td>
</tr>	

How can I group the users together (Maya has 4, John has 3, Ron has 2, and Lily with 6) and those people would have their items grouped

Your image and output say otherwise. I see
Maya: 4
Kie: 1
John: 3
Smith: 1
Ron: 2
Lily: 4
for a total of 15.

What does $requestArray look like? var_dump it.

The simplest way to do this is to index/pivot the data, using the ‘user’ value as the array index, then loop over the indexed data to produce the output.

heres the array

Array
(
    [0] => Array
        (
            [request_id] => 1
            [user] => Maya
            [item] => Pen
        )

    [1] => Array
        (
            [request_id] => 2
            [user] => Maya
            [item] => Clear Tape
        )

    [2] => Array
        (
            [request_id] => 3
            [user] => Maya
            [item] => Marker
        )

    [3] => Array
        (
            [request_id] => 7
            [user] => Maya
            [item] => Scanner
        )

    [4] => Array
        (
            [request_id] => 4
            [user] => Kie
            [item] => Printer
        )

    [5] => Array
        (
            [request_id] => 8
            [user] => John
            [item] => Clear Tape
        )

    [6] => Array
        (
            [request_id] => 9
            [user] => John
            [item] => Marker
        )

    [7] => Array
        (
            [request_id] => 11
            [user] => John
            [item] => Standing Table
        )

    [8] => Array
        (
            [request_id] => 10
            [user] => Smith
            [item] => Shredder
        )

    [9] => Array
        (
            [request_id] => 5
            [user] => Ron
            [item] => Marker
        )

    [10] => Array
        (
            [request_id] => 6
            [user] => Ron
            [item] => Thumbtack
        )

    [11] => Array
        (
            [request_id] => 12
            [user] => Lily
            [item] => Paper Clip
        )

    [12] => Array
        (
            [request_id] => 13
            [user] => Lily
            [item] => Notebook
        )

    [13] => Array
        (
            [request_id] => 14
            [user] => Lily
            [item] => Standing Table
        )

    [14] => Array
        (
            [request_id] => 15
            [user] => Lily
            [item] => Chair
        )

)

then do use something like

foreach($requestArray['user'] as user) {
//how do I check equality
}

There is no ‘user’ element at that level in the array.

See if the following suggests how you can produce the desired output -

$data = [];

// index/pivot the data using the 'user' value as the index
foreach($requestArray as $row)
{
	$data[$row['user']][] = $row;
}

// examine the result
echo '<pre>'; print_r($data); echo '</pre>';

ok, I see it now, thanks…

it produces

Array
(
    [Maya] => Array
        (
            [0] => Array
                (
                    [request_id] => 1
                    [user] => Maya
                    [item] => Pen
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 2
                    [user] => Maya
                    [item] => Clear Tape
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 3
                    [user] => Maya
                    [item] => Marker
                    [item_type] => 1
                )

            [3] => Array
                (
                    [request_id] => 7
                    [user] => Maya
                    [item] => Scanner
                    [item_type] => 2
                )

        )

    [Kie] => Array
        (
            [0] => Array
                (
                    [request_id] => 4
                    [user] => Kie
                    [item] => Printer
                    [item_type] => 2
                )

        )

    [John] => Array
        (
            [0] => Array
                (
                    [request_id] => 8
                    [user] => John
                    [item] => Clear Tape
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 9
                    [user] => John
                    [item] => Marker
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 11
                    [user] => John
                    [item] => Standing Table
                    [item_type] => 2
                )

        )

    [Smith] => Array
        (
            [0] => Array
                (
                    [request_id] => 10
                    [user] => Smith
                    [item] => Shredder
                    [item_type] => 2
                )

        )

    [Ron] => Array
        (
            [0] => Array
                (
                    [request_id] => 5
                    [user] => Ron
                    [item] => Marker
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 6
                    [user] => Ron
                    [item] => Thumbtack
                    [item_type] => 1
                )

        )

    [Lily] => Array
        (
            [0] => Array
                (
                    [request_id] => 12
                    [user] => Lily
                    [item] => Paper Clip
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 13
                    [user] => Lily
                    [item] => Notebook
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 14
                    [user] => Lily
                    [item] => Standing Table
                    [item_type] => 2
                )

            [3] => Array
                (
                    [request_id] => 15
                    [user] => Lily
                    [item] => Chair
                    [item_type] => 3
                )

        )

)

I’m trying to figure out how to loop through it and combining the users

foreach($data as $request) 
{
//test equality
    for(x=0;count($data);x++)
    {
         if($request[$x]['user'] = $request[($x+1)]['user']
         { 
              //keep track of number of requests made
             $count++;
          } else {
             $count=1;
          }
    }     
}

Take the array @mabismad gave you and haul it through

$counts = array_map(
    static fn ($a) => count($a),
    $originalArray,
);

Or, for PHP 8.1+

$counts = array_map(
    count(...),
    $originalArray,
);

What result are you trying to produce? There’s no need for any counting unless you want to display a count of something.

If you just want to produce your original output, grouped by user -

// produce the output
foreach($data as $user=>$rows)
{
	foreach($rows as $row)
	{
		echo "<tr>\r\n";
		echo "<td scope='row'><a href='show_request.php?id={$row['request_id']}'>$user</a></td>\r\n";
		echo "<td><a href='show_request.php?id={$row['request_id']}'>{$row['item']}</a></td>\r\n";
		echo "</tr>";
	}
}

this…

foreach($requestArray as $request) {
	switch($request['item_type']) {
		case '1':
		$type = "Office Supply";
		break;
		case '1':
		$type = "Equipment";
		break;
		case '3':
		$type = "Furniture";
		break;
		default:
		$type = "Unknown";
	}
  echo "<tr>";
  echo "\r\n";
  echo "<td scope='row'><a href='show_request.php?id=".$request['request_id']."'>".$request['user']."</a></td>";
  echo "\r\n";
  echo "<td><a href='show_request.php?id=".$request['request_id']."'>".$request['item']." (".$type.")</a></td>";
  echo "\r\n";                                                                                                                                            
  echo "</tr>";
}

produces

I want to include all the requested items by a user (Maya has 4) in a column, instead of a row.
The data count is 6 and breaks the my array into an array of arrays

A picture of what you don’t want, isn’t useful.

ok, my bad…
Heres the array

Array
(
    [Maya] => Array
        (
            [0] => Array
                (
                    [request_id] => 1
                    [user] => Maya
                    [item] => Pen
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 2
                    [user] => Maya
                    [item] => Clear Tape
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 3
                    [user] => Maya
                    [item] => Marker
                    [item_type] => 1
                )

            [3] => Array
                (
                    [request_id] => 7
                    [user] => Maya
                    [item] => Scanner
                    [item_type] => 2
                )

            [4] => Array
                (
                    [request_id] => 16
                    [user] => Maya
                    [item] => Pen
                    [item_type] => 1
                )

        )

    [Kie] => Array
        (
            [0] => Array
                (
                    [request_id] => 4
                    [user] => Kie
                    [item] => Printer
                    [item_type] => 2
                )

        )

    [John] => Array
        (
            [0] => Array
                (
                    [request_id] => 8
                    [user] => John
                    [item] => Clear Tape
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 9
                    [user] => John
                    [item] => Marker
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 11
                    [user] => John
                    [item] => Standing Table
                    [item_type] => 2
                )

        )

    [Smith] => Array
        (
            [0] => Array
                (
                    [request_id] => 10
                    [user] => Smith
                    [item] => Shredder
                    [item_type] => 2
                )

        )

    [Ron] => Array
        (
            [0] => Array
                (
                    [request_id] => 5
                    [user] => Ron
                    [item] => Marker
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 6
                    [user] => Ron
                    [item] => Thumbtack
                    [item_type] => 1
                )

        )

    [Lily] => Array
        (
            [0] => Array
                (
                    [request_id] => 12
                    [user] => Lily
                    [item] => Paper Clip
                    [item_type] => 1
                )

            [1] => Array
                (
                    [request_id] => 13
                    [user] => Lily
                    [item] => Notebook
                    [item_type] => 1
                )

            [2] => Array
                (
                    [request_id] => 14
                    [user] => Lily
                    [item] => Standing Table
                    [item_type] => 2
                )

            [3] => Array
                (
                    [request_id] => 15
                    [user] => Lily
                    [item] => Chair
                    [item_type] => 3
                )

        )

)

which seems to be an array of arrays
The index of the array is each of the 6 of the users.
Then each user has an array of items.
Im trying to show all requests in a table so that each user is on a row

foreach($data as $user => $request) 
{
echo "<tr>\r\n";
    for(x=0;count($data);x++)
    {
         if($request[$x]['user'] = $request[($x+1)]['user']
         { 
              
          } else {
             
          }
    }     
echo "</tr>\r\n";
}
foreach ($data AS $user => $requests) {
   echo "<tr><td>$user</td><td>".(implode(",",array_map(fn($x) => $x['item'],$requests)))."</td></tr>";
}

EDIT: Helps if i get my arrow function syntax right.

@m_hutley that’s just array_column :wink:

the array_map is analogous, yes.

array_map(fn($x) => $x['item'],$requests)
vs
array_column($requests,'item')

shrug

Perhaps if you show what you want for 2 users and a couple of items for each user?

That’s almost it…


with

<?php
foreach ($data AS $user => $requests) {
	
/* 	switch($requests['item_type']) {
		case '1':
		$type = "Office Supply";
		break;
		case '1':
		$type = "Equipment";
		break;
		case '3':
		$type = "Furniture";
		break;
		default:
		$type = "Unknown";
	}    */
	
   echo "<tr>\r\n";
   echo "<td><a href='edit_request.php?id=".$requests['request_id']."'><span class='icon-cog'></span></a></td>";
   echo "\r\n";
   echo "<td>$user</td>";
   echo "\r\n";
   echo "<td>".(implode(", ",array_map(fn($x) => $x['item'],$requests)))."</td>";
   echo "\r\n";
   echo "<td>".(implode(", ",array_map(fn($x) => $x['item_type'],$requests)))."</td>";
   echo "</tr>\r\n";
}
?>

Instead of those numbers on he right, I am trying to put the types of items. The switch statement was my attempt but it gave me an error

Well for starters, you’ve got 2 case 1’s in that switch.

Secondly, dont do a switch, just define them as an array.

$types = ['Unknown','Office Supply','Equipment','Furniture']; //Because arrays start at 0, not 1.
...
array_map(fn($x) => $types[$x['item_type']],$requests)))