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

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

Related

Parse JSON to variable or MySQL

Sorry for the question I am not PHP skiller :)
I have a problem with parsing data from JSON.
This is my JSON after dump
$response = curl_exec($ch);
$data = json_decode($response, true);
$c = count($data['products']);
var_dump($data);
array(4) {
["products"]=>
array(23347) {
[0]=>
array(25) {
["name"]=>
string(15) "DOOKY Strieška"
["description"]=>
string(0) ""
["count"]=>
float(0)
["measureType"]=>
string(2) "ks"
["price"]=>
float(20.83)
["vat"]=>
float(20)
["stockCardNumber"]=>
string(6) "000004"
["typeId"]=>
int(1)
["warehouseCode"]=>
string(3) "MOL"
["timestamp"]=>
string(19) "2019-05-09T10:38:30"
["foreignName"]=>
string(0) ""
["customText"]=>
string(12) "silver stars"
["warrantyPeriod"]=>
float(0)
["weightPerUnit"]=>
float(0)
["volumePerUnit"]=>
float(0)
["numberingSequenceCode"]=>
string(2) "SK"
["ean"]=>
string(13) "5038278982969"
["stn"]=>
string(0) ""
["jkpov"]=>
string(6) "126367"
["din"]=>
string(0) ""
["bn"]=>
string(0) ""
["plu"]=>
int(0)
["warrantyPeriodTypeId"]=>
int(0)
["priceWithVat"]=>
float(25)
["eshop"]=>
NULL
}
I need to get every EAN, JKPOV, COUNT to variable, and insert to MySQL
I have this code but it doesn't work properly, because not write me all data. But in var_dump($data); I have this missing data...
$i=0;
for ($i=0; $i <= $c; $i++) {
$ean = $data['products'][$i]['ean'];
$count = $data['products'][$i]['count'];
$jkpov = $data['products'][$i]['jkpov'];
conf::$db->query("insert ignore into sklad set `ean`='$ean', `jkpov`='$jkpov', `count`='$count'");
$i++; }

Parse non standard xml with PHP

I have this xml-like string coming from a SOAP call:
<GSSchedule schemaVersion="3.1.0.0" xmlns:gs_pe="PEContentSchemaGS" xmlns:gs_sl="SLContentSchemaGS" xmlns:gs_s="SongSchemaGS" xmlns:gs_l="LinkSchemaGS" xmlns:gs_b="BreakSchemaGS" xmlns:gs_n="NoteSchemaGS" xmlns:gs_err="OperationStatusSchemaGS" xmlns:gs_spot="SpotSchemaGS" xmlns:gs_control="ControlSchemaGS" xmlns:gs_pol="PolicySchemaGS" xmlns:gs_syncpol="SynchronizationPolicySchemaGS" xmlns="ScheduleSchemaGS">
<Day scheduledDate="2020-04-24">
<Hour scheduledTime="10:00:00" publicationStatus="" />
<Event entryType="ExactTimeMarker" entryID="1" status="Happened" timing="None" chain="Overlap" airDate="2020-04-24" airTime="10:00:00" cumulativeAirTime="2020-04-24T10:00:00" duration="0" runtime="0" skip="false" external="false" dontCopyPosition="false" fixedPosition="false" scheduledTime="10:00:00" startOffset="0" editCode="0" failureCode="0" fileName="" unscheduledCategoryGroup="" guid="84acfcc0-a580-ea11-87c8-000c29a54856" runtimeOverride="" intro1Override="" intro2Override="" intro3Override="" earlyNextToPlayOverride="">
<EventInfo>
<ThirdParty type="GS">11111</ThirdParty>
</EventInfo>
<TimeMarker type="Soft" />
</Event>
<Event entryType="Song" entryID="24955" status="Happened" timing="None" chain="Overlap" airDate="2020-04-24" airTime="10:00:21" cumulativeAirTime="2020-04-24T10:00:00" duration="2228.28" runtime="2228.19" skip="false" external="false" dontCopyPosition="false" fixedPosition="false" scheduledTime="10:00:00" startOffset="0" editCode="8" failureCode="0" fileName="" unscheduledCategoryGroup="" guid="517a9ac1-500b-4dfd-8886-186a8a68167d" runtimeOverride="" intro1Override="" intro2Override="" intro3Override="" earlyNextToPlayOverride="">
<EventInfo />
<ProgramElement>
<gs_s:Song internalID="24955" category="Rl" categoryGroup="Rock Live" dateAdded="2019-09-27T08:59:17" lastEdited="2020-04-23T10:15:49" startDate="" endDate="" startTime="" endTime="" stopset="false" bed="None" live="false" splitBehavior="None" guid="e9c2cc4b-0783-4b9d-a4ba-4928edec1312">
<gs_s:Title name="Radio - Aprile 2020" internalID="40251" />
<gs_s:Artist name="Giovanni" internalID="7572">
<gs_s:ArtistCodes>
<gs_s:Participants>
<gs_s:Vocalist name="Giovanni" />
</gs_s:Participants>
</gs_s:ArtistCodes>
</gs_s:Artist>
<gs_s:AlternateCategories />
<gs_s:Album name="MyRadio" internalID="111" />
<gs_s:SongGroup name="" />
<gs_s:FilmName name="" />
<gs_pe:Changes lastPlayDate="2020-04-24" lastPlayTime="14:02:12" lastEditedDateTime="2020-04-23T10:15:49" lastStationEditedDateTime="2019-09-27T08:59:17" dateActivated="2019-09-27T08:59:17" totalPlays="15" enteredCategory="2019-09-27T08:59:17" playsInCategory="0" />
<gs_pe:Media ID="" fileName="\\mysrv\mystore\Audio\92\92860~RFF_8_apr2020.mp3" trimStart="0.02" trimEnd="2228.27" hookStart="" hookEnd="" intro1="" intro2="" intro3="" opening="" ending="" totalTime="2228.25" runtime="2228.19" mediaExternal="false" earlyNextToPlay="" bpmIn="" bpmOut="" bpmOverall="" />
<gs_pe:HourRestriction action="unban" />
<gs_sl:Additional publisher="" isrc="" license="" label="" recordNumber="" promoter="" country="" association="" prefix="" suffix="" />
<gs_sl:UserDefined userDefined1="" userDefined2="" userDefined3="" userDefined4="" userDefined5="" userDefined6="" userDefined7="" userDefined8="" userDefined9="" userDefined10="" />
</gs_s:Song>
</ProgramElement>
</Event>
</Day>
<IsExportError>False</IsExportError>
</GSSchedule>
I would like to traverse the structure and get the value of the //Day/Event/ProgramElement/gs_s:Song/gs_s:Title node in PHP.
I tried to parse like a normal xml, with that:
<?php
$rawxml = "<GSSchedule [...]"; // The string pasted above
$xmlDoc = #new \SimpleXMLElement( $rawxml );
(Better elaboration based on comments) But, when I try to var_dump the response, I have a strange (for me) response. With no data for the node I'm looking for
object(SimpleXMLElement)#1 (3) {
["#attributes"]=>
array(1) {
["schemaVersion"]=>
string(7) "3.1.0.0"
}
["Day"]=>
object(SimpleXMLElement)#2 (3) {
["#attributes"]=>
array(1) {
["scheduledDate"]=>
string(10) "2020-04-24"
}
["Hour"]=>
object(SimpleXMLElement)#3 (1) {
["#attributes"]=>
array(2) {
["scheduledTime"]=>
string(8) "10:00:00"
["publicationStatus"]=>
string(0) ""
}
}
["Event"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#4 (3) {
["#attributes"]=>
array(26) {
["entryType"]=>
string(15) "ExactTimeMarker"
["entryID"]=>
string(1) "1"
["status"]=>
string(8) "Happened"
["timing"]=>
string(4) "None"
["chain"]=>
string(7) "Overlap"
["airDate"]=>
string(10) "2020-04-24"
["airTime"]=>
string(8) "10:00:00"
["cumulativeAirTime"]=>
string(19) "2020-04-24T10:00:00"
["duration"]=>
string(1) "0"
["runtime"]=>
string(1) "0"
["skip"]=>
string(5) "false"
["external"]=>
string(5) "false"
["dontCopyPosition"]=>
string(5) "false"
["fixedPosition"]=>
string(5) "false"
["scheduledTime"]=>
string(8) "10:00:00"
["startOffset"]=>
string(1) "0"
["editCode"]=>
string(1) "0"
["failureCode"]=>
string(1) "0"
["fileName"]=>
string(0) ""
["unscheduledCategoryGroup"]=>
string(0) ""
["guid"]=>
string(36) "84acfcc0-a580-ea11-87c8-000c29a54856"
["runtimeOverride"]=>
string(0) ""
["intro1Override"]=>
string(0) ""
["intro2Override"]=>
string(0) ""
["intro3Override"]=>
string(0) ""
["earlyNextToPlayOverride"]=>
string(0) ""
}
["EventInfo"]=>
object(SimpleXMLElement)#6 (1) {
["ThirdParty"]=>
string(5) "82454"
}
["TimeMarker"]=>
object(SimpleXMLElement)#7 (1) {
["#attributes"]=>
array(1) {
["type"]=>
string(4) "Soft"
}
}
}
[1]=>
object(SimpleXMLElement)#5 (3) {
["#attributes"]=>
array(26) {
["entryType"]=>
string(4) "Song"
["entryID"]=>
string(5) "24955"
["status"]=>
string(8) "Happened"
["timing"]=>
string(4) "None"
["chain"]=>
string(7) "Overlap"
["airDate"]=>
string(10) "2020-04-24"
["airTime"]=>
string(8) "10:00:21"
["cumulativeAirTime"]=>
string(19) "2020-04-24T10:00:00"
["duration"]=>
string(7) "2228.28"
["runtime"]=>
string(7) "2228.19"
["skip"]=>
string(5) "false"
["external"]=>
string(5) "false"
["dontCopyPosition"]=>
string(5) "false"
["fixedPosition"]=>
string(5) "false"
["scheduledTime"]=>
string(8) "10:00:00"
["startOffset"]=>
string(1) "0"
["editCode"]=>
string(1) "8"
["failureCode"]=>
string(1) "0"
["fileName"]=>
string(0) ""
["unscheduledCategoryGroup"]=>
string(0) ""
["guid"]=>
string(36) "517a9ac1-500b-4dfd-8886-186a8a68167d"
["runtimeOverride"]=>
string(0) ""
["intro1Override"]=>
string(0) ""
["intro2Override"]=>
string(0) ""
["intro3Override"]=>
string(0) ""
["earlyNextToPlayOverride"]=>
string(0) ""
}
["EventInfo"]=>
object(SimpleXMLElement)#7 (0) {
}
["ProgramElement"]=>
object(SimpleXMLElement)#6 (0) {
}
}
}
}
["IsExportError"]=>
string(5) "False"
}
Could someone help me to figure out what code I should use?

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.

counting foreach loop items

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"];
?>

Categories