I am trying to loop through a multidimensional array but in the foreach loop it just outputs error
index 'name' not found. index 'calories' not founder
foreach($responsex['foods'] as $fx5)
{
echo($fx5['name']);
echo($fx5['calories']);
}
Response: i.e. $responsex
array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) {
["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false)
["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449)
["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC"
["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574)
["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU"
["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding"
["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup"
["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754)
[1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=>
int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=>
array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=>
float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1)
["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=>
bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=>
int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=>
string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) ""
["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=>
string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish,
Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3)
{ ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4)
"cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=>
int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147)
[7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=>
int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=>
float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=>
array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10)
"2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) {
["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=>
string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638)
["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=>
string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226)
["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=>
array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389)
} } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157)
["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9)
["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=>
array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=>
int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090)
["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4)
["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) }
}
you can recursively iterate through the arrays and print them as follows as key value pairs.
<?php
//initially call the function
print_array($responsex);
function print_array($array){
foreach($array as $key=>$value){
//recursively print the array
if(is_array($value)){
echo("Array : ".$key."\n");
print_array($value);
}
else{
echo($key." => ".$value);
}
}
}
?>
You can define additional tasks other than printing them with the above code.
Edit:
if you are sure that the array is two dimensional, no need to go recursively.
<?php
//initially call the function
print_array($responsex);
//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
foreach($array as $key=>$value){
if(is_array($value)){
if($key==="foods"){
var_dump($array[$key]);
}
}
else{
echo($key." => ".$value);
}
}
}
Use this way..
<?php
$keys = array_keys($data);// put your array name as a place of $data
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
?>
Related
I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5
Wrote this recursive function to reindex an array (see image below - JSONized Array I work with, to better see it)
Code:
private function reindex($array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key]=$this->reindex($value);
}
array_values($array);
}
return $array;
}
The goal is to reindex it so there are no jump in indexes i.e
$arr['body'][0]['1']['body'] has elements at indexes 2, 4 and 6, would like to reindex it to be 0, 1, 2
Should work for different arrays also (n amount of nested arrays). Should be general function.
How can I do it?
Thanks,
Update 1:
Var_dump output:
array(4) {
["token_name"]=>
string(6) "C_ROOT"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(7) {
["token_name_org"]=>
string(8) "T_SWITCH"
["token"]=>
int(339)
["value"]=>
string(6) "switch"
["line"]=>
int(2)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(8) "C_SWITCH"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(4) "argv"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "1"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
}
}
[1]=>
array(4) {
["token_name"]=>
string(7) "C_BLOCK"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(4) {
[0]=>
array(1) {
[0]=>
array(4) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(2) "10"
["line"]=>
int(4)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
[2]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "single_quoted"
["line"]=>
int(7)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(5) {
[0]=>
array(6) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(3) "val"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
}
[1]=>
array(6) {
["token_name_org"]=>
string(18) "C_ASSIGNMENT_EQUAL"
["line"]=>
int(8)
["value"]=>
string(1) "="
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(11) "ASSIGNMENTS"
["token_name"]=>
string(18) "C_ASSIGNMENT_EQUAL"
}
[2]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "3"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
[3]=>
array(6) {
["token_name_org"]=>
string(7) "VALUE_-"
["line"]=>
int(8)
["value"]=>
string(1) "-"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(9) "OPERATORS"
["token_name"]=>
string(16) "C_OPERATOR_MINUS"
}
[4]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "5"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
[4]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double_quoted"
["line"]=>
int(11)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(6) "T_ECHO"
["token"]=>
int(328)
["value"]=>
string(4) "echo"
["line"]=>
int(13)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(6) "C_ECHO"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double quoted"
["line"]=>
int(13)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
}
}
}
}
}
}
[6]=>
array(3) {
[0]=>
array(4) {
["token_name_org"]=>
string(9) "T_DEFAULT"
["token"]=>
int(342)
["value"]=>
string(7) "default"
["line"]=>
int(18)
}
[1]=>
array(6) {
["token_name_org"]=>
string(17) "C_SEPARATOR_COLON"
["line"]=>
int(18)
["value"]=>
string(1) ":"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(10) "SEPARATORS"
["token_name"]=>
string(17) "C_SEPARATOR_COLON"
}
[2]=>
array(6) {
["token_name_org"]=>
string(7) "T_BREAK"
["token"]=>
int(343)
["value"]=>
string(5) "break"
["line"]=>
int(19)
["token_group"]=>
string(16) "TODO_BREAK_GROUP"
["token_name"]=>
string(7) "C_BREAK"
}
}
}
}
}
}
}
Here is a method that will only reindex numeric values:
function reindex($array)
{
$index = 0;
$return = [];
foreach ($array as $key => $value) {
if (is_string($key)) {
$newKey = $key;
} else {
$newKey = $index;
++$index;
}
$return[$newKey] = is_array($value) ? reindex($value) : $value;
}
// Sort alphabetically, numeric first then alpha
ksort($return, SORT_NATURAL);
return $return;
}
Example here: http://ideone.com/969OGa
I have array of students which is fetched in a variable $arraytosort and looks like
array(5027) { [0]=> array(5) { ["_id"]=> object(MongoId)#22 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["fullname"]=> string(19) "David Robert Lara" ["firstmiddle"]=> string(12) "David Robert" ["firstlast"]=> string(14) "David Lara" ["student"]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "David" ["middle_name"]=> string(4) "Robert" ["last_name"]=> string(6) "Lara" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } }
...
...
}
I have written below function to sort these students on the basis of key value given in $key so that the array will contain the similar results on top/first.
$sortedarray = array();
$sortedarray = $this->multiSort(
$arraytosort,
$key,
'fullname',
'firstmiddle',
'firstlast',
'first_name',
'middle_name',
'last_name',
'roll_no',
'email'
);
function multiSort()
{
$args = func_get_args();
$c = count($args);
if ($c < 2) {
return false;
}
$key= $args[1];
$array = array_splice($args, 0, 1);
$array = $array[0];
usort($array, function($a) use($args) {
$i = 0;
$c = count($args);
$cmp = 0;
while($cmp == 0 && $i < $c)
{
$cmp = stripos($a[$args[$i] ],$key);
$i++;
}
return $cmp;
});
return $array;
}
It throws error message "Undefined variable: key".
Please help!!!
I have the following:
[6199]=>
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
[6192]=>
array(11) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Java"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "java"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
and my expected output is:
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba".
[1]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars",
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
[2]=>
string(9) "Java"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
[3]=>
string(9) "java"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
See how it eliminates dupes and merges each array maintaining the "origin" key.
I've tried :
foreach ($resultterms as $keyname => $valuename){
foreach ($valuename as $keysub => $valuesub) {
foreach($valuesub['name'] as $keysubsub => $valuesubsub){
# code...
$prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
$prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
$prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];
}
}
}
where $resultterms is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?
I believe you're just looking for array_merge_recursive.
call_user_func_array('array_merge_recursive', array_values($prod_atts));
call_user_func_array allows to transform an array into a list of arguments
array_values because in the end, you seem to want to get rid of the first layer of your array
In order to try it, could you post the var_export of your variable instead of the var_dump?
echo(var_export($prod_atts, true));
merge your array by any suggested method. After that you will get duplicated values. And you need save only the unique items
$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);
foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }
I have this Array:
array(66) {
[0]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105a"
}
[1]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105b"
}
[2]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105c"
}
[3]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105d"
}
[4]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117b"
}
[5]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117c"
}
[6]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "123a"
}
[7]=> array(2) {
["location"]=> string(10) "Whateverelse" ["bsid"]=> string(4) "123b"
}
}
How can I count how many Breakwater's I have and how many Whateverelse's and get something like this:
array(2) {
[0]=> array(2) {
["Breakwater"]=> string(2) "20"
} [1]=> array(2) {
["Whateverelse"]=> string(1) "1"
}
}
Just loop around the original array, and each time a location is hit, increment a counter in the locations array where the index is the location.
$loc = array();
foreach($arr as $value) {
$location = $value['location'];
if(isset($loc[$location])) {
$loc[$location]++;
} else {
$loc[$location] = 1;
}
}
print_r($loc);
Will output
array
(
["Breakwater"] => 7,
["Whateverelse"] => 1
}
I got this finally :
$output = array("Breakwater" => 0, "Whateverelse" => 0);
foreach ($array as $val) {
$output["Breakwater"] += ($val["location"] == "Breakwater") ? 1 : 0;
$output["Whateverelse"] += ($val["location"] == "Whateverelse") ? 1 : 0;
}
var_dump($output);