تابع ووکامرسی wc_array_cartesian پیدا کردن تمامی داده های ترکیبی آرایه

تابع ووکامرسی wc_array_cartesian – استخراج دادهای ترکیبی آرایه ای woocommerce
Syntax – سینتکس
(array) wc_array_cartesian( (array) $input );
Parameters – پارامتر ها (1)
- 1- $input (array)
Usage – نحوه استفاده
if ( !function_exists( 'wc_array_cartesian' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php';
}
// The input.
$input = array();
// NOTICE! Understand what this does before running.
$result = wc_array_cartesian($input);
Defined – محل تعریف
/includes/wc-core-functions.php
function wc_array_cartesian( $input ) {
$input = array_filter( $input );
$results = array();
$indexes = array();
$index = 0;
// Generate indexes from keys and values so we have a logical sort order
foreach ( $input as $key => $values ) {
foreach ( $values as $value ) {
$indexes[ $key ][ $value ] = $index++;
}
}
// Loop over the 2D array of indexes and generate all combinations
foreach ( $indexes as $key => $values ) {
// When result is empty, fill with the values of the first looped array
if ( empty( $results ) ) {
foreach ( $values as $value ) {
$results[] = array( $key => $value );
}
// Second and subsequent input sub-array merging.
} else {
foreach ( $results as $result_key => $result ) {
foreach ( $values as $value ) {
// If the key is not set, we can set it
if ( ! isset( $results[ $result_key ][ $key ] ) ) {
$results[ $result_key ][ $key ] = $value;
// If the key is set, we can add a new combination to the results array
} else {
$new_combination = $results[ $result_key ];
$new_combination[ $key ] = $value;
$results[] = $new_combination;
}
}
}
}
}
// Sort the indexes
arsort( $results );
// Convert indexes back to values
foreach ( $results as $result_key => $result ) {
$converted_values = array();
// Sort the values
arsort( $results[ $result_key ] );
// Convert the values
foreach ( $results[ $result_key ] as $key => $value ) {
$converted_values[ $key ] = array_search( $value, $indexes[ $key ] );
}
$results[ $result_key ] = $converted_values;
}
return $results;
}
versions – نسخه ها
از نسخه : 2.5.0
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر