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);
Related
Hi i have a multidimensional array and a parameter that may change frequently .
i made some code to extract data just for 3 level loop through array .
but i can't convert it to function that work in array_map or array_walk .
is it possible ?
sample array data :
array(4) {
[0]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test1"
}
["rel"]=>
array(1) {
["id"]=>
int(1)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test3"
}
["rel"]=>
array(1) {
["id"]=>
int(3)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test4"
}
["rel"]=>
array(1) {
["id"]=>
int(4)
}
}
}
[1]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test10"
}
["rel"]=>
array(1) {
["id"]=>
int(10)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test11"
}
["rel"]=>
array(1) {
["id"]=>
int(11)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test12"
}
["rel"]=>
array(1) {
["id"]=>
int(12)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test13"
}
["rel"]=>
array(1) {
["id"]=>
int(13)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test14"
}
["rel"]=>
array(1) {
["id"]=>
int(14)
}
}
}
[2]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test20"
}
["rel"]=>
array(1) {
["id"]=>
int(20)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test21"
}
["rel"]=>
array(1) {
["id"]=>
int(21)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test22"
}
["rel"]=>
array(1) {
["id"]=>
int(22)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test23"
}
["rel"]=>
array(1) {
["id"]=>
int(23)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test24"
}
["rel"]=>
array(1) {
["id"]=>
int(24)
}
}
}
}
and here is sample function that can loop 3 times
but i want make it dynamic loop through this array
foreach ( $data as $k => $v ) {
$cnt = 0 ;
foreach ( $data as $m => $n ) {
array_push($parents,$k);
if ( !in_array($m,$parents)) {
foreach ( $data as $x => $y ) {
array_push($parents,$m);
if ( !in_array($x,$parents)) {
$hold[$k][$cnt] = array(
$k , $m , $x
);
$cnt++;
}
}
$parents = deleteFromArray($parents , $m);
}
}
$parents = deleteFromArray($parents , $k);
}
I call to array and try to compare the values, is there something wrong in my syntax?
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
$field = "100";
if ($item["#attributes"]["tag"] == $field) {
}
}
This is my array:
array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["#value"]=> string(5) "22333" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["#value"]=> string(3) "110" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(2) { [0]=> array(2) { ["subfield"]=> array(2) { ["#value"]=> string(2) "92" ["#attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["#value"]=> string(4) "ntft" ["#attributes"]=> array(1) { ["cod"]=> string(1) "b" } } [1]=> array(2) { ["#value"]=> string(5) "nthgfr" ["#attributes"]=> array(1) { ["cod"]=> string(1) "c" } } [2]=> array(2) { ["#value"]=> string(5) "test2" ["#attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } } } } }
I compare `tag = 100` to a variable with value 100: `if ($item["#attributes"]["tag"] == $field)`
This array I received after changes that used from last discussion from this post:
array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["#value"]=> string(5) "22333" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["#value"]=> string(3) "110" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(3) { [0]=> array(2) { ["subfield"]=> array(2) { ["#value"]=> string(2) "92" ["#attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["#value"]=> string(4) "ntft" ["#attributes"]=> array(1) { ["code"]=> string(1) "b" } } [1]=> array(2) { ["#value"]=> string(5) "nthgfr" ["#attributes"]=> array(1) { ["code"]=> string(1) "c" } } [2]=> array(2) { ["#value"]=> string(4) "test" ["#attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } ["subfield"]=> array(1) { [2]=> array(1) { ["#value"]=> string(12) "26A 1 2 test" } } } } } }
You're using ["#attributes"] but $item is not a "standard" array. It seems to be a SimpleXMLElement object.
Try to use the following code :
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
$field = "100";
if ((string)$item["tag"] == $field) {
var_dump("Equals") ;
}
}
edit
$field = "100";
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
var_dump($key_item) ;
if (is_array($item)) {
foreach ($item as $key_element => $element) {
var_dump($key_element) ;
if (!isset($element["#attributes"])) { echo("no attribute"); continue ; }
if (!isset($element["#attributes"]['tag'])) { echo("no tag"); continue; }
if ($element["#attributes"]["tag"] == $field) {
var_dump("match") ;
}
}
}
else{
echo "item is not an array" ;
}
}
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);
}
?>
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 want to compare the key values in the array and if a match is found to increase the value of the element.
For this I use code:
// var_dump $all_array
array(2) {
[0]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269755)
}
[1]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269874)
}
}
// var_dump $count
array(2) { [0]=> string(2) "10" [1]=> string(1) "1" }
// var_dump $product
array(2) { [0]=> string(10) "1372269755" [1]=> string(10) "1372269874" }
$count=$_POST['count'];
$product=$_POST['product'];
$count_arr_products=count($product);
for ($i=0; $i<=$count_arr_products; $i++){
foreach ($all_array as $keys => $elms) {
if ($product[$i]==$elms[5]) {
if($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($keys);
}
}
}
}
but step $elms[2] = $count[$i]; not work - in result value $elms[2] not change...
You need to majke $elms a reference. By default it will be a copy of the sub-array, so assignment won't update the original array.
$all_array = array(array("art_7880", "Арт.7880", "1", NULL, "png", 1372269755),
array("art_7880", "Арт.7880", "1", NULL, "png", 1372269874));
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count_arr_products = count($product);
for($i=0; $i<$count_arr_products; $i++){ // Use < not <=
foreach ($all_array as $keys => &$elms) { // Use a reference so we can update it
if ($product[$i]==$elms[5]){
if ($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($all_array[$keys]); // not unset($keys)
}
}
}
}
var_dump($all_array);
Output:
array(2) {
[0]=>
array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(2) "10"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269755)
}
[1]=>
&array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(1) "1"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269874)
}
}