Get values from multidimensional array PhP - php

I have an array with products (each product has more arrays)
I want to get the values ["0_1"] or ["0_2"]?
If it starts with 0 it should send that product to the end of the array
array(14) {
["0_1"]=> array(27) {
["files"]=> array(0) { }
["images"]=> array(0) { }
["form_addtocart"]=> string(0) ""
["has_addtocart"]=> bool(false)
["product_flypage"]=> string(137) "index.php?page=shop.product_details&flypage=flypage.tpl&product_id=3299&category_id=88&option=com_virtuemart&Itemid=2"
["product_thumb_image"]=> string(170) "http://www.e-theodoulidis.gr/components/com_virtuemart/show_image_in_imgtag.php?filename=General_Electric_4e5f4d8265044.jpg&newxsize=120&newysize=120&fileout="
["product_full_image"]=> string(108) "http://www.e-theodoulidis.gr/components/com_virtuemart/shop_image/product/General_Electric_4e5f4d8265044.jpg"
["full_image_width"]=> int(1064)
["full_image_height"]=> int(1064)
["product_name"]=> string(42) "General Electric Τηλέγωνο CE 30044"
["product_s_desc"]=> string(0) ""
["product_details"]=> string(43) "Λεπτομέρειες προϊόντος"
["product_rating"]=> string(0) ""
["product_price"]=> string(344) " Καλέστε για Τιμή "
["product_price_raw"]=> array(1) {
["product_price"]=> float(0)
}
["product_sku"]=> string(25) "General Electric CE 30044"
["product_weight"]=> string(6) "0.0000"
["product_weight_uom"]=> string(2) "kg"
["product_length"]=> string(6) "0.0000"
["product_width"]=> string(6) "0.0000"
["product_height"]=> string(6) "0.0000"
["product_lwh_uom"]=> string(6) "inches"
["product_in_stock"]=> string(1) "0"
["product_availability_date"]=> NULL
["product_availability"]=> string(0) ""
["cdate"]=> string(10) "1314868610"
["mdate"]=> string(10) "1314868610"
}
["0_2"]=> array(27) {
["files"]=> array(0) { }
["images"]=> array(0) { }
["form_addtocart"]=> string(0) ""
["has_addtocart"]=> bool(false)
["product_flypage"]=> string(137) "index.php?page=shop.product_details&flypage=flypage.tpl&product_id=7609&category_id=88&option=com_virtuemart&Itemid=2"
["product_thumb_image"]=> string(188) "http://www.e-theodoulidis.gr/components/com_virtuemart/show_image_in_imgtag.php?filename=resized%2FPanasonic________533a7b4f573b0_120x120.jpg&newxsize=120&newysize=120&fileout="
["product_full_image"]=> string(108) "http://www.e-theodoulidis.gr/components/com_virtuemart/shop_image/product/Panasonic________533a7b4f5cba6.jpg"
["full_image_width"]=> int(530)
["full_image_height"]=> int(403)
["product_name"]=> string(39) "Panasonic Τηλέφωνο KX-TS560EX2B"
["product_s_desc"]=> string(0) ""
["product_details"]=> string(43) "Λεπτομέρειες προϊόντος"
["product_rating"]=> string(0) ""
["product_price"]=> string(339) " Καλέστε για Τιμή "
["product_price_raw"]=> array(1) {
["product_price"]=> float(0)
}
["product_sku"]=> string(22) "Panasonic KX-TS560EX2B"
["product_weight"]=> string(6) "0.0000"
["product_weight_uom"]=> string(2) "kg"
["product_length"]=> string(6) "0.0000"
["product_width"]=> string(6) "0.0000"
["product_height"]=> string(6) "0.0000"
["product_lwh_uom"]=> string(6) "inches"
["product_in_stock"]=> string(1) "0"
["product_availability_date"]=> NULL
["product_availability"]=> string(0) ""
["cdate"]=> string(10) "1396341583"
["mdate"]=> string(10) "1426577025"
}
}

Those on the left (eg. "0_1") are the array keys, and the values are the ones on the right.
To get just the keys, use:
$arrayOfKeys = array_keys($array)
or to iterate over each item, use:
foreach ($array as $key => $value) { }

Related

PHP - get values from an array inside an array

Hi I have this huge array:
array(14) {
["imagen-profesor"]=>
array(8) {
["id"]=>
string(15) "imagen-profesor"
["slug"]=>
string(15) "imagen-profesor"
["type"]=>
string(5) "image"
["name"]=>
string(15) "Imagen profesor"
["description"]=>
string(17) "Foto del profesor"
["data"]=>
array(8) {
["slug-pre-save"]=>
string(15) "imagen-profesor"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
["validate"]=>
array(1) {
["url2"]=>
array(3) {
["active"]=>
string(1) "1"
["message"]=>
string(60) "Please enter a valid URL address pointing to the image file."
["suppress_for_cred"]=>
bool(true)
}
}
}
["meta_key"]=>
string(20) "wpcf-imagen-profesor"
["meta_type"]=>
string(8) "usermeta"
}
["grados-academicos"]=>
array(8) {
["id"]=>
string(17) "grados-academicos"
["slug"]=>
string(17) "grados-academicos"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(18) "Grados académicos"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(17) "grados-academicos"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(22) "wpcf-grados-academicos"
["meta_type"]=>
string(8) "usermeta"
}
["experiencia-profesional"]=>
array(8) {
["id"]=>
string(23) "experiencia-profesional"
["slug"]=>
string(23) "experiencia-profesional"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(23) "Experiencia profesional"
["description"]=>
string(46) "Por favor coloca aquí experiencia profesional"
["data"]=>
array(5) {
["slug-pre-save"]=>
string(23) "experiencia-profesional"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(28) "wpcf-experiencia-profesional"
["meta_type"]=>
string(8) "usermeta"
}
["areas-de-interes"]=>
array(8) {
["id"]=>
string(16) "areas-de-interes"
["slug"]=>
string(16) "areas-de-interes"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(18) "Áreas de interés"
["description"]=>
string(22) "Intereses del profesor"
["data"]=>
array(5) {
["slug-pre-save"]=>
string(16) "areas-de-interes"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(21) "wpcf-areas-de-interes"
["meta_type"]=>
string(8) "usermeta"
}
["cursos-relevantes"]=>
array(8) {
["id"]=>
string(17) "cursos-relevantes"
["slug"]=>
string(17) "cursos-relevantes"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(17) "Cursos relevantes"
["description"]=>
string(7) "Cursos "
["data"]=>
array(5) {
["slug-pre-save"]=>
string(17) "cursos-relevantes"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(22) "wpcf-cursos-relevantes"
["meta_type"]=>
string(8) "usermeta"
}
["articulos"]=>
array(8) {
["id"]=>
string(9) "articulos"
["slug"]=>
string(9) "articulos"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(10) "Artículos"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(9) "articulos"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(14) "wpcf-articulos"
["meta_type"]=>
string(8) "usermeta"
}
["areas-academicas"]=>
array(8) {
["id"]=>
string(16) "areas-academicas"
["slug"]=>
string(16) "areas-academicas"
["type"]=>
string(6) "select"
["name"]=>
string(18) "Áreas académicas"
["description"]=>
string(0) ""
["data"]=>
array(6) {
["slug-pre-save"]=>
string(16) "areas-academicas"
["options"]=>
array(12) {
["wpcf-fields-select-option-96129381b0cc289dd28e00d98f8c15b0-1"]=>
array(2) {
["title"]=>
string(23) "Análisis de decisiones"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-f320b41329b42d462bd08eb7961f2c65-1"]=>
array(2) {
["title"]=>
string(17) "Comercialización"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-f6b63eb3926b7de2462225998e09cc13-1"]=>
array(2) {
["title"]=>
string(32) "Control e información directiva"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-b2610684e6f824fd687a74d3be7bcabe-1"]=>
array(2) {
["title"]=>
string(21) "Dirección financiera"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-d2c3f7b635105db769c4772a15d5cec2-1"]=>
array(2) {
["title"]=>
string(25) "Dirección de operaciones"
["value"]=>
string(1) "5"
}
["wpcf-fields-select-option-f14bad56bc815fa8060ae231ccb8a217-1"]=>
array(2) {
["title"]=>
string(22) "Dirección de personal"
["value"]=>
string(1) "6"
}
["wpcf-fields-select-option-c6114cc9883ef2a4c8d23d9985fd41fc-1"]=>
array(2) {
["title"]=>
string(15) "Empresa-familia"
["value"]=>
string(1) "7"
}
["wpcf-fields-select-option-6378fcaa75be017d5cddec1f4c837bba-1"]=>
array(2) {
["title"]=>
string(18) "Entorno económico"
["value"]=>
string(1) "8"
}
["wpcf-fields-select-option-48713712cd807bd0920369faaea72630-1"]=>
array(2) {
["title"]=>
string(26) "Entorno político y social"
["value"]=>
string(1) "9"
}
["wpcf-fields-select-option-35c50a5e264b2951ccbd9a83f0d52185-1"]=>
array(2) {
["title"]=>
string(13) "Factor humano"
["value"]=>
string(2) "10"
}
["wpcf-fields-select-option-bdac1a39ab699e74fbffd3b6a9aa85be-1"]=>
array(2) {
["title"]=>
string(20) "Política de empresa"
["value"]=>
string(2) "11"
}
["default"]=>
string(10) "no-default"
}
["validate"]=>
array(1) {
["required"]=>
array(3) {
["active"]=>
string(1) "1"
["value"]=>
string(4) "true"
["message"]=>
string(25) "Por favor, elige el área"
}
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(21) "wpcf-areas-academicas"
["meta_type"]=>
string(8) "usermeta"
}
["calendario"]=>
array(8) {
["id"]=>
string(10) "calendario"
["slug"]=>
string(10) "calendario"
["type"]=>
string(4) "date"
["name"]=>
string(10) "Calendario"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(10) "calendario"
["placeholder"]=>
string(0) ""
["date_and_time"]=>
string(4) "date"
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(15) "wpcf-calendario"
["meta_type"]=>
string(8) "usermeta"
}
["duracion"]=>
array(8) {
["id"]=>
string(8) "duracion"
["slug"]=>
string(8) "duracion"
["type"]=>
string(8) "textarea"
["name"]=>
string(9) "Duración"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(8) "duracion"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(13) "wpcf-duracion"
["meta_type"]=>
string(8) "usermeta"
}
["fechas"]=>
array(8) {
["id"]=>
string(6) "fechas"
["slug"]=>
string(6) "fechas"
["type"]=>
string(8) "textarea"
["name"]=>
string(6) "Fechas"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(6) "fechas"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(11) "wpcf-fechas"
["meta_type"]=>
string(8) "usermeta"
}
["contacto"]=>
array(8) {
["id"]=>
string(8) "contacto"
["slug"]=>
string(8) "contacto"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(8) "Contacto"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(8) "contacto"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(13) "wpcf-contacto"
["meta_type"]=>
string(8) "usermeta"
}
["areas-academicas-2"]=>
array(8) {
["id"]=>
string(18) "areas-academicas-2"
["slug"]=>
string(18) "areas-academicas-2"
["type"]=>
string(6) "select"
["name"]=>
string(24) "Segunda área académica"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(18) "areas-academicas-2"
["options"]=>
array(12) {
["wpcf-fields-select-option-a84588db3f03c2837345f94adfa18588-1"]=>
array(2) {
["title"]=>
string(23) "Análisis de decisiones"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-5a20ef6e60591caaf5bec848cf43cec0-1"]=>
array(2) {
["title"]=>
string(17) "Comercialización"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-3b00b7d1d1040c63c3512a7e52446443-1"]=>
array(2) {
["title"]=>
string(32) "Control e información directiva"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-da90a64b9b2bf7becfbec27c9c4025da-1"]=>
array(2) {
["title"]=>
string(21) "Dirección financiera"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-65c40a4ea4cde81d6fb7b5bbe1c73282-1"]=>
array(2) {
["title"]=>
string(25) "Dirección de operaciones"
["value"]=>
string(1) "5"
}
["wpcf-fields-select-option-3b7a56cdb15cef8028daf43082269339-1"]=>
array(2) {
["title"]=>
string(22) "Dirección de personal"
["value"]=>
string(1) "6"
}
["wpcf-fields-select-option-6eded9521556a27a4cf4465c82f0a203-1"]=>
array(2) {
["title"]=>
string(15) "Empresa-familia"
["value"]=>
string(1) "7"
}
["wpcf-fields-select-option-5102092609e13386bfadc0ed367591b7-1"]=>
array(2) {
["title"]=>
string(18) "Entorno económico"
["value"]=>
string(1) "8"
}
["wpcf-fields-select-option-e1aadcfe82cc3389a57288a85ed45ff6-1"]=>
array(2) {
["title"]=>
string(26) "Entorno político y social"
["value"]=>
string(1) "9"
}
["wpcf-fields-select-option-6440b6d9a60739b88dacaafae2bccaa9-1"]=>
array(2) {
["title"]=>
string(13) "Factor humano"
["value"]=>
string(2) "10"
}
["wpcf-fields-select-option-2d081231584de301011755851f93b246-1"]=>
array(2) {
["title"]=>
string(20) "Política de empresa"
["value"]=>
string(2) "11"
}
["default"]=>
string(10) "no-default"
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(23) "wpcf-areas-academicas-2"
["meta_type"]=>
string(8) "usermeta"
}
["cargo-profesor"]=>
array(8) {
["id"]=>
string(14) "cargo-profesor"
["slug"]=>
string(14) "cargo-profesor"
["type"]=>
string(6) "select"
["name"]=>
string(14) "Cargo Profesor"
["description"]=>
string(36) "Selecciona un cargo para el profesor"
["data"]=>
array(6) {
["slug-pre-save"]=>
string(14) "cargo-profesor"
["options"]=>
array(5) {
["wpcf-fields-select-option-2a9da69d4d39817ac2a4b687ac49dc27-1"]=>
array(2) {
["title"]=>
string(18) "Director del área"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-b7ae4c59b346a60a6680338c9c44c456-1"]=>
array(2) {
["title"]=>
string(8) "Profesor"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-51a473bf339e2669511accaa90983fb0-1"]=>
array(2) {
["title"]=>
string(17) "Profesor invitado"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-734e5707d3253ded0bf0d972f1c56afe-1"]=>
array(2) {
["title"]=>
string(6) "Decano"
["value"]=>
string(1) "3"
}
["default"]=>
string(10) "no-default"
}
["validate"]=>
array(1) {
["required"]=>
array(3) {
["active"]=>
string(1) "1"
["value"]=>
string(4) "true"
["message"]=>
string(23) "This field is required."
}
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(19) "wpcf-cargo-profesor"
["meta_type"]=>
string(8) "usermeta"
}
["segundo-cargo"]=>
array(8) {
["id"]=>
string(13) "segundo-cargo"
["slug"]=>
string(13) "segundo-cargo"
["type"]=>
string(6) "select"
["name"]=>
string(13) "Segundo cargo"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(13) "segundo-cargo"
["options"]=>
array(5) {
["wpcf-fields-select-option-30ef69a1ba17b84f26edcee1404746f4-1"]=>
array(2) {
["title"]=>
string(18) "Director del área"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-8b8dbd4c42d2e4d61053f442e82a88be-1"]=>
array(2) {
["title"]=>
string(8) "Profesor"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-862f1fbf0e317265cf544b754d6fa3f0-1"]=>
array(2) {
["title"]=>
string(17) "Profesor invitado"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-d49829cdb8f8aaae8af93f30f220ca86-1"]=>
array(2) {
["title"]=>
string(6) "Decano"
["value"]=>
string(1) "4"
}
["default"]=>
string(10) "no-default"
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(18) "wpcf-segundo-cargo"
["meta_type"]=>
string(8) "usermeta"
}
}
I just want to get this value: Análisis de decisiones, Comercialización, Control e información directiva, Dirección financiera, "Dirección de operaciones, Dirección de personal, Empresa-familia, Entorno económico, Entorno político y social, Factor humano, Política de empresa.
I mean what is inside areas-academicas
I'm using nested foreach but nothing:
foreach ($un as $key => $value)
{
if ( $key === "areas-academicas" )
{
echo "Llave: " . $key . " => ";
//echo $value;
foreach ( $value as $key2 => $value2 )
{
echo "Llave2: " . $key2 . "<br />" . "valor2: " . $value2;
foreach ($value2 as $key3 => $value3)
{
echo "Llave3: " . $key3 . "<br />" . "valor3: " . $value3;
}
}
}
}
What could be an easy way to get those values? thanks.
If I don't messed up with your nesting this should work. If it doesn't just fix the path inside the foreach.
$data = array(...); // the array you posted
foreach ($data["areas-academicas"]["data"]["options"] as $area) {
echo $area["title"];
}
The bottom line is you can start the iteration from any level, you don't have to begin at the top necessarily.
Looking at the Array it appears that youre only interested in two of arrays at the top level:
$un["areas-academicas"]
$un["areas-academicas-2"]
Then you are looking for specific values, titles of the options of the data, in the nested structure underneath these two:
$un["areas-academicas"]['data']['options'][....]['title']
You can probably limit the iterations of your loop by using the uniform structure of these arrays. But I will assume you wish to loop through the top level array for keys containing "areas-academicas":
$titles = array();
foreach ($un as $key => $value){
if(strpos($key, 'areas-academicas') == false){ continue; }
foreach($value['data']['options'] as $val){
$titles[] = $val['title'];
}
}
This loop should collect all the option titles from the areas-academicas arrays but is reliant on the idea that the nested structure will be uniform for each instance of the areas-academicas key in the top level array. Hope this helps

WooCommerce Booking - Retrieving specific data in Cart?

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) I get this:
array(1) {
["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 php variables (the booking date, the time and the person):
["date"]=> string(13) "28 août 2016"
["_persons"]=> array(1) { [0]=> int(1) }
["time"]=> string(11) "21 h 30 min"
How can I do to get this specific values from WooCommerce cart object?
Thanks
You could 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]['booking']['date'] . '<br>';
echo $items[0]['booking']['_persons'] . '<br>';
echo $items[0]['booking']['time'] . '<br>';
If not working with a foreach loop it will:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$date = $item['booking']['date'];
$person = $item['booking']['_persons'];
$time = $item['booking']['time'];
}
// displaying values for test
echo 'Booking - Date: ' . $date . '/ Person: ' . $person . '/ Time: ' . $time;
One of these will work, for sure…

Select the value from an associative massive array

I am trying to get some value from the massive array, and there occured some problem.
I have such array:
array(2) {
[0]=>
array(23) {
["ITEM_ID"]=>
int(2331)
["ITEM_TYPE_ID"]=>
string(1) "1"
["CART_ITEM_ID"]=>
string(0) ""
["SAVED_TYPE_ID"]=>
string(0) ""
["ITEM_NAME"]=>
string(17) "TEST STOCK LEVELS"
["ERROR"]=>
string(0) ""
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1122"
}
}
["PROPERTIES_INFO"]=>
array(1) {
[522]=>
array(12) {
["USAGE_TYPE"]=>
string(1) "1"
["CONTROL"]=>
string(7) "LISTBOX"
["TYPE"]=>
string(1) "1"
["NAME"]=>
string(11) "TEST OPTION"
["VALUES"]=>
array(1) {
[0]=>
string(4) "1122"
}
["REQUIRED"]=>
string(1) "1"
["PARENT_PROPERTY_ID"]=>
string(0) ""
["PARENT_VALUE_ID"]=>
string(0) ""
["TEXT"]=>
array(0) {
}
["CONTROL_PRICE"]=>
int(0)
["ORDER"]=>
string(1) "1"
["QUANTITY_ACTION"]=>
string(1) "1"
}
}
["PROPERTIES_PRICE"]=>
float(10)
["PROPERTIES_PERCENTAGE"]=>
int(0)
["PROPERTIES_BUYING"]=>
int(0)
["PROPERTIES_DISCOUNT"]=>
int(0)
["PROPERTIES_MORE"]=>
string(1) "0"
["COMPONENTS"]=>
array(0) {
}
["QUANTITY"]=>
int(11)
["TAX_FREE"]=>
string(1) "0"
["DISCOUNT"]=>
int(1)
["BUYING_PRICE"]=>
string(0) ""
["PRICE_EDIT"]=>
string(1) "0"
["PRICE"]=>
string(5) "10.00"
["CHECK_PRICE"]=>
float(20)
["MAX_ITEM_DISCOUNT"]=>
float(20)
["CART_PRICE"]=>
float(20)
}
[1]=>
array(23) {
["ITEM_ID"]=>
int(2331)
["ITEM_TYPE_ID"]=>
string(1) "1"
["CART_ITEM_ID"]=>
string(0) ""
["SAVED_TYPE_ID"]=>
string(0) ""
["ITEM_NAME"]=>
string(17) "TEST STOCK LEVELS"
["ERROR"]=>
string(0) ""
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1123"
}
}
["PROPERTIES_INFO"]=>
array(1) {
[522]=>
array(12) {
["USAGE_TYPE"]=>
string(1) "1"
["CONTROL"]=>
string(7) "LISTBOX"
["TYPE"]=>
string(1) "1"
["NAME"]=>
string(11) "TEST OPTION"
["VALUES"]=>
array(1) {
[0]=>
string(4) "1123"
}
["REQUIRED"]=>
string(1) "1"
["PARENT_PROPERTY_ID"]=>
string(0) ""
["PARENT_VALUE_ID"]=>
string(0) ""
["TEXT"]=>
array(0) {
}
["CONTROL_PRICE"]=>
int(0)
["ORDER"]=>
string(1) "1"
["QUANTITY_ACTION"]=>
string(1) "1"
}
}
["PROPERTIES_PRICE"]=>
float(20)
["PROPERTIES_PERCENTAGE"]=>
int(0)
["PROPERTIES_BUYING"]=>
int(0)
["PROPERTIES_DISCOUNT"]=>
int(0)
["PROPERTIES_MORE"]=>
string(1) "0"
["COMPONENTS"]=>
array(0) {
}
["QUANTITY"]=>
int(5)
["TAX_FREE"]=>
string(1) "0"
["DISCOUNT"]=>
int(1)
["BUYING_PRICE"]=>
string(0) ""
["PRICE_EDIT"]=>
string(1) "0"
["PRICE"]=>
string(5) "10.00"
["CHECK_PRICE"]=>
float(30)
["MAX_ITEM_DISCOUNT"]=>
float(30)
["CART_PRICE"]=>
float(30)
}
}
Before looping this array, I receive some property_id (1123 for example).
I need to check if this property_id matches this element (1122 in my case)
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1122"
}
}
then select its quantity property (in my case it is this element)
["QUANTITY"]=>
int(11)
The problem is to identify from which arrays select this property, because first I need to compare my received value with existing in array(1122 in my case).
I think you could do it faster with a better organized array. As it is I'll do a loop.
Something like that :
$quantity = 0;
$which_array = -1;
foreach($massive as $key=>$value){
if(isset($value['PROPERTIES_INFO'])){
$copy_properties = $value['PROPERTIES_INFO'];
$property_first = reset($copy_properties);
$id_property = key($property_first);
if($id_property == $what_id_you_are_looking_for){
$quantity = $value['QUANTITY'];//delete one of this
//or I don't know wich quantity you want so I put them both
$quantity = $value['PROPERTIES_INFO'][$id_property ]['QUANTITY_ACTION'];//delete one of this
$which_array = $key;
break;
}
}
}

PHP current() returns false when passing a valid array to it

i have an array with size=1 and when i try to get the current array its returns false but the array has values.
$article=getArticle($id);
if(is_array($article))
{ $article=current($article); }
the getArticle returns multidimensional array and when var_dump before current i can see the array.
If var_dump after current i get false.
if i use the code above (because the array size=1)
if(is_array($article))
{
foreach($article as $k=>$v)
{
$article=$v;
}
}
It works without any problem
Whats wrong with current?
Please help
===============EDIT========================
This is the var_dump before current
I have remove the content and summary because are large texts
array(1) {
[529]=>
array(14) {
["articles_id"]=>
string(3) "529"
["issue"]=>
string(3) "161"
["membership_type"]=>
string(1) "1"
["el"]=>
array(9) {
["title"]=>
string(23) "AUTOBIANCHI A112 ABARTH"
["url"]=>
string(23) "autobianchi-a112-abarth"
["summary"]=>
string(397) " i have remove it
"
["content"]=>
string(11580) " i have remove it too large
"
["meta_keywords"]=>
string(23) "AUTOBIANCHI,A112,ABARTH"
["meta_description"]=>
string(402) " "
["created"]=>
string(10) "1362076380"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
["categories_id"]=>
string(2) "12"
["authors_id"]=>
string(2) "16"
["brands_id"]=>
string(2) "36"
["models_id"]=>
string(3) "206"
["engines_id"]=>
string(3) "174"
["o-categories"]=>
array(1) {
["values"]=>
array(1) {
[12]=>
array(15) {
["categories_id"]=>
string(2) "12"
["object_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "0"
["path"]=>
string(2) "12"
["handler"]=>
string(0) ""
["icon"]=>
string(0) ""
["tpl"]=>
string(12) "articles.php"
["alias"]=>
string(1) "0"
["is_nav"]=>
string(1) "1"
["pos"]=>
string(2) "10"
["depth"]=>
string(1) "0"
["cts"]=>
string(0) ""
["mts"]=>
string(0) ""
["configuration"]=>
string(0) ""
["el"]=>
array(15) {
["categories_id"]=>
string(2) "12"
["lang"]=>
string(2) "el"
["category_name"]=>
string(13) "Classic"
["sub_title"]=>
string(0) ""
["summary"]=>
string(0) ""
["image"]=>
string(0) ""
["html_code"]=>
string(0) ""
["meta_title"]=>
string(0) ""
["url"]=>
string(13) "classic"
["url_path"]=>
string(13) "classic"
["meta_keywords"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["created"]=>
string(10) "1355235888"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-authors"]=>
array(1) {
["values"]=>
array(1) {
[16]=>
array(4) {
["authors_id"]=>
string(2) "16"
["object_id"]=>
string(2) "26"
["image"]=>
string(0) ""
["el"]=>
array(9) {
["authors_id"]=>
string(2) "16"
["lang"]=>
string(2) "el"
["name"]=>
string(31) "Last First"
["last_name"]=>
string(10) "Last"
["first_name"]=>
string(20) "First"
["nick_name"]=>
string(0) ""
["created"]=>
string(10) "1360567827"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-brands"]=>
array(1) {
["values"]=>
array(1) {
[36]=>
array(6) {
["brands_id"]=>
string(2) "36"
["object_id"]=>
string(2) "17"
["brand_name"]=>
string(11) "AUTOBIANCHI"
["created"]=>
string(10) "1363179463"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-models"]=>
array(1) {
["values"]=>
array(1) {
[206]=>
array(7) {
["models_id"]=>
string(3) "206"
["object_id"]=>
string(2) "18"
["brands_id"]=>
string(2) "36"
["model_name"]=>
string(11) "A112 ABARTH"
["created"]=>
string(10) "1363179480"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-engines"]=>
array(1) {
["values"]=>
array(1) {
[174]=>
array(6) {
["engines_id"]=>
string(3) "174"
["object_id"]=>
string(2) "19"
["engine"]=>
string(5) "1.050"
["created"]=>
string(10) "1363179448"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
}
Can you try
$article=getArticle($id);
if (is_array($article)) {
$article = reset($article);
}
or
$article=getArticle($id);
if (is_array($article)) {
$article = array_shift($article);
}
You can look into the documentation about reset and the documentation about array_shift.
If you copy array in getArticle() function then its internal pointer is lost.
I think this is the reason, it returns false.

Retrieve values from view in PHP module development

I have retrieve a view programmaticly in my module with PHP. How can retrieve the values of this view? I'm using drupal 6.
That's the view result in my PHP code:
enter code hereobject(view)#48 (22) {
["db_table"]=>
string(10) "views_view"
["base_table"]=>
string(4) "node"
["args"]=>
array(0) {
}
["use_ajax"]=>
bool(false)
["result"]=>
array(0) {
}
["pager"]=>
array(5) {
["use_pager"]=>
bool(false)
["items_per_page"]=>
int(10)
["element"]=>
int(0)
["offset"]=>
int(0)
["current_page"]=>
int(0)
}
["old_view"]=>
array(0) {
}
["vid"]=>
string(2) "48"
["name"]=>
string(7) "student"
["description"]=>
string(7) "Student"
["tag"]=>
string(0) ""
["view_php"]=>
string(0) ""
["is_cacheable"]=>
string(1) "0"
["display"]=>
array(2) {
["default"]=>
object(views_display)#11 (7) {
["db_table"]=>
string(13) "views_display"
["vid"]=>
string(2) "48"
["id"]=>
string(7) "default"
["display_title"]=>
string(8) "Defaults"
["display_plugin"]=>
string(7) "default"
["position"]=>
string(1) "1"
["display_options"]=>
array(2) {
["fields"]=>
array(5) {
["field_locatie_student_lid"]=>
array(14) {
["label"]=>
string(7) "Locatie"
["alter"]=>
array(16) {
["alter_text"]=>
int(0)
["text"]=>
string(0) ""
["make_link"]=>
int(0)
["path"]=>
string(0) ""
["link_class"]=>
string(0) ""
["alt"]=>
string(0) ""
["prefix"]=>
string(0) ""
["suffix"]=>
string(0) ""
["target"]=>
string(0) ""
["help"]=>
string(0) ""
["trim"]=>
int(0)
["max_length"]=>
string(0) ""
["word_boundary"]=>
int(1)
["ellipsis"]=>
int(1)
["html"]=>
int(0)
["strip_tags"]=>
int(0)
}
["empty"]=>
string(41) "geen overeenkomstige resultaten gevonden."
["hide_empty"]=>
int(0)
["empty_zero"]=>
int(0)
["link_to_node"]=>
int(0)
["label_type"]=>
string(6) "widget"
["format"]=>
string(7) "default"
["multiple"]=>
array(4) {
["group"]=>
bool(true)
["multiple_number"]=>
string(0) ""
["multiple_from"]=>
string(0) ""
["multiple_reversed"]=>
bool(false)
}
["exclude"]=>
int(0)
["id"]=>
string(25) "field_locatie_student_lid"
["table"]=>
string(31) "node_data_field_locatie_student"
["field"]=>
string(25) "field_locatie_student_lid"
["relationship"]=>
string(4) "none"
}
["field_naam_student_value"]=>
array(14) {
["label"]=>
string(4) "Naam"
["alter"]=>
array(16) {
["alter_text"]=>
int(0)
["text"]=>
string(0) ""
["make_link"]=>
int(0)
["path"]=>
string(0) ""
["link_class"]=>
string(0) ""
["alt"]=>
string(0) ""
["prefix"]=>
string(0) ""
["suffix"]=>
string(0) ""
["target"]=>
string(0) ""
["help"]=>
string(0) ""
["trim"]=>
int(0)
["max_length"]=>
string(0) ""
["word_boundary"]=>
int(1)
["ellipsis"]=>
int(1)
["html"]=>
int(0)
["strip_tags"]=>
int(0)
}
["empty"]=>
string(41) "geen overeenkomstige resultaten gevonden."
["hide_empty"]=>
int(0)
["empty_zero"]=>
int(0)
["link_to_node"]=>
int(0)
["label_type"]=>
string(6) "widget"
["format"]=>
string(7) "default"
["multiple"]=>
array(4) {
["group"]=>
bool(true)
["multiple_number"]=>
string(0) ""
["multiple_from"]=>
string(0) ""
["multiple_reversed"]=>
bool(false)
}
["exclude"]=>
int(0)
["id"]=>
string(24) "field_naam_student_value"
["table"]=>
string(28) "node_data_field_naam_student"
["field"]=>
string(24) "field_naam_student_value"
["relationship"]=>
string(4) "none"
}
["field_ugentid_student_value"]=>
array(14) {
["label"]=>
string(7) "UGentID"
["alter"]=>
array(16) {
["alter_text"]=>
int(0)
["text"]=>
string(0) ""
["make_link"]=>
int(0)
["path"]=>
string(0) ""
["link_class"]=>
string(0) ""
["alt"]=>
string(0) ""
["prefix"]=>
string(0) ""
["suffix"]=>
string(0) ""
["target"]=>
string(0) ""
["help"]=>
string(0) ""
["trim"]=>
int(0)
["max_length"]=>
string(0) ""
["word_boundary"]=>
int(1)
["ellipsis"]=>
int(1)
["html"]=>
int(0)
["strip_tags"]=>
int(0)
}
["empty"]=>
string(41) "geen overeenkomstige resultaten gevonden."
["hide_empty"]=>
int(0)
["empty_zero"]=>
int(0)
["link_to_node"]=>
int(0)
["label_type"]=>
string(6) "widget"
["format"]=>
string(7) "default"
["multiple"]=>
array(4) {
["group"]=>
bool(true)
["multiple_number"]=>
string(0) ""
["multiple_from"]=>
string(0) ""
["multiple_reversed"]=>
bool(false)
}
["exclude"]=>
int(0)
["id"]=>
string(27) "field_ugentid_student_value"
["table"]=>
string(31) "node_data_field_ugentid_student"
["field"]=>
string(27) "field_ugentid_student_value"
["relationship"]=>
string(4) "none"
}
["field_voornaam_student_value"]=>
array(14) {
["label"]=>
string(8) "Voornaam"
["alter"]=>
array(16) {
["alter_text"]=>
int(0)
["text"]=>
string(0) ""
["make_link"]=>
int(0)
["path"]=>
string(0) ""
["link_class"]=>
string(0) ""
["alt"]=>
string(0) ""
["prefix"]=>
string(0) ""
["suffix"]=>
string(0) ""
["target"]=>
string(0) ""
["help"]=>
string(0) ""
["trim"]=>
int(0)
["max_length"]=>
string(0) ""
["word_boundary"]=>
int(1)
["ellipsis"]=>
int(1)
["html"]=>
int(0)
["strip_tags"]=>
int(0)
}
["empty"]=>
string(41) "geen overeenkomstige resultaten gevonden."
["hide_empty"]=>
int(0)
["empty_zero"]=>
int(0)
["link_to_node"]=>
int(0)
["label_type"]=>
string(6) "widget"
["format"]=>
string(7) "default"
["multiple"]=>
array(4) {
["group"]=>
bool(true)
["multiple_number"]=>
string(0) ""
["multiple_from"]=>
string(0) ""
["multiple_reversed"]=>
bool(false)
}
["exclude"]=>
int(0)
["id"]=>
string(28) "field_voornaam_student_value"
["table"]=>
string(32) "node_data_field_voornaam_student"
["field"]=>
string(28) "field_voornaam_student_value"
["relationship"]=>
string(4) "none"
}
["field_voorkeur_student_value"]=>
array(14) {
["label"]=>
string(19) "Voorkeurstageplaats"
["alter"]=>
array(16) {
["alter_text"]=>
int(0)
["text"]=>
string(0) ""
["make_link"]=>
int(0)
["path"]=>
string(0) ""
["link_class"]=>
string(0) ""
["alt"]=>
string(0) ""
["prefix"]=>
string(0) ""
["suffix"]=>
string(0) ""
["target"]=>
string(0) ""
["help"]=>
string(0) ""
["trim"]=>
int(0)
["max_length"]=>
string(0) ""
["word_boundary"]=>
int(1)
["ellipsis"]=>
int(1)
["html"]=>
int(0)
["strip_tags"]=>
int(0)
}
["empty"]=>
string(41) "geen overeenkomstige resultaten gevonden."
["hide_empty"]=>
int(0)
["empty_zero"]=>
int(0)
["link_to_node"]=>
int(0)
["label_type"]=>
string(6) "widget"
["format"]=>
string(7) "default"
["multiple"]=>
array(4) {
["group"]=>
bool(true)
["multiple_number"]=>
string(0) ""
["multiple_from"]=>
string(0) ""
["multiple_reversed"]=>
bool(false)
}
["exclude"]=>
int(0)
["id"]=>
string(28) "field_voorkeur_student_value"
["table"]=>
string(32) "node_data_field_voorkeur_student"
["field"]=>
string(28) "field_voorkeur_student_value"
["relationship"]=>
string(4) "none"
}
}
["filters"]=>
array(2) {
["type"]=>
array(9) {
["operator"]=>
string(2) "in"
["value"]=>
array(1) {
["student"]=>
string(7) "student"
}
["group"]=>
string(1) "0"
["exposed"]=>
bool(false)
["expose"]=>
array(2) {
["operator"]=>
bool(false)
["label"]=>
string(0) ""
}
["id"]=>
string(4) "type"
["table"]=>
string(4) "node"
["field"]=>
string(4) "type"
["relationship"]=>
string(4) "none"
}
["field_ugentid_student_value"]=>
array(9) {
["operator"]=>
string(9) "not empty"
["value"]=>
array(3) {
["value"]=>
string(0) ""
["min"]=>
string(0) ""
["max"]=>
string(0) ""
}
["group"]=>
string(1) "0"
["exposed"]=>
bool(false)
["expose"]=>
array(2) {
["operator"]=>
bool(false)
["label"]=>
string(0) ""
}
["id"]=>
string(27) "field_ugentid_student_value"
["table"]=>
string(31) "node_data_field_ugentid_student"
["field"]=>
string(27) "field_ugentid_student_value"
["relationship"]=>
string(4) "none"
}
}
}
}
["page_1"]=>
object(views_display)#43 (7) {
["db_table"]=>
string(13) "views_display"
["vid"]=>
string(2) "48"
["id"]=>
string(6) "page_1"
["display_title"]=>
string(6) "Pagina"
["display_plugin"]=>
string(4) "page"
["position"]=>
string(1) "2"
["display_options"]=>
array(1) {
["path"]=>
string(17) "studentenLijstCSV"
}
}
}
["type"]=>
string(6) "Normal"
["loaded"]=>
bool(true)
["executed"]=>
bool(false)
["built"]=>
bool(false)
["build_info"]=>
array(0) {
}
["attachment_before"]=>
string(0) ""
["attachment_after"]=>
string(0) ""
["get_total_rows"]=>
bool(true)
}
<div style="display: none" id="drupalforfirebug_general"><fieldset><legend>Drupal for Firebug General Messages</legend>There were no messages sent to the general log. Please use "firep($item, $optional_title)" to output messages to this console.</div><div style="display: none" id="drupalforfirebug_sql"><fieldset><legend>Devel Module is Not Installed</legend>Please install and enable the Devel Module to display the SQL queries.</fieldset></div><div style="display: none" id="drupalforfirebug_hook_form_alter">There was no form altering.</div><div style="display: none" id="drupalforfirebug_hook_user">There was no user processing.</div><div style="display: none" id="drupalforfirebug_hook_nodeapi">There was no node processing.</div><div style="display: none" id="drupalforfirebug_hook_views">There was no views processing.</div><div style="display: none" id="drupalforfirebug_php"><object style="width:100%;frameborder=0;height=100%;margin-bottom:-3px;" type="text/html" data="http://localhost/testplanning/?q=admin/firebug/exec"></object></div><div style="display: none" id="drupalforfirebug_hook_page_alter">This feature is only available in Drupal 7.</div>
why you not using just views?
$view = view_get_view('MY_VIEW_NAME');
$view->set_display('MY_DISPLAY'); // page_1 or block_1 ... block_n, ...page_n
$view->set_items_per_page(0);
$view->execute();
$result = $view->result;
$result will contains array of values of each row
I haven't actually tried this myself. First thing to do would be to execute the views query. This can be done with the execute function.
So doing something like this:
$view->execute('display_id');
Should populate the $view object with at least some data. The data available differs through the different layers of theme functions, so you might not get what you want. But this is the best method I can think of.

Categories