Reputation: 165
I need to add a new line inside an array. If possible in a specific position (see below). But this new line should only be added if a determined variable is equal something.
This is my array:
$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'category_group' => $category,
);
But depending on variable ($typeword and $typevalue), it add a new value:
$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'apartament_type' => '1', // this is new
'category_group' => $category,
);
But this new key and value needs to change depending on a variable, I have called it $typeword and $typevalue, so it should be something like:
$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
$typeword => $typevalue,
'category_group' => $category,
);
So... if this $typeword is Apartament or House, it prints a new key and value inside of this array. If $typeword is Store, it should not print this new line in the array.
So let's suppose $typeword is House:
$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'house_type' => '2', // this is new
'category_group' => $category,
);
If $typeword is Store:
$post_data = array(
'check_type_diff' => '0',
'parent_category' => '1000',
'category_group' => $category,
);
What I'm trying to add below array:
// 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.
}
How can I achieve this?
Upvotes: 0
Views: 67
Reputation: 5760
Improvement to Tro's answer:
$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;'
}
I have to say, this way is better than what you have tried. This is so less expensive and is more modifiable. You may want to add for example two indexes to the array sometime.
Upvotes: 1
Reputation: 909
What you want is a case statement.
$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,
);
}
}
What we are doing is checking if $typeword
is either "Apartment"
or "House"
, and if so creating the array with the additional line. If $typeword
is neither of those, i.e. "Store"
, then it instead assigns $post_data
with the normal array without the extra line. This can be easily extended to add additional cases if required.
Upvotes: 1