Group rows in a 2d array and count number of rows in each respective group

Just iterate over the array and use another array for the groups. It should be fast enough and is probably faster than the overhead involved when using sqlite or similar. $groups = array(); foreach ($data as $item) { $key = $item[‘key_to_group’]; if (!isset($groups[$key])) { $groups[$key] = array( ‘items’ => array($item), ‘count’ => 1, ); } … Read more

Filter rows of a 2d array by the rows in another 2d array

Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same. http://php.net/manual/en/function.array-diff-assoc.php The (string) value of any array is “Array”. Thus, your call to array_diff_assoc is effectively comparing these two things: Array … Read more

Merge 2d array rows with same column value and sum another column [duplicate]

Use function array_reduce() to combine the items having the same city: $input = array( array(‘city’ => ‘NewYork’, ‘cash’ => ‘1000’), array(‘city’ => ‘Philadelphia’, ‘cash’ => ‘2300’), array(‘city’ => ‘NewYork’, ‘cash’ => ‘2000’), ); $output = array_reduce( // Process the input list $input, // Add each $item from $input to $carry (partial results) function (array $carry, … Read more

How can I malloc a struct array inside a function? Code works otherwise

Function arguments are passed by value in C and modifying arguments in callee won’t affect caller’s local variables. Use pointers to modify caller’s local variables. void CreateMap(MapTileData ***Map, int xSize, int ySize) { //Variables int i; //Allocate Initial Space *Map = calloc(xSize, sizeof(MapTileData)); for(i = 0; i < xSize; i++) { (*Map)[i] = calloc(ySize, sizeof(MapTileData)); … Read more

Display and count qualifying data from a 2d array

PHP has no support for a SQL where sort of thing, especially not with an array of arrays. But you can do the counting your own while you iterate over the data: $count = array(); foreach($fruit as $one) { @$count[$one[‘color’]]++; } printf(“You have %d green fruit(s).\n”, $count[‘green’]); The alternative is to write yourself some little … Read more