这是我从数组循环和输出数据的最远的一次。它可能触及了某些人的表面,但我仍然在思考使用数组的基本概念。
我有一个包含两个主要集合的多维数组,每个集合嵌套 5 深。
我已经格式化了数组的var_dump();
- 所以我可以更清楚地看到发生了什么。
Array (
'set_56aad4d86660c' => Array ( // first foreach
'conditions' => Array ( // second foreach
'1' => Array (
'type' => apply_to
'args' => Array (
'applies_to' => roles
'roles' => Array (
'0' => administrator
)
)
)
)
'rules' => Array (
'1' => Array (
'from' => 1
'to' => 4
'type' => fixed_price
'amount' => 10
)
)
)
'set_56aad4d867064' => Array (
'conditions' => Array (
'1' => Array (
'type' => apply_to
'args' => Array (
'applies_to' => roles
'roles' => Array (
'0' => trader
)
)
)
)
'rules' => Array (
'1' => Array (
'from' => 5
'to' => 10
'type' => fixed_price
'amount' => 5
)
)
)
)
我在循环遍历和打印数组中的任何值时没有问题,我已经确保我可以打印所有值。
我被困在哪里
我想做的是 - 当数组集具有管理员角色时,仅输出其相应数组中的值。
即"规则"数组中的数据 - "从"、"到"、"金额"。
我可以打印所有集合的详细信息,但不是基于条件(当前用户(
这是我到目前为止所处的位置
// Pull the data
global $post, $product;
// get the current user's details
$current_user = wp_get_current_user();
// Loop through main rule sets array
$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
// Check to see if there are rule sets
if ($array_rule_sets && is_array($array_rule_sets) && sizeof($array_rule_sets) > 0) {
// Loop through rule sets
foreach ( $array_rule_sets as $rule_set ) {
// Get conditions
$conditions = $rule_set['conditions'];
// Loop through conditions
foreach ( $conditions as $key => $condition ) {
// Get Roles
$roles = $condition['args']['roles'];
// Loop through roles
foreach ( $roles as $role ) {
if ( in_array( $role, (array)$current_user->roles ) ) {
$role_rules[] = $key; // getting stuck here
}
}
// Loop through rules array and print pricing table values based on key
foreach ( $rule_set['rules'] as $key => $value ) {
$tempstring .= $rule_set['rules'][$key]['from']."- ".$rule_set['rules'][$key]['to']."</td>";
$tempstring .= $rule_set['rules'][$key]['amount'];
}
// Print results
echo $tempstring;
}
}
}
我浏览了一个教程,这是很多代码的来源。这是一个稍微不同的案例,但它足以让我开始。
我花了一天的时间一点一点地重建它。我已经评论了我卡住的地方。
如果有人能理解我想要做什么,如果可能的话,向我解释我哪里出错了,以及是否有可能达到预期的结果,我将不胜感激。
提前非常感谢
在我的
选项卡上点击它需要很长时间 - 所以我希望它对您有用 - 基本上我将具有用户角色的所有设置键存储在数组中,然后将所有规则放在数组中使用设置键作为键。然后,在所有循环完成后,我获取所需的数据。这里是:
// Pull the data
global $post, $product;
//** NEW: store user set keys and available rules **//
$user_set_keys = array();
$all_rules = array();
// get the current user's details
$current_user = wp_get_current_user();
// Loop through main rule sets array
$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
// Check to see if there are rule sets
if ($array_rule_sets && is_array($array_rule_sets) && sizeof($array_rule_sets) > 0) {
// Loop through rule sets
foreach ( $array_rule_sets as $rule_key => $rule_set ) { // ** UPDATED
// Get conditions
$conditions = $rule_set['conditions'];
// Loop through conditions
foreach ( $conditions as $key => $condition ) {
// Get Roles
$roles = $condition['args']['roles'];
// Loop through roles
foreach ( $roles as $role ) {
if ( in_array( $role, (array)$current_user->roles ) ) {
$user_set_keys[] = $rule_key; // ** UPDATED **
}
}
}
// Loop through rules array and print pricing table values based on key
foreach ( $rule_set['rules'] as $ruleArray ) {
$all_rules[$rule_key] = array(
'from' => $ruleArray['from'],
'to' => $ruleArray['to'],
'amount' => $ruleArray['amount']
);
}
}
}
// all done now show the data here!
foreach($user_set_keys as $user_set){
print_r($all_rules[$user_set]);
}
回答您的评论:
如果你真的不想要最后一个循环,你可以把你的第二个循环改成这个 - 我不是 100% 无法运行,但我确信我的逻辑是正确的:
// Loop through rules array and print pricing table values based on key
foreach ( $rule_set['rules'] as $ruleArray ) {
if(in_array($rule_key, $user_set_keys)){
print_r($ruleArray);
}
}
如果我
理解正确,那么我认为这就是您需要更改的:
// Loop through rules array and print pricing table values based on key
// foreach ( $rule_set['rules'] as $key => $value ) { // remove this and replace with....
for($i=0; $i<count($role_rules); $i++){
$key = $role_rules[$i];
$tempstring .= $rule_set['rules'][$key]['from']."- ".$rule_set['rules'][$key]['to']."</td>";
$tempstring .= $rule_set['rules'][$key]['amount'];
}