0

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?

Diego
  • 145
  • 4
  • 14

2 Answers2

1

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.

Tro
  • 897
  • 9
  • 32
  • But I will have to make a new case for every new modification? I said only 2 (Apartament and House), but I have more or less 8 types and the default array has about 26 lines... This will messy a lot the code, don't you think? – Diego Sep 03 '15 at 16:14
  • The original question only mentioned three types; could you update the question with all the additional information so we can suggest a better solution? – Tro Sep 03 '15 at 16:16
  • Try removing the quotes in the last line, so it becomes `$post_data[$word] = array ($typevalue);` – Tro Sep 03 '15 at 16:21
1

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.

Ahmad
  • 5,551
  • 8
  • 41
  • 57
  • Indeed. But... when I var_dump I cannot see this modification. Weird.... But thanks :D Is it possible to select a position? I'd like to add this new line after the third line of my array. – Diego Sep 03 '15 at 16:28
  • 1
    I could not get what you said, but I hope my answer helped! This is not built in the language, but if it is so important, you can try something like this: http://stackoverflow.com/questions/3353745/how-to-insert-element-into-array-to-specific-position – Ahmad Sep 03 '15 at 16:34
  • Thanks! And nevermind what I have said before (to take a look in other post). I have already figured it out :D – Diego Sep 03 '15 at 16:42