counting foreach loop items - php

i have the following array:
[2]=>
object(stdClass)#9 (4) {
["ID"]=>
string(32) "43c845f895a56fbe8aea9435ef8fa806"
["Type"]=>
string(8) "Campaign"
["Name"]=>
string(28) "An unmissable invitation for"
["Actions"]=>
array(5) {
[0]=>
object(stdClass)#10 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 17:00:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[1]=>
object(stdClass)#11 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 09:01:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[2]=>
object(stdClass)#12 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 22:29:00"
["IPAddress"]=>
string(14) "94.171.192.216"
["Detail"]=>
string(0) ""
}
[3]=>
object(stdClass)#13 (4) {
["Event"]=>
string(5) "Click"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(60) "http://www.rbh.co.uk/rbhevent/?name=[fullname]&email=[email]"
}
[4]=>
object(stdClass)#14 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
}
}
i am trying to count the events that are the same. So for example ["Event"] = Open =4 / ["Event"] = Click =1.
I am trying to achieve this via counting a foreach loop:
$i=0;
foreach($entry->Actions as $actions ) {
echo $i++;
}
Im not quite sure how to approach this? Can someone suggest a best practice?

$counts = array();
foreach($entry->Actions as $actions) {
if(!isset($counts[$actions->Event])) {
$counts[$actions->Event] = 0;
}
++$counts[$actions->Event];
}
print_r($counts);

<?php
$amounts = array(); // Events as key
foreach($entry->Actions as $actions)
{
if (isset($amounts[$actions["Event"]])) $amounts[$actions["Event"]]++;
else $amounts[$actions["Event"]] = 1;
}
print_r($amounts);
echo "<br>".$amounts["Open"];
?>

Related

php foreach with additional condition

I Have a Json output like
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(1) ["monthly"]=> string(4) "4.99" } [79]=> object(stdClass)#301 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(2) ["monthly"]=> string(4) "345.23" } [80]=> object(stdClass)#300 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(6) ["monthly"]=> string(5) "4.01" } [81]=> object(stdClass)#299 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(1) ["monthly"]=> string(4) "5.99" } [82]=> object(stdClass)#298 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(2) ["monthly"]=> string(6) "443.44" } [83]=> object(stdClass)#297 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(6) ["monthly"]=> string(4) "5.03" } [84]=> object(stdClass)#296 (4)
I want to get the value with id as 19 and currency as 2. The output I require is 443.44
I was trying using PHP foreach for the above json decoded output. It always gives me the first value of 5.99
I am using the below code.
$pid = 19;
$cur = 2;
foreach($products as $product){
$getproductrrice[$product->id] = $product->monthly;
}
return $getproductrrice[$pid];
I need to add one more condition so that I can pass currency and get the output as 443.44.
AS you are looping, test each product to see if it is one you want
$pid = 19;
$cur = 2;
foreach($products as $product){
if ( $product->id == $pid && $product->currency == $cur ) {
$getproductrrice[$product->id] = $product->monthly;
}
}

Concrete5 - PHP: Cannot get desired sort order from array using usort

So I'm trying to alphabetically order my article titles in concrete5, however, they always show the default order. I've attempted different methods of sorting before finding this documentation but it still doesn't seem to work. I'm newer to the concept of sorting something like this.
If someone could clarify how usort works --I was a bit confused by the php documentation I read. How is it making a comparison between $a and $b? I imagine this might not even be the way to go since I want its sort order to be alphabetical.
Any wisdom would be greatly appreciated.
Here is the code I'm working with:
<?php
function orderNode($a, $b) {
return strcmp($a->value, $b->value);
}
usort($artCategoryOptions, "orderNode");
?>
<?php if($urlparam == '?category=Articles' || strpos($urlparam, '?article_category') !== false) {?>
<h3>ARTICLE CATEGORIES</h3>
<ul class="nav nav-pills nav-stacked space-bottom">
<?php foreach ($artCategoryOptions as $cat) { ?>
<li class="<?php if ($artCategoriesFilter == $cat->value) echo 'active'; ?>">
<a href="/newsroom/?article_category=<?php echo $cat->value ?>">
<strong><?php echo $cat->value ?></strong>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
The vardump of $artCategoryOptions --which is what I'm trying to sort is:
object(SelectAttributeTypeOptionList)#327 (2)
{ ["options":"Concrete5_Model_SelectAttributeTypeOptionList":private]=> array(7)
{ [0]=> object(SelectAttributeTypeOption)#328 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "49"
["value"]=> string(10) "Employment"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(0)
"usageCount"]=> string(1) "6"
}
[1]=> object(SelectAttributeTypeOption)#329 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "52"
["value"]=> string(18) "Products Liability"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(1)
["usageCount"]=> string(1) "3"
}
[2]=> object(SelectAttributeTypeOption)#330 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "50"
["value"]=> string(10) "Litigation"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(2)
["usageCount"]=> string(1) "3"
}
[3]=> object(SelectAttributeTypeOption)#331 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "48"
["value"]=> string(12) "Construction"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(3)
["usageCount"]=> string(1) "2"
}
[4]=> object(SelectAttributeTypeOption)#332 (6)
{
["error"]=> string(0) "" ["ID"]=> string(2) "53"
["value"]=> string(11) "Health Care"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(4)
["usageCount"]=> string(1) "2"
}
[5]=> object(SelectAttributeTypeOption)#333 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "54"
["value"]=> string(11) "Real Estate"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(5)
["usageCount"]=> string(1) "1"
}
[6]=> object(SelectAttributeTypeOption)#334 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "55"
["value"]=> string(17) "Creditor's Rights"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(6)
["usageCount"]=> string(1) "1" }
}
["error"]=> string(0) ""
}
I believe if you go into the Dashboard and change the "Option Order" for the attribute to "Alphabetical" it would return them in the order you want without you having to re-sort them.

Unshift the array on condition

I have written below lines of code
public function shiftarray($cursor, $arg)
{
$keyarguments = array("first_name","roll_no");
$arrayStudents =array();
foreach ($cursor as $k => $row)
{
foreach ($keyarguments as $key)
{
if (strcasecmp($row[$key], $arg) == 0)
{
array_unshift($arrayStudents, $row);
}
else
{
}
}
}
return $arrayStudents;
}
I have array of students in $cursor like
{ [0]=> 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) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["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 am trying to bring those student on the beginning of array whose first name is brian and rest of students should at the bottom.
Now the above code place the students on the top of the array but the rest of the students are ommited/removed. I don't want them to be removed.
I am trying to tweak what code to write in else condition.
Please help!!!
Fill $arrayStudents before the loop otherwise only matches will be collected and unshifted.
Extend example of php.net/unshift
$add = array('big');
$queue = array(
array("orange"),
array("banana"),
array("apple1"),
array("raspberry")
);
array_unshift($queue, $add);
print_r($queue);
In your case: search for keys you want to unshift, the rest goes to the standard $list = array(/list of studends/):
$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
if (/* in in the list to bring on top*/) {
$memory[] = $item;
} else {
$list[] = $item;
}
}
foreach($memory as $item) {
array_unshift($list, $item);
}
print_r($list);
Kind regards
Add $arrayStudents[] = $row; to your else scope. Here is a simple demo to illustrate it.
When matches add the element to the result from the start of array, unmatches add them from the end of the array.

Woocommerce Booking - Get a specific value from "Data" Cart item object

I have this array, that is dumped from WooCommerce using these lines:
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
// some code here…
}
If I make a raw output of my cart ($items) and I get this:
["f584d8671586d336d84e8cf9ed43303c"]=> array(11) {
["booking"]=> array(15) {
["_year"]=> int(2016)
["_month"]=> int(8)
["_day"]=> int(28)
["_persons"]=> array(1) {
[0]=> int(1)
}
["_date"]=> string(9) "2016-8-28"
["date"]=> string(13) "28 août 2016"
["_time"]=> string(5) "21:30"
["time"]=> string(11) "21 h 30 min"
["_qty"]=> int(1)
["Personnes"]=> int(1)
["_start_date"]=> int(1472419800)
["_end_date"]=> int(1472421600)
["_all_day"]=> int(0)
["_cost"]=> int(0)
["_booking_id"]=> int(13013)
}
["product_id"]=> int(12856)
["variation_id"]=> int(0)
["variation"]=> array(0) { }
["quantity"]=> int(1)
["line_total"]=> float(0)
["line_tax"]=> int(0)
["line_subtotal"]=> int(0)
["line_subtotal_tax"]=> int(0)
["line_tax_data"]=> array(2) {
["total"]=> array(0) { }
["subtotal"]=> array(0) { }
}
["data"]=> object(WC_Product_Booking)#11131 (20) {
["availability_rules":"WC_Product_Booking":private]=> array(0) { }
["id"]=> int(12856)
["post"]=> object(WP_Post)#11132 (24) {
["ID"]=> int(12856)
["post_author"]=> string(2) "14"
["post_date"]=> string(19) "2016-08-16 22:04:09"
["post_date_gmt"]=> string(19) "2016-08-16 20:04:09"
["post_content"]=> string(0) ""
["post_title"]=> string(10) "La Cuchara"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(4) "open"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(12) "la-cuchara-2"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2016-08-16 22:13:52"
["post_modified_gmt"]=> string(19) "2016-08-16 20:13:52"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(59) ""
["menu_order"]=> int(0)
["post_type"]=> string(7) "product"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
}
["product_type"]=> string(7) "booking"
["shipping_class":protected]=> string(0) ""
["shipping_class_id":protected]=> int(0)
["total_stock"]=> NULL
["supports":protected]=> array(0) { }
["price"]=> string(1) "0"
["wc_display_cost"]=> string(0) ""
["wc_booking_base_cost"]=> string(0) ""
["wc_booking_min_duration"]=> string(1) "1"
["wc_booking_cost"]=> string(0) ""
["wc_booking_has_resources"]=> string(2) "no"
["wc_booking_has_persons"]=> string(3) "yes"
["wc_booking_has_person_types"]=> string(2) "no"
["wc_booking_min_persons_group"]=> string(1) "1"
["tax_status"]=> string(7) "taxable"
["stock_status"]=> string(7) "instock"
["manage_stock"]=> string(2) "no"
}
}
I would like to use specific data values in some php variables, and I don't find the right way to achieve this…
How can I do to get the specific value from ["wc_booking_has_resources"]?
Thanks
FIRST — Try first this (but I am unsure as I think it can be an object, so may be it will not work):
echo $items[0]['data']['wc_booking_has_resources'] . '<br>';
SECOND — With a foreach loop it will work certainly:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// displaying the value for test
echo 'Booking has ressources: ' . $item_data['wc_booking_has_resources'];
// or may be this one (as $item['data'] is an object)
echo 'Booking has ressources: ' . $item_data->wc_booking_has_resources;
One of these will work, for sure…
Update related to author comments
How to get The booking try one of these:
// first (very unsure of this one)
$item_data = $items[0]['data']['id'];
echo $item_data['id'] . '<br>'; // not sure this one work
Using a for each loop it will be much better:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// As $item['data'] is an object
$booking_has_resources = $item_data->booking_has_resources;
// displaying
echo 'Booking has ressources: ' . $booking_has_resources;
This should work

Cut array by sub array key value

I got this array
array(5) {
[0]=>
array(5) {
["id"]=>
int(1411667077)
["nachricht"]=>
string(13) "iiiiiiiiiiiii"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "25.09.2014 19:44"
["deleted"]=>
string(0) ""
}
[1]=>
array(5) {
["id"]=>
int(1411701734)
["nachricht"]=>
string(2) "dd"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[2]=>
array(5) {
["id"]=>
int(1411701737)
["nachricht"]=>
string(6) "swfsfs"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[3]=>
array(5) {
["id"]=>
int(1411701739)
["nachricht"]=>
string(7) "egwegeg"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[4]=>
array(5) {
["id"]=>
int(1411742201)
["nachricht"]=>
string(3) "sss"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 16:36"
["deleted"]=>
string(0) ""
}
}
I want to cut the where the id is 1411701737 so I tryed:
foreach($array as $arr => $sub_arr)
{
if $sub_arr['id'] == 1411701737
{
break;
}
}
I know I need to create a whole new array in the foreach, but isn't there maybe a build in function?
$arr_new = array(); //Define a new array
foreach($arr_old as $a) { //Loop through the old one
if($a['id'] === 1411701737) break; //If the value of id matches, leave the foreach loop
$arr_new[] = $a; //Else, copy this array to the new array
}
print_r($arr_new); //Print the new array
$new = array();
foreach($array as $k) {
if($k['id'] == 1411701737) break;
array_push($new, $k);
}
var_dump($new);
The answer by Mooseman is likely a faster way to do this since it does not rely on a PHP function call to place the array into the new array. However unless you are doing this on a set of arrays that is in the 100k+ it really won't matter.

Categories