I am trying to wrangle data into a desired format. Basically, I have a list in which a subset of the entities can have multiple values and need to be grouped and iterated over within the list.
Given the following example:
$entities = [
0 => ['name' => 'a', 'type' => 'single'],
1 => ['name' => 'b', 'type' => 'single'],
2 => ['name' => 'c', 'type' => 'grouped'],
3 => ['name' => 'd', 'type' => 'grouped'],
4 => ['name' => 'e', 'type' => 'single'],
];
foreach ($entities as $entityKey => $entity) {
if ($entity['type'] == 'single') {
$array[] = ['name' => $entity['name']];
}
if ($entity['type'] == 'grouped') {
// arbitrary number of items
$items = [0 => 'apple', 1 => 'orange', 2 => 'banana'];
foreach ($items as $itemKey => $item) {
$array[] = ['name' => $entity['name']];
}
}
}
print_r($array);
How can I produce output: a, b, c, d, c, d, c, d, e, such that the entities of type 'grouped' are paired and iterated over before he next entity is inserted into the array. Specifically:
Array
(
[0] => Array
(
[name] => a
)
[1] => Array
(
[name] => b
)
[2] => Array
(
[name] => c
)
[3] => Array
(
[name] => d
)
[4] => Array
(
[name] => c
)
[5] => Array
(
[name] => d
)
[6] => Array
(
[name] => c
)
[7] => Array
(
[name] => d
)
[8] => Array
(
[name] => e
)
)
here is a working solution for you.
$entities = [
0 => ['name' => 'a', 'type' => 'single'],
1 => ['name' => 'b', 'type' => 'single'],
2 => ['name' => 'c', 'type' => 'grouped'],
3 => ['name' => 'd', 'type' => 'grouped'],
4 => ['name' => 'e', 'type' => 'single'],
];
$grouped = [];
foreach($entities as $entity){
if($entity['type'] == 'grouped'){
$grouped[] = $entity['name'];
}
}
$f = 0;
foreach ($entities as $entity) {
if ($entity['type'] == 'single') {
$array[] = ['name' => $entity['name']];
}
if ($entity['type'] == 'grouped') {
$items = [0 => 'apple', 1 => 'orange', 2 => 'banana'];
for($i = 0; $i < count($items); $i++) {
foreach($grouped as $grouped_item){
if($f < count($items)*2){
$array[] = ['name' => $grouped_item];
}
$f++;
}
}
}
}
print_r($array);
Related
The task is to merge ("inexpensively") two arrays, which have matching key-value pairs of subarrays. E.g.:
Array 1:
Array
(
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
)
[1] => Array
(
[count] => 2
[da_table] => client_contract_specification_price
[da_class] => classes\elements\tables\ClientContractSpecificationPrice
[da_page_class] => Page_ClientContractSpecification
)
[2] => Array
(
[count] => 2
[da_table] => supplier
[da_class] => classes\elements\tables\Supplier
[da_page_class] => Page_Supplier
)
)
Array 2:
Array
(
[0] => Array
(
[name] => Articles
[name_short] =>
[da_page_class] => Page_Article
)
[1] => Array
(
[name] => Client contract specifications
[name_short] => cc_specifications
[da_page_class] => Page_ClientContractSpecification
)
[2] => Array
(
[name] => Suppliers
[name_short] =>
[da_page_class] => Page_Supplier
)
)
How to merge the two above arrays by a matching [da_page_class] => ... pairs, so the resulting array will contain both key-values of the first and the second array, i.e.:
...
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
[name] => Articles
[name_short] =>
)
...
Additional requirements:
Subarrays may come in random order. Also, there can be "orphans", which contain values of ['da_page_class'], but have no match in another array. These should be ignored.
Well, you simply iterate over the array elements and combine them:
<?php
$data1 = [
[
'count' => 1,
'da_table' => 'article',
'da_class' => 'classes\elements\tables\Article',
'da_page_class' => 'Page_Article'
],
[
'count' => 2,
'da_table' => 'client_contract_specification_price',
'da_class' => 'classes\elements\tables\ClientContractSpecificationPrice',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'count' => 2,
'da_table' => 'supplier',
'da_class' => 'classes\elements\tables\Supplier',
'da_page_class' => 'Page_Supplier'
]
];
$data2 = [
[
'name' => 'Articles',
'name_short' => null,
'da_page_class' => 'Page_Article'
],
[
'name' => 'Client contract specifications',
'name_short' => 'cc_specifications',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'name' => 'Suppliers',
'name_short' => null,
'da_page_class' => 'Page_Supplier'
]
];
$output = [];
for ($i=0; $i<count($data1); $i++) {
$output[$i] = array_merge($data1[$i], $data2[$i]);
}
print_r($output);
The output obviously is:
Array
(
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
[name] => Articles
[name_short] =>
)
[1] => Array
(
[count] => 2
[da_table] => client_contract_specification_price
[da_class] => classes\elements\tables\ClientContractSpecificationPrice
[da_page_class] => Page_ClientContractSpecification
[name] => Client contract specifications
[name_short] => cc_specifications
)
[2] => Array
(
[count] => 2
[da_table] => supplier
[da_class] => classes\elements\tables\Supplier
[da_page_class] => Page_Supplier
[name] => Suppliers
[name_short] =>
)
)
Alternatively you could also merge the contents of the elements of the second array into the corresponding elements of the first array. That reduces the memory footprint for large data sets.
Considering the additional requirement you specified in your comment I changed the merge strategy to allow for arbitrary orders of the two sets:
<?php
$data1 = [
[
'count' => 1,
'da_table' => 'article',
'da_class' => 'classes\elements\tables\Article',
'da_page_class' => 'Page_Article'
],
[
'count' => 2,
'da_table' => 'client_contract_specification_price',
'da_class' => 'classes\elements\tables\ClientContractSpecificationPrice',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'count' => 2,
'da_table' => 'supplier',
'da_class' => 'classes\elements\tables\Supplier',
'da_page_class' => 'Page_Supplier'
]
];
$data2 = [
[
'name' => 'Articles',
'name_short' => null,
'da_page_class' => 'Page_Article'
],
[
'name' => 'Client contract specifications',
'name_short' => 'cc_specifications',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'name' => 'Suppliers',
'name_short' => null,
'da_page_class' => 'Page_Supplier'
]
];
$output = [];
array_walk($data1, function($entry, $key) use (&$output, $data2) {
$output[$key] = $entry;
foreach($data2 as $cand) {
if ($entry['da_page_class'] == $cand['da_page_class']) {
$output[$key] = array_merge($output[$key], $cand);
}
}
});
print_r($output);
The resulting output obviously is identical to above.
O(m*n) solution:
$result = array();
foreach ($array1 as $value1) {
// do not handle elements without pageclass
if (!array_key_exists('da_page_class', $value1) || !$value1['da_page_class']) {
continue;
}
foreach ($array2 as $value2) {
if (!array_key_exists('da_page_class', $value2) || !$value2['da_page_class']) {
continue;
}
if ($value1['da_page_class'] == $value2['da_page_class']) {
array_push($result, $value1 + $value2)
break;
}
}
}
print_r($result);
O(m+n) solution:
$result = array();
foreach ($array1 as $value) {
// do not handle elements without pageclass
if (!array_key_exists('da_page_class', $value) || !$value['da_page_class']) {
continue;
}
$result[$value['da_page_class']] = $value;
}
foreach ($array2 as $value) {
if (
// do not handle elements without pageclass
!array_key_exists('da_page_class', $value) || !$value['da_page_class'] ||
// do not handle elements that do not exist in array 1
!array_key_exists($value['da_page_class'], $result)
) {
continue;
}
// merge values of this pageclass
$result[$value['da_page_class']] = array_merge($result[$value['da_page_class']], $value);
}
print_r($result);
EDIT: added O(m+n) solution
I am trying to compare nested multidimensional arrays by value and key so my scenario is:
I need to compare two nested multidimensional arrays and find the difference in terms of their values by quantity and update those quantities but also items which are in the new array and not the old one for example:
if I have the following data:
First Array:
Array (
[0] => Array ( [name] => hey [qty] => 3 )
[1] => Array ( [name] => hello [qty] => 1 )
[2] => Array ( [name] => test [qty] => 1 )
)
And another nested multidimensional array with the following values:
Second Array:
Array (
[0] => Array ( [name] => hey [qty] => 5 )
[1] => Array ( [name] => hello [qty] => 5 )
[2] => Array ( [name] => PHP [qty] => 2 )
)
I am trying to achieve the following output with only the new items by key in second array and update their values based upon the difference between the first and second array item's quantities i.e.:
Desired output
Array (
[0] => Array ( [name] => hey [qty] => 2 )
[1] => Array ( [name] => hello [qty] => 4 )
[2] => Array ( [name] => PHP [qty] => 2 )
)
I am producing the difference using the following but note how PHP is not added. I am not too sure how to check for it in my inner for loop without adding it.
<?php
$items = [
'hey',
'hey',
'hey',
'hello',
'test'
];
$arr = array_count_values($items);
$oldItems = array();
foreach ($arr as $name => $qty) {
$oldItems[] = compact('name', 'qty');
}
$newItems = [
['name' => 'hey', 'qty' => 5],
['name' => 'hello', 'qty'=> 5],
['name' => 'PHP', 'qty' => 2]
];
$diff = [];
foreach($newItems as $newItem) {
foreach($oldItems as $oldItem) {
if ($newItem['name'] == $oldItem['name']) {
//get quantity
$qtyDiff = $newItem['qty'] - $oldItem['qty'];
if ($qtyDiff > 0) {
$diff[$newItem['name']] = $qtyDiff;
}
}
}
}
print_r($diff); die();
My current output from this script is as follows:
Array (
[0] => Array ( [name] => hey [qty] => 2 )
[1] => Array ( [name] => hello [qty] => 4 )
)
Any help is appreciated or feedback on improvements. Thanks!
Only need one foreach:
<?php
$oldItems = [
['name' => 'hey', 'qty' => 3],
['name' => 'hello', 'qty'=> 1],
['name' => 'test', 'qty' => 1]
];
$newItems = [
['name' => 'hey', 'qty' => 5],
['name' => 'hello', 'qty'=> 5],
['name' => 'PHP', 'qty' => 2]
];
$diff = array();
foreach ($oldItems as $id => $oldRow) {
$newRow = $newItems[$id];
if ($newRow['name'] == $oldRow['name']) {
$diff[] = array(
'name' => $oldRow['name'],
'qty' => $newRow['qty'] - $oldRow['qty']
);
} else {
$diff[] = $newItems[$id];
}
}
print_r($diff);
Output:
Array
(
[0] => Array
(
[name] => hey
[qty] => 2
)
[1] => Array
(
[name] => hello
[qty] => 4
)
[2] => Array
(
[name] => PHP
[qty] => 2
)
)
If the order or size of old and new items are different, then use the following:
<?php
$oldItems = [
['name' => 'hey', 'qty' => 3],
['name' => 'hello', 'qty'=> 1],
['name' => 'test', 'qty' => 1]
];
$newItems = [
['name' => 'hey', 'qty' => 5],
['name' => 'hello', 'qty'=> 5],
['name' => 'PHP', 'qty' => 2]
];
$name2newItem = array();
foreach ($newItems as $item) {
$name2newItem[$item['name']] = $item;
}
$diff = array();
foreach ($oldItems as $id => $oldRow) {
$name = $oldRow['name'];
if (isset($name2newItem[$name])) {
$newRow = $name2newItem[$name];
$diff[$name] = array(
'name' => $name,
'qty' => $newRow['qty'] - $oldRow['qty']
);
}
}
foreach ($name2newItem as $name => $item) {
if (!isset($diff[$name])) {
$diff[$name] = $item;
}
}
$diff = array_values($diff);
print_r($diff);
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 7 years ago.
i have a multiple array.. the output looks like this:
Array
(
[0] => Array
(
[0] => AdsBot
[1] => 7
)
[1] => Array
(
[0] => SurveyBot
[1] => 1
)
[2] => Array
(
[0] => bingbot
[1] => 3
)
[3] => Array
(
[0] => bot
[1] => 27
)
what i need now is to sort arrays by there number.. so it should look exactly like this:
Array
(
[0] => Array
(
[0] => bot
[1] => 27
)
[1] => Array
(
[0] => AdsBot
[1] => 7
)
[2] => Array
(
[0] => bingbot
[1] => 3
)
[3] => Array
(
[0] => SurveyBot
[1] => 1
)
i need to sort it by the numbers array key.. but i really dont know how- well, i'm new to php
the multi. array code :
$bot_array = [
['name' => 'bingbot', 'number' => $bingbot],
['name' => 'googlebot', 'number' => $googlebot],
['name' => 'robots.txt', 'number' => $robots_txt],
['name' => 'exabot', 'number' => $exabot],
['name' => 'bot', 'number' => $bot],
['name' => 'robot', 'number' => $robot],
['name' => 'BaiDuSpider', 'number' => $BaiDuSpider],
['name' => 'Yahoo Slurp', 'number' => $yahoo_slurp],
['name' => 'AdsBot', 'number' => $adsbot],
['name' => 'SurveyBot', 'number' => $surveybot],
['name' => 'scanner', 'number' => $scanner],
['name' => 'checker', 'number' => $checker],
];
or maybe there is a more smarter way to do this?
i need this for a top ten :) on the left should be written all the names and at the right the quantity
thanks for any help :)
EDIT:
$tmp = Array();
foreach($bot_array as &$ba)
$tmp[] = &$ba["number"];
array_multisort($tmp, $bot_array);
foreach($bot_array as &$ba)
echo $ba["number"]."<br/>";
i did this but it still doesnt sort it like i want it..
0
0
0
1
10
12
27
3
3
5
7
9
this is what it gives me now :o
you can use this function
function sortAscending($accounts, $key)
{
$ascending = function($accountA, $accountB) use ($key) {
if ($accountA[$key] == $accountB[$key]) {
return 0;
}
return ($accountA[$key] < $accountB[$key]) ? -1 : 1;
};
usort($accounts, $ascending);
return $accounts;
}
You can use usort
<?php
$data = array(
array('AdsBot', 7),
array('SurveyBot', 1),
array('bingbot', 3),
array('bot', 27)
);
usort($data, 'botSort');
function botSort($val1, $val2) {
if (is_array($val1) && is_array($val2)) {
if ($val1[1] <= $val2[1]) {
return 1;
}
}
return -1;
}
var_dump($data);
Output
array(4) {
[0] =>
array(2) {
[0] =>
string(3) "bot"
[1] =>
int(27)
}
[1] =>
array(2) {
[0] =>
string(6) "AdsBot"
[1] =>
int(7)
}
[2] =>
array(2) {
[0] =>
string(7) "bingbot"
[1] =>
int(3)
}
[3] =>
array(2) {
[0] =>
string(9) "SurveyBot"
[1] =>
int(1)
}
}
According to your updated question, it should be.
<?php
usort($bot_array, 'botSort');
function botSort($val1, $val2) {
if (is_array($val1) && is_array($val2)) {
if ($val1['number'] <= $val2['number']) {
return 1;
}
}
return -1;
}
print_r($data);
Use this way to sort your array
$arry = array( array('AdsBot','7'),
array('SurveyBot','1'),
array('bingbot','3'),
array('bot','27')
);
echo "<pre>"; print_r($arry); echo "</pre>";
function sortByOrder($a, $b) {
return $a['1'] - $b['1'];
}
usort($arry, 'sortByOrder');
echo "<pre>"; print_r($arry); echo "</pre>";
Please, see bellow code.
$arr = array(
0 => array(
'0' => 'AdsBot',
'1' => 7
),
1 => array(
'0' => 'SurveyBot',
'1' => 1
),
2 => array(
'0' => 'bingbot',
'1' => 3
),
3 => array(
'0' => 'bot',
'1' => 27
)
);
echo '<pre>';
print_r(sortDescOrder($arr,'1'));
echo '</pre>';
function sortDescOrder($accounts, $key)
{
$ascending = function($accountA, $accountB) use ($key) {
if ($accountA[$key] == $accountB[$key]) {
return 0;
}
return ($accountA[$key] > $accountB[$key]) ? -1 : 1;
};
usort($accounts, $ascending);
return $accounts;
}
Or see another code
$bot_array = [
['name' => 'bingbot', 'number' => 5],
['name' => 'googlebot', 'number' => 8],
['name' => 'robots.txt', 'number' => 3],
['name' => 'exabot', 'number' => 7],
['name' => 'bot', 'number' => 7],
['name' => 'robot', 'number' => 12],
['name' => 'BaiDuSpider', 'number' => 45],
['name' => 'Yahoo Slurp', 'number' => 18],
['name' => 'AdsBot', 'number' => 78],
['name' => 'SurveyBot', 'number' => 96],
['name' => 'scanner', 'number' => 41],
['name' => 'checker', 'number' => 10]
];
usort($bot_array, function($x, $y) {
return $x['number'] <= $y['number'];
});
echo '<pre>';
print_r($bot_array);
echo '</pre>';
Try usort(). It allows you to define a callback to sort on a custom expression. In your case you want to reverse sort by the second indexed element in each sub-array:
<?php
$bots = [
[
'name' => 'AdsBot',
'number' => 7,
],
[
'name' => 'SurveyBot',
'number' => 1,
],
[
'name' => 'bingbot',
'number' => 3,
],
[
'name' => 'bot',
'number' => 27,
],
];
usort($bots, function($a, $b) {
return $a['number'] <= $b['number'];
});
print_r($bots);
Yields:
Array
(
[0] => Array
(
[name] => bot
[number] => 27
)
[1] => Array
(
[name] => AdsBot
[number] => 7
)
[2] => Array
(
[name] => bingbot
[number] => 3
)
[3] => Array
(
[name] => SurveyBot
[number] => 1
)
)
Hope this helps :)
I have three array (is about the data migration)
$a = Array
(
[0] => Array
(
[0] => province
[1] => 701
[2] => AA
[3] => A
)
..
)
$b = Array
(
[0] => Array
(
[0] => district
[1] => 70101
[2] => BB
[3] => B
)
[1] => Array
(
[0] => district
[1] => 70102
[2] => BB1
[3] => B1
)
..
)
$c = Array
(
[0] => Array
(
[0] => commune
[1] => 7010101
[2] => CC
[3] => C
),
[1] => Array
(
[0] => commune
[1] => 7010102
[2] => CC1
[3] => C1
)
..
)
What I want is to merge all $a , $b , $c' to become a new array
in this example array that have value701is the key of sub array70101and70101is the key of sub array7010101`
So the final array may look something like this:
$d = array (
701=>array(
70101=>array(7010101,7010102),
70102=>array(7010201,7010202),
),
)
The attempt is like the following:
# Your data structure here:
$a = array(
'701' => 'foo',
'702' => 'bar',
);
$b = array(
'70101' => 'foo-foo',
'70102' => 'foo-bar',
);
$c = array(
'7010101' => 'foo-foo-foo',
'7010102' => 'foo-foo-bar',
'7020101' => 'bar-foo-foo',
'7020201' => 'bar-bar-foo',
);
# The array you want
$buffer = array();
# Loop through the deepest elements (here: commune)
foreach ($c as $key => $value) {
# Find the keys for the parent groups
$province_key = substr($key, 0, 3);
$district_key = substr($key, 0, 5);
# Fill the buffer
$buffer[$province_key][$district_key][$key] = $value;
}
# Debug: The generated array
echo '<pre>';
print_r($buffer);
echo '</pre>';
You can copy&paste it here and hit run.
Test data:
$arrayA = [
[
0 => 'province',
1 => 701,
2 => 'AA',
3 => 'A'
],
[
0 => 'province',
1 => 702,
2 => 'AA1',
3 => 'A1'
],
];
$arrayB = [
[
0 => 'district',
1 => 70102,
2 => 'BB',
3 => 'B'
],
[
0 => 'district',
1 => 70101,
2 => 'BB1',
3 => 'B1'
],
];
$arrayC = [
[
0 => 'commune',
1 => 7010101,
2 => 'CC',
3 => 'C'
],
[
0 => 'commune',
1 => 7010102,
2 => 'CC1',
3 => 'C1'
]
];
Solution:
function mergeArraysToOneOnField(array $arrayA, array $arrayB, array $arrayC, $fieldName) {
$result = [];
/*
checks like
!is_string($fieldName) && !is_integer($fieldName)
*/
$arrayARelevantFields = array_column($arrayA, $fieldName);
$arrayBRelevantFields = array_column($arrayB, $fieldName);
$arrayCRelevantFields = array_column($arrayC, $fieldName);
foreach ($arrayARelevantFields as $arrayARelevantField) {
$arrayAFilteredRelevantField = filterArrayByStrpos($arrayBRelevantFields, $arrayARelevantField);
foreach ($arrayAFilteredRelevantField as $arrayBRelevantField) {
$result[$arrayARelevantField][$arrayBRelevantField] =
filterArrayByStrpos($arrayCRelevantFields, $arrayBRelevantField)
;
}
}
return $result;
}
Test run:
$mergedArray = mergeArraysToOneOnField($arrayA, $arrayB, $arrayC, 1);
print_r($mergedArray);
Test Result:
Array
(
[701] => Array
(
[70102] => Array
(
)
[70101] => Array
(
[0] => 7010101
[1] => 7010102
)
)
)
The solution can be extended with recursion, in order to hanlde a variable number of input arrays:
function mergeArraysToOneOnField(array &resultArray, array $inputAarray, $field) {
...
}
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
Output
Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )
I am trying to count from the following associative array in two's key->value filter, by Key-gender and Key-rname, Please see below ...
<pre>
Array
(
[0] => Array
(
[fname] => jone
[lname] => jani
[gender] => m
[rname] => N
)
[1] => Array
(
[fname] => jani
[lname] => jone
[gender] => m
[rname] => N
)
[2] => Array
(
[fname] => sara
[lname] => bulbula
[gender] => w
[rname] => D
)
[3] => Array
(
[fname] => hani
[lname] => hanu
[gender] => w
[rname] => P
)
[4] => Array
(
[fname] => ttttttt
[lname] => sssssss
[gender] => m
[rname] => C
)
)
</pre>
What i want the result is as follows..
rname gender
m w
N 2 0
D 0 1
P 0 1
C 1 0
total 3 2
Some help please?
I don't see your code so I am trying to make own code.
$arr = array(
array(
'fname' => 'jone',
'lname' => 'jani',
'gender' => 'm',
'rname' => 'N'
),
array(
'fname' => 'jani',
'lname' => 'jone',
'gender' => 'm',
'rname' => 'N'
),
array(
'fname' => 'sara',
'lname' => 'bulbula',
'gender' => 'w',
'rname' => 'D'
),
array(
'fname' => 'hani',
'lname' => 'hanu',
'gender' => 'w',
'rname' => 'P'
),
array(
'fname' => 'ttttttt',
'lname' => 'sssssss',
'gender' => 'm',
'rname' => 'C'
),
);
$matrix = array();
foreach (array('N', 'D', 'P', 'C', 'total') as $name) {
foreach (array('m', 'w') as $gender) {
$matrix[$name][$gender] = 0;
}
}
foreach ($arr as $el) {
$matrix[$el['rname']][$el['gender']] += 1;
$matrix['total'][$el['gender']] += 1;
}
echo '<pre>';
print_r($matrix);
echo '</pre>';
// OR dynamicaly keys
$matrix = array();
foreach ($arr as $el) {
if (!isset($matrix[$el['rname']][$el['gender']]))
$matrix[$el['rname']][$el['gender']] = 0;
if (!isset($matrix['total'][$el['gender']]))
$matrix['total'][$el['gender']] = 0;
$matrix[$el['rname']][$el['gender']] += 1;
$matrix['total'][$el['gender']] += 1;
}
echo '<pre>';
print_r($matrix);
echo '</pre>';
I have a similar function which I have used to do something similar to your request.. So this is just a copy and paste, because you have shown a lack of code.. This returns an array as your expecting.. Just iterating over this returned array, is something you will need to research into yourself
function Array_Counter($Array){
$Return_Arr = array(
"M" => array(),
"F" => array()
);
foreach ($Array AS $Arr){
if ($Arr['gender'] === "m"){
//Is Male
#$Return_Arr['M'][$Arr['rname']] ++; // Message supressed due to returning undefined index warnings... Which because of not using isset().. Will still work
}elseif ($Arr['gender'] === "f"){
// Is Female
#$Return_Arr['F'][$Arr['rname']] ++; // Message supressed due to returning undefined index warnings... Which because of not using isset().. Will still work
}
}
return $Return_Arr;
}
$Array = array (
array(
"gender" => "m",
"rname" => "c"
),
array(
"gender"=> "f",
"rname" => "d"
),
array (
"gender" => "f",
"rname" => "a"
),
array (
"gender" => "f",
"rname" => "x"
),
/* Array shortened for exampling purposes */
);
$Array_Count = Array_Counter($Array);
Using the function as followed
echo "<pre>";
print_r($Array_Count);
echo "</pre>";
Returns:
Array
(
[M] => Array
(
[c] => 1
[j] => 1
[g] => 1
[d] => 1
)
[F] => Array
(
[d] => 3
[a] => 1
[x] => 1
)
)