I have this multidimensional array:
Array
(
[car] => Array
(
[responsecode] => 200
[ford] => Array
(
[start] => 0
[count] => 20
[model] => 972000
[results] => Array
(
[0] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => xx
[abstract] => xx
)
[1] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => Txx
[abstract] => xx
)
I need retrieve value from [model] (972000)
Its really hard for me. Thanks in advance!
if you have your array assigned to a variable say $arry then it would be
$arry['car']['ford']['model']
there are other techniques to get the 'model' from every 'car' in an array, is that what you are looking for?
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
http://php.net/manual/en/language.types.array.php
Related
I am on functions.php and I want to add this filter:
function graphene_alter_the_query( $request ) {
$dummy_query = new WP_Query();
$dummy_query->parse_query( $request );
if (!is_category('25')) {
$request['gdsr_sort'] = 'rating';
}
else {
$request['gdsr_sort'] = 'comment_count';
}
return $request;
}
add_filter( 'request', 'graphene_alter_the_query' );
the filter should do the same thing for all categories except cat 25 but seems like it cant tell it apart like if the IF condition is not detecting the category so I tried to debug it by looking at the values in WP_Query and I get this:
$myquery = new WP_Query(); print_r($myquery);
WP_Query Object ( [query] => Array ( [category_name] => 'my cat' ) [query_vars] => Array ( [category_name] => 'my cat' [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 'my cat' ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => 'my cat' ) [field] => slug ) ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_favicon] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 6d85cdf18f3e6a58a470a3002d5807fc [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] =>
but both tag_id and cat appear empty. Why is that?
I have a function that it is supposed to create a variation and fill the variation with the values, the attributes of the variable product. To become more flexible i didn't create values for the products attributes besides
I have tried a hundred ways the make the values stick to the variation but they dont. The variation is created in the backend but it has none of the values atributed?????? PLEEEEASE HELP!!!! Im at this for 4 days now. ;(
The only values attributed to the product's atributes is ">>EMPTY<<" value. ///////////// Variables $data -> Array (Key -> Value), so that the keys == to the products atributes $product -> $product_id
function add_variable_product_to_cart($product_id, $data) {
$my_post = array(
'post_title'=> 'Order_Printaway',
'post_name' => 'product_' . $data["itemIdentifier"],
'post_status' => 'publish',
'post_parent' => $product_id ,//post is a child post of product post
'post_type' => 'product_variation',//set post type to product_variation
'guid'=>home_url() . '/?product_variation=product_' . $data["itemIdentifier"]
);
//Insert ea. post/variation into database:
$attID = wp_insert_post( $my_post );
update_post_meta($attID, '_price', $data["final_price"]);
update_post_meta($attID, '_regular_price', $data["final_price"]);
foreach($data as $key => $value){
$avail_attributes = array($value);
wp_set_object_terms($product_id, $avail_attributes, $key);
$thedata[$key]['name'] = $value;
}
update_post_meta( $attID,'_product_attributes',$thedata);
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
else{
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
}
///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// OUTPUT //////////////////////////////////////////////////////////////////////
//Atributes of the Product ->
Array ( [a_color] => Array ( [name] => a_color [value] => >>EMPTY<< [position] => 0 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_creasing] => Array ( [name] => a_creasing [value] => >>EMPTY<< [position] => 1 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing] => Array ( [name] => a_finishing [value] => >>EMPTY<< [position] => 2 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing_desc] => Array ( [name] => a_finishing_desc [value] => >>EMPTY<< [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_format_dir] => Array ( [name] => a_format_dir [value] => >>EMPTY<< [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_page] => Array ( [name] => a_page [value] => >>EMPTY<< [position] => 5 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper] => Array ( [name] => a_paper [value] => >>EMPTY<< [position] => 6 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper_desc] => Array ( [name] => a_paper_desc [value] => >>EMPTY<< [position] => 7 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_perforation] => Array ( [name] => a_perforation [value] => >>EMPTY<< [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_processing] => Array ( [name] => a_processing [value] => >>EMPTY<< [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [product] => Array ( [name] => product [value] => >>EMPTY<< [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [shipping] => Array ( [name] => shipping [value] => >>EMPTY<< [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) )
////Array of data that i am trying to introduce in the Variation
Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) Atributes of the variation after get_post_meta(757, '_product_attributes', true)) Array ( [0] => Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) )
I struggled with same issue and the key was in attributes' taxonomies.
If attribute marked as taxonomy then it requires slug instead of string for text based attrs. So firstly check
if ( $attribute['is_taxonomy'] ) {
$options = wc_get_product_terms( $post_id, $attribute['name'], array( 'fields' => 'slugs' ) );
} else {
$options = explode( WC_DELIMITER, $attribute['value'] );
}
and fill $attr_val with slug or string value.
update_post_meta( $post_id, 'attribute_' . sanitize_title($attribute['name'] ), $attr_val);
WC_AJAX::save_attributes() may be helpful
my content type, plan to have one or more 'maps'.
With the following php code, I can take all their link:
<?php print render($content['field_mappe_di_riferimento']); ?>
and I get the following result:
I would like to put before the link the image that is within the content type "Mappa", entered in the field "field_immagine_min."
this is the structure of my content type:
this is the content type "Mappa":
with: "print_r($field_mappe_di_riferimento);" I get it:
Array ( [0] => Array ( [nid] => 30 [access] => 1 [node] => stdClass Object ( [vid] => 30 [uid] => 1 [title] => Carta di Marignano, primo giorno, 13 Settembre 1515 [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 30 [type] => mappa [language] => en [created] => 1352066743 [changed] => 1352127342 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1352127342 [revision_uid] => 1 [field_battaglia_di_riferimento] => Array ( [und] => Array ( [0] => Array ( [nid] => 2 ) ) ) [field_immagine_std] => Array ( [und] => Array ( [0] => Array ( [fid] => 23 [alt] => [title] => [width] => 672 [height] => 554 [uid] => 1 [filename] => polesella.jpg [uri] => public://polesella_0.jpg [filemime] => image/jpeg [filesize] => 337755 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_immagine_min] => Array ( [und] => Array ( [0] => Array ( [fid] => 24 [alt] => [title] => [width] => 150 [height] => 110 [uid] => 1 [filename] => polesella_small.jpg [uri] => public://polesella_small.jpg [filemime] => image/jpeg [filesize] => 10672 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_testo_opzionale] => Array ( [und] => Array ( [0] => Array ( [value] => pisello [format] => [safe_value] => pisello ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item 1 => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date 1 => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1352066743 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => ant [picture] => 0 [data] => b:0; ) ) )
so I tried to take the nid of the node that I want to take the picture, with success:
print ($content['field_mappe_di_riferimento']['#items'][0][nid]);
I get 30...
I found the solution:
$array = $content['field_mappe_di_riferimento']['#items'];
$k = 0;
foreach($array as $k => $v){
print($k);
//carico il nodo
$node = node_view(node_load($content['field_mappe_di_riferimento']['#items'][$k]['nid']));
//stampa l'img del nodo
print render($node['field_immagine_min']);
}
I have two model name image and comment. here the relation is like image has many comments . Now in my listing page I want to display all imae detail and only the no of comment on that image . can you tell me how should I get that ?
after I write the query my return data is
Array
(
[0] => Array
(
[Image] => Array
(
[image_id] => 57
[user_id] => 1
[category_id] => 22
[image_title] => scroul
[description] => beutifull natural image for the animal
[keyword] => scrual
[image_price] =>
[image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 12
[size] => 3244
[resolution] => 2162 x 1644
[i_date] => 1348573022
[i_by] => 1
[u_date] => 1348573022
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 5
[image_id] => 57
[user_id] => 2
[comment] => socute
[comment_date] => 1348739230
[is_active] => N
[is_deleted] => N
)
)
)
[1] => Array
(
[Image] => Array
(
[image_id] => 56
[user_id] => 1
[category_id] => 22
[image_title] => cute dog
[description] => cute dog looking
[keyword] =>
[image_price] =>
[image_name] => d4af899b0d52cccbec94952a3abd0077.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 8
[size] => 620
[resolution] => 2592 x 1944
[i_date] => 1348572897
[i_by] => 1
[u_date] => 1348572897
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 3
[image_id] => 56
[user_id] => 2
[comment] => ohhhhhhh
[comment_date] => 1348737968
[is_active] => N
[is_deleted] => N
)
)
)
[3] => Array
(
[Image] => Array
(
[image_id] => 55
[user_id] => 1
[category_id] => 22
[image_title] => ships
[description] => ships with beutiful green background
[keyword] => ship,green,animal,nature,background,eating,white ship
[image_price] =>
[image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 1
[size] => 1831
[resolution] => 2520 x 1944
[i_date] => 1348572846
[i_by] => 1
[u_date] => 1348661976
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 2
[image_id] => 55
[user_id] => 2
[comment] => i like it
[comment_date] => 1348737942
[is_active] => Y
[is_deleted] => N
)
[1] => Array
(
[comment_id] => 4
[image_id] => 55
[user_id] => 2
[comment] => good scene
[comment_date] => 1348738004
[is_active] => N
[is_deleted] => N
)
)
)
)
in the above array there is a all comment of that image. I don't want here comment list I just want no of comments .
Could you post the find statement you're using. You can set recursive to a certain level, from the documentation:
-1 Cake fetches Group data only, no joins.
0 Cake fetches Group data and its domain
1 Cake fetches a Group, its domain and its associated Users
2 Cake fetches a Group, its domain, its associated Users, and the Users’ associated Articles
So you can call the following if you only want the data from the current model only:
$this->Model->find('all', array('recursive' => -1));
There is also the Containable behavior, which allows you to specify which Model data you want to retrieve when calling find.
Say you have a Post model which hasMany Image. The call, with the Containable behaviour in the Post Model properly included, would be:
$this->Post->find('all', array(
'conditions' => array('Post.id' => 1),
'contain' => array('Image')
));
EDIT:
Because of the spelling and formatting I misread your initial question. I thought you wanted "no comments" to appear in the data array, instead you want only the "number of comments" to appear.
If you want the comment count, use counterCache as Kishor Kundan proposes.
Cake offers another amazing magic, "counterCache".
You can define the counterCache in your Comments model
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true
);
Then you add a field, 'comment_count' in your images table (or the table which is being used by the model Image) and the cake will do the rest for you.
This does add an overhead every time a comment is added/deleted but it is far better alternative than to issue an 'count' every time you fetch image data.
For more info you can check the cookbook. Look out for "counterCache" there.
UPDATE:
To limit the scope for counter cache, use additional attribute 'counterScope' as
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true,
'counterScope' => array('Image.active' => 1)
);
I need to hide only one of the 2 children of a fieldset element.
I have an issue with the assigning the #type='hidden' value to 2 children elements of a fieldset.
In other words I have:
Array
(
[4] => Array
(
[#type] => hidden
[#title] => Distribution Product
[#default_value] => Array
(
)
[#options] => Array
(
[] => - None selected -
[0] => stdClass Object
(
[option] => Array
(
[38] => Cosmetics
)
)
[1] => stdClass Object
(
[option] => Array
(
[39] => Hair
)
)
)
[#description] =>
[#multiple] => 0
[#size] => 0
[#weight] => 0
[#theme] => taxonomy_term_select
[#required] => 0
)
[1] => Array
(
[#type] => select
[#title] => Product
[#default_value] => Array
(
[0] => 26
)
[#options] => Array
(
[] => - None selected -
[0] => stdClass Object
(
[option] => Array
(
[28] => Cosmetics
)
)
[1] => stdClass Object
(
[option] => Array
(
[26] => -Joelle Ciocco
)
)
)
[#description] =>
[#multiple] => 0
[#size] => 0
[#weight] => 0
[#theme] => taxonomy_term_select
[#required] => 0
)
[#type] => fieldset
[#title] => Vocabularies
[#collapsible] => 1
[#collapsed] =>
[#weight] => -3
[#tree] => 1
)
I'm currectly assigning
$form['taxonomy'][4]['#type'] = 'hidden';
$form['taxonomy'][1]['#type'] = 'hidden';
But they are both visible.
Of course if I hide
$form['taxonomy']['#type'] = 'hidden';
it works and I hide everything.
thanks
What if you try
unset($form['taxonomy'][4]);