如果在Array里面添加键和值

时间:2015-09-03 15:58:44

标签: php arrays if-statement

我需要在数组中添加一个新行。如果可能,在特定位置(见下文)。但是,只有在确定的变量相等的情况下,才应添加此新行。

这是我的阵列:

$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'category_group' => $category,
);

但是根据变量($ typeword和$ typevalue),它会添加一个新值:

$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'apartament_type' => '1', // this is new
'category_group' => $category,
);

但是这个新的键和值需要根据变量而改变,我称之为$ typeword和$ typevalue,所以它应该是这样的:

$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
 $typeword => $typevalue,
'category_group' => $category,
);

所以......如果这个$ typeword是Apartament或House,它会在这个数组中打印一个新的键和值。如果$ typeword是Store,则不应在数组中打印此新行。

所以让我们假设$ typeword是House:

$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'house_type' => '2', // this is new
'category_group' => $category,
);

如果$ typeword是Store:

$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'category_group' => $category,
);

我尝试添加以下数组:

// ARRAY START
    $post_data = array(
    'check_type_diff' => '0',
    'parent_category' => '1000',
    'category_group' => $category,
    );
// ARRAY END
// Try to merge a new part of array
if ($typeword == 'Apartament' || $typeword == 'House') {
$wordtypeword = array("Apartament","House");
$correctword = array("apartment_typeword","home_typeword"); 
$word = str_replace($wordtypeword, $correctword, $typeword);

$typevaluenew = array("1","1","2","3","1","2"); 
$newtypeword = str_replace($wordtypeword, $typevaluenew, $typeword);
$typevalue = $xml->stuff[$i]->typeword = $newtypeword;
$post_data["$word"] = array ("$typevalue"); //Now I don't know how to to add this into the array, this is not working.        

}

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

你想要的是个案陈述。

$post_data = null;

switch ($typeword) {
    case "House": {
        $post_data = array(
            'check_type_diff' => '0',
            'parent_category' => '1000',
            'house_type' => '2', // this is new
            'category_group' => $category,
        );
        break;
    }
    case "Apartment": {
        $post_data = array(
            'check_type_diff' => '0',
            'parent_category' => '1000',
            'apartment_type' => '2', // this is new
            'category_group' => $category,
        );
        break;
    }
    default: {
        $post_data = array(
            'check_type_diff' => '0',
            'parent_category' => '1000',
            'category_group' => $category,
        );
    }
}

我们正在做的是检查$typeword"Apartment"还是"House",如果是,请创建带有附加行的数组。如果$typeword既不是"Store",也不是$post_data,那么它会为/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -license accept 分配正常数组而不使用额外的行。如果需要,可以轻松扩展以添加其他案例。

答案 1 :(得分:1)

改善Tro的回答:

$post_data = array(
            'check_type_diff' => '0',
            'parent_category' => '1000',
            'category_group' => $category,
        );
switch ($typeword) {
    case "House":
        $post_data['house_type'] = '2';
        break;
    case "Apartment":
        $post_data['apartment_type'] = '2';
        break;'
}

我不得不说,这种方式比你尝试过的要好。这是如此便宜并且更易于修改。您可能希望有时在数组中添加两个索引。