1

How do I increment(add more values) to an array with key value pairs in a loop.

$field['choices'] = array(
   'custom' => 'My Custom Choice'
 );

Let's says I want to add three more choices from another array?

Output I want to achieve:

$field['choices'] = array(
  'custom1' => 'My Custom Choice1'
  'custom2' => 'My Custom Choice2'
  'custom3' => 'My Custom Choice3'
  'custom4' => 'My Custom Choice4'
);
4
  • Could you give an example output of what you're trying to achieve? Commented Aug 16, 2013 at 21:43
  • Your example doesn't match your description. You started with the key custom, but where is that in the result? You said you wanted to add 3 elements, but instead you removed 1 and added 4. Commented Aug 16, 2013 at 21:48
  • So specific..the key values don't matter. Just want to know how to add them. I will try the suggestions below. Commented Aug 16, 2013 at 21:50
  • 1
    What is the other array? Is it also an associative array, do you need to use its keys? How are the elements of the result related to the other array? Commented Aug 16, 2013 at 21:53

5 Answers 5

1

Iterate, and concatenate the index to the prefix in your key:

for ($i = 2; $i <= 4; $i++) {
    $field['choices']['custom' . $i] = 'My Custom Choice' . $i;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Barmar. I was having trouble figuring out the syntax. But as you and the other two answers shows... this works > $field['choices']['custom' . $i] = 'My Custom Choice' . $i;
If you need a variable (or a key, for that matter) named SomethingN, you need an (inner) array. Also, OP said he's adding the other choices from a different array.
@MadaraUchiha He said that, but nothing else in his question supports that. I asked him about the other array, he never answered. But I agree that whenever you're incrementing strings like this, it strongly suggests that you should be using a numeric sub-array.
0

You can use the array functions to sort or merge.

Or you can do something like this:

$field['choices']['custom'] = 'My Custom Choice';
$field['choices']['custom2'] = 'My Custom Choice';
$field['choices']['custom3'] = 'My Custom Choice';
$field['choices']['custom4'] = 'My Custom Choice';

2 Comments

This works, but he said he wanted to get those other choices "from a different array".
That is why I suggested the array functions first.
0

You would want to use array_merge().

From the manual:

array array_merge ( array $array1 [, array $... ] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

2 Comments

What other array is he merging?
@Barmar: The one he's adding his three other choices from. See the question.
0

As you had outlined in your question:

$field['choices'] = array(
   'custom' => 'My Custom Choice'
 );

So:

$array = $field['choices'];

to simplify the following example:

$otherArray = range(1, 3); // another array with 3 values

$array += $otherArray; // adding those three values (with _different_ keys)

done. The + operator with arrays is called union. You find it documented here:

So as long as the other array you want to add has three different keys compared to the one array you add it to, you can use the + operator.

Comments

0

Here is the code i would use to add $otherArray values to a custom increment array key value

if (count($field['choices']) === 1) {
    $field['choices']['custom1'] = $field['choices']['custom'];
    unset($field['choices']['custom'];
    $i = 2;
} else {
    $i = count($field['choices']) + 1;
}//END IF
foreach ($otherArray as $key => $val) {
    $field['choices']['custom' . $i] =  $val;//Where val is the value from the other array
    $i++;
}//END FOREACH LOOP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.