array(4) { [109]=> array(11) { ["ID"]=> string(3) "109" ["name"]=> string(29) "unsplash_52cd96f512830_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1.jpeg" ["title"]=> string(24) "unsplash_52cd96f512830_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [113]=> array(11) { ["ID"]=> string(3) "113" ["name"]=> string(29) "unsplash_52d65d1ebeeb2_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1.jpeg" ["title"]=> string(24) "unsplash_52d65d1ebeeb2_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [132]=> array(11) { ["ID"]=> string(3) "132" ["name"]=> string(29) "unsplash_523ae1f5502d6_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1.jpeg" ["title"]=> string(24) "unsplash_523ae1f5502d6_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [163]=> array(11) { ["ID"]=> string(3) "163" ["name"]=> string(12) "building.jpg" ["path"]=> string(96) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/building.jpg" ["url"]=> string(80) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/building-150x150.jpg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(72) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/building.jpg" ["title"]=> string(8) "building" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } }
I need to get the url in a foreach and i am using this code:
<?php global $post;$medias = rwmb_meta( 'mimo_imgadv', 'type=image_advanced' );?>
<?php } else {$medias = '';}; ?>
<?php if ($medias !== ''){ ?>
<div class="cn_wrapper flexslider" >
<ul id="allimg_id<?php the_Id(); ?>" class="format_images slides">
<?php foreach($medias as $media){
// check if string ends with image extension
if (preg_match('/(\.jpg|\.png|\.bmp)$/', $media['url'])) {
echo '<li class="selector">';
?>
<a class="mimo-link-overimage" href="<?php the_permalink(); ?> " >
<div class="mimo-image-hover"><i class="fa fa-file-o"></i></div>
<?php
echo '<img src="' . $media['url'] . '" alt=""/>';
echo '</a>';
echo '</li>';
// check if there is youtube.com in string
}
}
?>
</ul>
</div>
But it only gives me the last url.
Anyone knows what is happening with the code?
You just need to edit your regex to detect the file types you have in your array. You are looking for types .jpg, .png, and .bmp, but there is only one file in the array of those types. That's why you got only one result. The other three images are .jpeg. Here is the edited code to include the other files:
<?php foreach($medias as $media){
// check if string ends with image extension
if (preg_match('/(\.jpg|\.jpeg|\.png|\.bmp)$/', $media['url'])) {
echo '<li class="selector">';
//remaining code unchanged...
?>
Related
I get response from the server in Simple XML, but I want get to my page only data I need and show this data in html tags. How I can do it?
My request code:
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');
$c = new TopClient;
$c->appkey = '*******';
$c->secretKey = '***************';
$req = new AliexpressAffiliateProductQueryRequest;
$req->setAppSignature("1111");
$req->setFields("sale_price");
$req->setKeywords("car");
$req->setMaxSalePrice(300);
$req->setMinSalePrice(150);
$req->setPageNo(1);
$req->setPageSize(2);
$req->setSort("SALE_PRICE_ASC");
$req->setTargetCurrency("USD");
$req->setTargetLanguage("en");
$req->setTrackingId("trackID");
echo "<pre>";
var_dump($c->execute($req));
echo "</pre>";
Server response in SXML:
object(SimpleXMLElement)#2717 (2) {
["resp_result"]=>
object(SimpleXMLElement)#2486 (3) {
["resp_code"]=>
string(3) "200"
["resp_msg"]=>
string(13) "Call succeeds"
["result"]=>
object(SimpleXMLElement)#2500 (4) {
["current_page_no"]=>
string(1) "1"
["current_record_count"]=>
string(1) "2"
["products"]=>
object(SimpleXMLElement)#2493 (1) {
["product"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#2434 (29) {
["app_sale_price"]=>
string(4) "1.50"
["app_sale_price_currency"]=>
string(3) "USD"
["commission_rate"]=>
string(4) "9.0%"
["discount"]=>
string(3) "80%"
["first_level_category_id"]=>
string(2) "34"
["first_level_category_name"]=>
string(32) "Automobiles, Parts & Accessories"
["hot_product_commission_rate"]=>
string(4) "0.0%"
["lastest_volume"]=>
string(1) "0"
["original_price"]=>
string(4) "7.50"
["original_price_currency"]=>
string(3) "USD"
["product_detail_url"]=>
string(53) "https://www.aliexpress.com/item/1005002897604484.html"
["product_id"]=>
string(16) "1005002897604484"
["product_main_image_url"]=>
string(65) "https://ae04.alicdn.com/kf/H251d6cd1fb6b457f86d9418c8fb69bfc4.jpg"
["product_small_image_urls"]=>
object(SimpleXMLElement)#2431 (1) {
["string"]=>
array(6) {
[0]=>
string(65) "https://ae04.alicdn.com/kf/H251d6cd1fb6b457f86d9418c8fb69bfc4.jpg"
[1]=>
string(65) "https://ae04.alicdn.com/kf/Hef9247dcda18452b86cec742dc6e1512T.jpg"
[2]=>
string(65) "https://ae04.alicdn.com/kf/H0c39946d5ce345feb6f8fe2785157a2aw.jpg"
[3]=>
string(65) "https://ae04.alicdn.com/kf/H46444b25ac824ca48144ce8a41de8df1E.jpg"
[4]=>
string(65) "https://ae04.alicdn.com/kf/Hd5ec838b5f2544b3a21827f2751bcf48y.jpg"
[5]=>
string(65) "https://ae04.alicdn.com/kf/H550518786e1a472ebc1cf0b8cfdc522f2.jpg"
}
}
["product_title"]=>
string(163) "Car Parasol Front Windshield Side Window Cover Interior UV-protective Curtain шторки для автомобиля шторка для автомобилей "
["promotion_link"]=>
string(807) "https://s.click.aliexpress.com/s/4BScZUQRCtnCPCxSv0PKwEvDQD4u6TRQDsTl9yY3LPNpjcFJYJYgdvBwDDpTrktMG0Bk7vaKjL6XyPRVpHoWa7nQNlQFkqJPggCvZt8DKU9umyHh6eIX5opg0KuXEYNPi7ieG5LeqJDbqOacEd03kB7TjoPS5lw5oIH5JFWbhzN203U3K2CHxLHglZ3KrbYkGRGOE1cgm0v2oQRL2P5WzsJEV9UY7mMsJJsfwdoEo4YKzpQ4A6hFKY1Q4wYvOojPSfj2bFrEVwvkPSR5Y6FnaSrCZc6K5dZ2jfPZN2c5Uz6ken8DkMcl0at0TpyWMnasTOgxAit4gFga3NDjwESqiUlIktUrZigriucp0TyWz5DmYrGN654flY9yNmmuVez6QNmDok6kjJHM2iFZeBpJBYwSpaEMGvA21M0pjyJqLzq0lO7917WednH9wn4FJlFBHs5yGVX2PaCvU9EH41B3YaDNHDfKWP9aj0XcvBrV85e7h6Zukmu3erQ1h85q1gaClmOGLuF1rrw8w5b4j4NoikPBrUQlBXFQxH8B6bISevyTUl95U1uWA7Gt4uk4ySEEqlsHQLrcVwcF70ySnxuoDQ5dCf6LjaoOs8SOMJRm6I8SippRCihNz7PiLXmfmhyaTO6Bcff1hHQ4xIa4bwasbnh5IL3kF3MobRZBYCinXezrYyhCxlXrqw1GbFYwJ1dc9NGGSwU33s6x8XBpjUzib5y5uE1NVAwTBFCdwOewHMKpebf9e5El5yOT9IQBp2tp2O7zWr"
["relevant_market_commission_rate"]=>
string(4) "3.0%"
["sale_price"]=>
string(4) "1.50"
["sale_price_currency"]=>
string(3) "USD"
["second_level_category_id"]=>
string(9) "200003411"
["second_level_category_name"]=>
string(20) "Interior Accessories"
["shop_id"]=>
string(9) "912064829"
["shop_url"]=>
string(42) "https://www.aliexpress.com/store/912064829"
["target_app_sale_price"]=>
string(4) "1.50"
["target_app_sale_price_currency"]=>
string(3) "USD"
["target_original_price"]=>
string(4) "7.50"
["target_original_price_currency"]=>
string(3) "USD"
["target_sale_price"]=>
string(4) "1.50"
["target_sale_price_currency"]=>
string(3) "USD"
}
[1]=>
object(SimpleXMLElement)#2432 (29) {
["app_sale_price"]=>
string(4) "1.35"
["app_sale_price_currency"]=>
string(3) "USD"
["commission_rate"]=>
string(4) "9.0%"
["discount"]=>
string(3) "73%"
["first_level_category_id"]=>
string(2) "34"
["first_level_category_name"]=>
string(32) "Automobiles, Parts & Accessories"
["hot_product_commission_rate"]=>
string(4) "0.0%"
["lastest_volume"]=>
string(1) "0"
["original_price"]=>
string(4) "5.00"
["original_price_currency"]=>
string(3) "USD"
["product_detail_url"]=>
string(53) "https://www.aliexpress.com/item/1005003402966179.html"
["product_id"]=>
string(16) "1005003402966179"
["product_main_image_url"]=>
string(65) "https://ae04.alicdn.com/kf/Hd263fbbd1dad4d49a3a3277261c6c408L.jpg"
["product_small_image_urls"]=>
object(SimpleXMLElement)#2431 (1) {
["string"]=>
array(5) {
[0]=>
string(65) "https://ae04.alicdn.com/kf/Hd263fbbd1dad4d49a3a3277261c6c408L.jpg"
[1]=>
string(65) "https://ae04.alicdn.com/kf/H3595b1b670c04d8b95307ad54f30ee5ac.jpg"
[2]=>
string(65) "https://ae04.alicdn.com/kf/Hbc1076a0bbca4f91a60d8eb27f9392547.jpg"
[3]=>
string(65) "https://ae04.alicdn.com/kf/H999a513e61be49d2ad374fa87e4f34897.jpg"
[4]=>
string(65) "https://ae04.alicdn.com/kf/H7e29d629b6d04ec8870f166a71cf87212.jpg"
}
}
["product_title"]=>
string(124) "2022 New Car Interior Accessories Car Perfume Aromatherapy Cute Crown Teddy Bear Air Outlet Plaster Bear Ornament Decoration"
["promotion_link"]=>
string(807) "https://s.click.aliexpress.com/s/4BScZUQRCtnCPCxSv0PKwEvDQD4u6TRQDsTl9yY3LPNpjcFJYJYgdvBwDDpTrktMG0Bk7vaKjL6XyPRVpHoWa7nQNlQFkqJPggCvZt8DKU9umyHh6eIX5opg0KuXEYNPi7ieG5LeqJDbqOacEd03kB7TjoPS5lw5oIH5JFWbhzN203U3K2CHxLHglZ3KrbYkGRGOE1cgm0v2oQRL2P5WzsJEV9UY7mMsJJsfwdoEo4YKzpQ4A6hFKY1Q4wYvOojPSfj2bFrEVwvkPSR5Y6FnaSrCZc6K5dZ2jfPZN2c5Uz6ken8DkMcl0at0TpyYdHF0HQtA8jRRgCMGPA1XaVUh1IydbFgJOCvwetULs4d0J8ib9Xni5t3QrUNsxQ3lSARntkxnshpruYkDmzb3HJ3sf9GyH5rTL3OTC0dpG3XfbyCmQI7sE1rDkdWSBbIvIHJXWaawBxtuhmqr6WfjZDNeYY0JfdgUsh9F8OqLed1uePoqJHK8FdFG6GAnXwZkCbS84viXRSeHP4EjjjC4Z7gtBzNveIjurnHopaqkY4FNXnJzdGJwnudZyXpX7pV7jkaygdr7GnlfsroGM4P1e8GnFgcimWdqfV0vg71vTxlBJmVDSrtmndZVpTXu3dfIZbjuIyWZWwlCrKCxWbqsppQYItz8662L6l9uKNJLIoKipN0ZTyWkGA2XYBg9al3sPUsO39aTK7ZnxOr3SWOk8Zo6Ua3q3v6UemjvLqqruSU46TCFydPTWxg9wegSuYctUTY0MgdLen"
["relevant_market_commission_rate"]=>
string(4) "3.0%"
["sale_price"]=>
string(4) "1.35"
["sale_price_currency"]=>
string(3) "USD"
["second_level_category_id"]=>
string(9) "200003411"
["second_level_category_name"]=>
string(20) "Interior Accessories"
["shop_id"]=>
string(9) "912521256"
["shop_url"]=>
string(42) "https://www.aliexpress.com/store/912521256"
["target_app_sale_price"]=>
string(4) "1.35"
["target_app_sale_price_currency"]=>
string(3) "USD"
["target_original_price"]=>
string(4) "5.00"
["target_original_price_currency"]=>
string(3) "USD"
["target_sale_price"]=>
string(4) "1.35"
["target_sale_price_currency"]=>
string(3) "USD"
}
}
}
["total_record_count"]=>
string(6) "472235"
}
}
["request_id"]=>
string(12) "iirdj5wwp91x"
}
I need for example olny strings: Title["product_title"], Price["sale_price"] and TrackID["promotion_link"], how I can get it's? And show on web page only this data:
<h1>Title</h1>
<h2>Price</h2>
<a href>TrackID</a>
Thanks!
try this.
$content = "xml content";
$xml = new \SimpleXMLElement($content);
foreach($xml->resp_result->result->products->product as $product) {
echo "<h1>" . $product->product_title . "</h1>";
echo "<h2>" . $product->sale_price . "</h2>";
echo "<a href>" . $product->promotion_link . "</a>";
}
if not running or error, check if php-xml module as installed.
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?
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.
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
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"];
?>