IF在数组中添加键和值


IF inside Array to add key and value

我需要在数组中添加新行。如果可能,在一个特定的位置(见下文)。但是只有当一个确定的变量等于什么时,才应该添加这一行。

这是我的数组

$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是公寓或房子,它在这个数组中打印一个新的键和值。如果$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.        
}

我怎样才能做到这一点?

您需要的是case语句。

$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,而不需要额外的行。如果需要,可以很容易地对其进行扩展以添加额外的大小写。

对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;'
}
我不得不说,这种方法比你所尝试的要好。这是如此便宜,更容易修改。例如,有时您可能需要向数组中添加两个索引。