Select the value from an associative massive array - php

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;
}
}
}

Related

How to get the count of each field inside a group tab object

This is my object and I want to get the number of fields in each tab. I'm thinking of skipping the field if its type is a Tab then start counting the fields before the next Type of tab..
Anyway here is my object below:
array(49) {
[0]=>
array(12) {
["key"]=>
string(19) "field_596796ae0c4c4"
["label"]=>
string(7) "GENERAL"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(0)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[1]=>
array(18) {
["key"]=>
string(19) "field_5964e9206df82"
["label"]=>
string(5) "Brand"
["name"]=>
string(5) "brand"
["_name"]=>
string(5) "brand"
["type"]=>
string(8) "taxonomy"
["order_no"]=>
int(1)
["instructions"]=>
string(0) ""
["required"]=>
int(1)
["id"]=>
string(15) "acf-field-brand"
["class"]=>
string(8) "taxonomy"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["taxonomy"]=>
string(8) "pa_brand"
["field_type"]=>
string(6) "select"
["allow_null"]=>
int(0)
["load_save_terms"]=>
int(0)
["return_format"]=>
string(2) "id"
["field_group"]=>
int(165)
["multiple"]=>
int(0)
}
[2]=>
array(12) {
["key"]=>
string(19) "field_596796da0c4c5"
["label"]=>
string(6) "LAUNCH"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(2)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[3]=>
array(15) {
["key"]=>
string(19) "field_5964e99e6df84"
["label"]=>
string(20) "Announced (Globally)"
["name"]=>
string(18) "announced_globally"
["_name"]=>
string(18) "announced_globally"
["type"]=>
string(11) "date_picker"
["order_no"]=>
int(3)
["instructions"]=>
string(23) "Date globally announced"
["required"]=>
int(0)
["id"]=>
string(28) "acf-field-announced_globally"
["class"]=>
string(11) "date_picker"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["date_format"]=>
string(4) "mmyy"
["display_format"]=>
string(5) "MM yy"
["first_day"]=>
int(1)
["field_group"]=>
int(165)
}
or this object that I simplified and run in Javascript:
var productObject = [
{
"type":"tab",
"value":"GENERAL"
},
{
"type":"taxonomy",
"value":"56",
"label":"Brand"
},
{
"type":"tab",
"value":"LAUNCH"
},
{
"type":"text",
"name":"announced_ph",
"label":"Announced(Philippines)",
"value":"07072017"
},{
"type":"text",
"name":"announced_global",
"label":"Announced(Global)",
"value":"09092017"
},{
"type":"tab",
"value":"NETWORK"
},{
"type":"text",
"name":"sim_type",
"label":"SIM",
"value":"dualnano"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
}
];
I need to know the number of fields/objects after an object with a type of "tab". Because I need to know it for the rowspan value of my table.
The ouput I'm thinking should be:
Tab "General" has 1 field
Tab "Launch" has 2 field
Tab "Network" has 3 field
Thanks!
Loop over the object, then just check if the type is equal to tab.
Something like if (productObject.type == 'tab').
If it is equal to tab, set a counter variable to 0, then just increment it until you reach the next tab value.
my_counter = 0
tab_type = ''
for po in productObject:
if po['type'] == 'tab':
my_counter = 0
tab_type = po['value']
my_counter += 1
Something like this.
Using the productObject, I'll based it on the key value
//Getting all "value" keys
var arr = $.map(productObject,function(k,v){
if(k.type == "tab"){
return k.value;
}
});
//Counting each duplicates
var map = arr.reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
//Returns JSON
console.log(JSON.stringify(map));
Sample Output:
{"GENERAL":1,"LAUNCH":1,"NETWORK":1}

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

How to get object like value:item:private from PHP Array

I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.

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.

print PHP value in an array

Hello I am trying to print a specific value from an array of objects. I am trying to get a value from an array name $allPhotos with a object's property of "nme"'s value.
This is what im trying:
echo $allPhotos[0]["nme"];
this is what the array looks like:
var_dump($allPhotos);
array(2) {
[0]=> object(Photo)#1 (10) {
["product"]=> array(5) {
["PKG1"]=> string(4) "6500"
["PKG2"]=> string(4) "9500"
["8x10"]=> string(4) "1500"
["5x7"]=> string(3) "750"
["4x6"]=> string(3) "300"
}
["price"]=> NULL ["sku"]=> string(1) "1"
["nme"]=> string(5) "test1"
["dir"]=> string(51) "http://"
["status"]=> string(1) "1" ["gallery"]=> string(16) "Church Directory"
["galleryCover"]=> string(1) "0"
["family"]=> string(0) ""
["familyCover"]=> string(0) ""
}
[1]=> object(Photo)#2 (10) {
["product"]=> array(5) {
["PKG1"]=> string(4) "6500"
["PKG2"]=> string(4) "9500"
["8x10"]=> string(4) "1500"
["5x7"]=> string(3) "750"
["4x6"]=> string(3) "300"
}
["price"]=> NULL
["sku"]=> string(1) "2"
["nme"]=> string(5) "test2"
["dir"]=> string(51) "http://"
["status"]=> string(1) "1"
["gallery"]=> string(16) "Church Directory"
["galleryCover"]=> string(1) "0"
["family"]=> string(0) ""
["familyCover"]=> string(0) ""
}
}
Thanks in advance!
I believe echo $allPhotos[0]->nme; should work.

Categories