PHP - count unique arrays - count

Array (
[0] => Array ( [0] => b [1] => d)
[1] => Array ( [0] => c [1] => a)
[2] => Array ( [0] => b [1] => d)
[3] => Array ( [0] => a [1] => d)
[4] => Array ( [0] => c )
[5] => Array ( [0] => a [1] => d [2] => e)
[6] => Array ( [0] => d [1] => b)
)
I would like to perform a count on unique inner arrays, so I can get a count similar to:
2 of b,d
1 of c,a
1 of a,d
1 of c
1 of a,d,e
1 of d,b
I looked at "implode" function, but I only get a listing of all values in the inner arrays instead of a count.
foreach ($result_array as &$pair)
{
$pair = implode(', ', $pair);
}

You were so close... Try this:
$original = array (
array ('b', 'd'),
array ('c', 'a'),
array ('b', 'd'),
array ('a', 'd'),
array ('c'),
array ('a', 'd', 'e'),
array ('d', 'b')
);
$result = array();
foreach ($original as $part) {
$key = implode(', ', $part);
if( ! array_key_exists ($key, $result)) {
$result[$key] = 0;
}
$result[$key] = $result[$key] + 1;
}
foreach ($result as $key => $value) {
echo "$value of {$key}<br/>";
}
Output:
2 of b, d
1 of c, a
1 of a, d
1 of c
1 of a, d, e
1 of d, b

Related

Count total number of array inside while loop

Count the array.
Array ( [0] => 4 [id] => 4 [1] => 2019-2020 [fyear] => 2019-2020 [2] => 01-04-2019 [start_date] => 01-04-2019 [3] => 31-03-2020 [end_date] => 31-03-2020 ) Array ( [0] => 5 [id] => 5 [1] => 2020-2021 [fyear] => 2020-2021 [2] => 01-04-2020 [start_date] => 01-04-2020 [3] => 31-03-2021 [end_date] => 31-03-2021 )
Print total number of array i.e
1. we have number of 2 array.
2. I want to print count 2 inside loop.

wp_query and meta_query for repeater field values

I have a word press site with custom fields.
I need to query a repeater field as a table. It looks like that:
| param_1 | param_2 | param_2 | param_4
| 20 | 20 | 20 | 20
| 555 | 680 | 56 | 0
| 5555 | 45 | 56 | 1
| 69 | 0 | 45 | 0
I need to query this repeater to get a result only if it match the query in the same line
My meta query looks like that:
[post_type] => product
[posts_per_page] => -1
[orderby] => title
[order] => ASC
[meta_query] => Array (
[relation] => AND
[0] => Array (
[key] => BBBproduct_params_values_AAA_param_value_0
[value] => 25
[compare] => <=
[type] => NUMERIC
)
[1] => Array (
[key] => BBBproduct_params_values_AAA_param_value_1
[value] => 56
[compare] => >=
[type] => NUMERIC
)
[2] => Array (
[key] => BBBproduct_params_values_AAA_param_value_2
[value] => 56
[compare] => >=
[type] => NUMERIC
)
[3] => Array (
[key] => BBBproduct_params_values_AAA_param_value_3
[value] => 0
[compare] => =
[type] => NUMERIC
)
)
She is created trough this code:
if( $product_search_by_param_active ){
function my_posts_where( $where ){
$where = str_replace("AAA_param_value_", "%_param_value_", $where);
$where = str_replace("meta_key = 'BBBproduct_params_values_", "meta_key LIKE 'product_params_values_", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$range_angine_settings = get_field('param_search_range_settings');
$search_args['meta_query'] = array();
$search_args['meta_query'] ['relation'] = 'AND';
foreach( $range_angine_settings as $rangekey => $search_range ){
${'range-'.$rangekey} = $_GET['range-'.$rangekey]? $_GET['range-'.$rangekey] : '0';
if ( $_GET['range-'.$rangekey] ) $has_range_query = true;
//$pre_prama_value = 'product_params_values_'.strval($rangekey).'_param_value_AAA';
if( isset($_GET['range-'.$rangekey])){
$search_args['meta_query'][] = array(
"key" => 'BBBproduct_params_values_AAA_param_value_'.strval($rangekey),
"value" => $search_range['search_metric_ranges'][${'range-'.$rangekey}]['range_value'],
"compare" => $search_range['product_search_logic'],
'type' => 'NUMERIC'
);
}
}
}
The problem is that this because of the '%' I get result even if the query match not on the same line.
In the example above it is intended not to get any results, but as every condition is met on a different line I do get a result.
Is there a way to create this king of query to give results by line number?
For me I prefer using CMB2 for custom fields
https://github.com/CMB2/CMB2

I have some arrays of questions with each questions contain different types. how to find duplicate values in multidimensional array?

Below I have an array list. How to find duplicate values in multidimensional array?
Array
(
[0] => stdClass Object
(
[q_id] => 1
[pt_type_id] => 1
[pt_shortcode] => e
)
)
Array
(
[0] => stdClass Object
(
[q_id] => 2
[pt_type_id] => 2
[pt_shortcode] => i
)
)
Array
(
[0] => stdClass Object
(
[q_id] => 3
[pt_type_id] => 2
[pt_shortcode] => i
)
)
Array
(
[0] => stdClass Object
(
[q_id] => 4
[pt_type_id] => 1
[pt_shortcode] => e
)
)
Array
(
[0] => stdClass Object
(
[q_id] => 5
[pt_type_id] => 3
[pt_shortcode] => s
)
)
Array
(
[0] => stdClass Object
(
[q_id] => 6
[pt_type_id] => 4
[pt_shortcode] => n
)
)

WC_Order does not return the product id by get_items() [duplicate]

This question already has answers here:
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
Closed 5 years ago.
I have tried to get the product id and the product name by the following code:
if ( $query->have_posts() ) {
$order_id = $query->posts[0]->ID;
$order = new WC_Order( $order_id );
$items = $order->get_items();
}
foreach ( $items as $item ) {
$product_id = $item['product_id'];
$product = wc_get_product( $item_id );
$product_name = $item['name'];
}
In the above code I got the product name, but the it is return 0 for $product_id. Is there any other method for this?
I can't find any solution for this.
My Edited Version:
When I tried this:
$order_id = $query->posts[0]->ID;
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['product_id'];
$product_name = $item['name'];
print_r( $item ); exit;
}
I got this array:
WC_Order_Item_Product Object
(
[extra_data:protected] => Array
(
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data:protected] => Array
(
[order_id] => 684
[name] => Dark Skirt "Erebos" - Large, White
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 19
[subtotal_tax] => 0
[total] => 19
[total_tax] => 0
[taxes] => Array
(
[total] => Array
(
)
[subtotal] => Array
(
)
)
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 1
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
(
[internal_meta_keys:protected] => Array
(
[0] => _order_id
[1] => _name
[2] => _product_id
[3] => _variation_id
[4] => _quantity
[5] => _tax_class
[6] => _subtotal
[7] => _subtotal_tax
[8] => _total
[9] => _total_tax
[10] => _taxes
[11] => _product_id
[12] => _variation_id
[13] => _qty
[14] => _tax_class
[15] => _line_subtotal
[16] => _line_subtotal_tax
[17] => _line_total
[18] => _line_tax
[19] => _line_tax_data
)
[meta_type:protected] => order_item
[object_id_field_for_meta:protected] => order_item_id
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => WC_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
)
[current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
[object_type:WC_Data_Store:private] => order-item-product
)
[meta_data:protected] => Array
(
)
)
Of you see the [data:protected] array, you can see this where the [product_id] is 0. That is the problem I guess. But what is the solution for this?
it should be:
foreach ( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
}

How to get value from multidimensional array

Array ( [0] => Array ( [data] => Array ( [0] => Array ( [like_info] => Array ( [like_count] => 30 ) [comment_info] => Array ( [comment_count] => 6 ) [share_count] => 17 [attachment] => Array ( [description] => Mitkkk kkssk [media] => Array ( [0] => Array ( [src] => com.jpg&cfs=1 ) ) [name] => euch ) [permalink] => example.com/47457343655 [created_time] => 1925 ) ) ) )
how to get value from all the column in variable listed here
like_count
share_count
comment_count
description
src
permalink
created_time
You can make a vector of variables, i mean a single array:
$var = array();
Then you can do a double loop to get all the values of the multi dimensional array and store them on the vector, like:
for($i = 0; i< count($yourarray); i++){
if(is_array($yourarray[i])){
for($j = 0; j< count($yourarray[i]); j++) $var[] = $yourarray[i][j];
}else $var[] = $yourarray[i];
}

Resources