0

I have tried array_merge, to merge them based on similar keys, array_push, various [] combinations but I just can't figure this one out. I have two arrays, one looks like:

Array
(
    [650] => Array
        (
            [Kampan] => 
            [ZelvaUL] => 650
            [ZelvaOV] => 
            [OCS] => 
            [Rezim] => Ruční
        )

    [651] => Array
        (
            [Kampan] => 3003C_DSL_upsell_TV_SU
            [ZelvaUL] => 651
            [ZelvaOV] => 
            [OCS] => 21
            [Rezim] => IN
        )

    [652] => Array
        (
            [Kampan] => 
            [ZelvaUL] => 652
            [ZelvaOV] => 
            [OCS] => 22
            [Rezim] => IN
        )

And, I want to add one new key to each of 650, 651, 652... sub-arrays (I will call the key 'Barva'), and short set of values from another array (10 total) to periodically loop in each sub-array under that key, so that 1st and 11th value is the same, 2nd and 12th is the same and so on, and all to be under the same key. It would look like:

Array
(
    [650] => Array
        (
            [Kampan] => 
            [ZelvaUL] => 650
            [ZelvaOV] => 
            [OCS] => 
            [Rezim] => Ruční
            [Barva] => 1
        )

    [651] => Array
        (
            [Kampan] => 3003C_DSL_upsell_TV_SU
            [ZelvaUL] => 651
            [ZelvaOV] => 
            [OCS] => 21
            [Rezim] => IN
            [Barva] => 2
        )

    [652] => Array
        (
            [Kampan] => 
            [ZelvaUL] => 652
            [ZelvaOV] => 
            [OCS] => 22
            [Rezim] => IN
            [Barva] => 3
        )

...

    [660] => Array
        (
            [Kampan] => ...
            [ZelvaUL] => ...
            [ZelvaOV] => ...
            [OCS] => ...
            [Rezim] => ...
            [Barva] => 1
        )

Seriously, I am out of ideas... Thanks for any help guys.

edit: This is the array I want to add:

$camp_barvy = array(
  'background-color:#ffffff;color:#111111;',
  'background-color:#ffcc02;color:#111111;',
  'background-color:#ff7700;color:#ffffff;',
  'background-color:#ff2323;color:#ffffff;',
  'background-color:#ff00aa;color:#ffffff;',
  'background-color:#aa44ff;color:#ffffff;',
  'background-color:#1188ff;color:#ffffff;',
  'background-color:#11ddff;color:#111111;',
  'background-color:#00dd77;color:#111111;',
  'background-color:#119911;color:#ffffff;'
);

I wanna do some large and extensive conditioned formatting and both javascript and php if statement make the loading too slow, so I figured I will make the format part of the array I already look in for the values based on which I choose the desired format. Really, its the best choice :)

5
  • Where do you get the correct value for each Barva? Commented Mar 24, 2014 at 22:54
  • Have you tried the array_merge_recursive method? Commented Mar 24, 2014 at 22:57
  • kingkero - I have those in another array; oski555 - i tried to merge them but I can't figure out how to build the foreach loop, I am new to php Commented Mar 24, 2014 at 22:59
  • @IRMA 1) can you post the sample of your current array 2) and the array you need to include on the current array 3) as well as the desired result? Right now you have number 1 and number 3 but you're missing number 2. You can use the EDIT to update your question with more information Commented Mar 24, 2014 at 23:00
  • @Prix - I added the part you want to see. I didn't want to add it at first because I am kinda afraid you will just tell me to do it other way, but I wanna try it this way because of the reasons described in the edit. Commented Mar 24, 2014 at 23:08

1 Answer 1

2

What you want to do is iterate over each value in your "input" array and insert in it a new value taken from your "data" array (those 10 values you mention). When your data array is exhausted, you want to loop back to its start and continue inserting values in the "input" array elements.

So you want something like:

foreach ($input as &$row) {
    $row['Brava'] = $next_item_from_data_array;
}

which leaves just the problem of how to easily iterate and loop over the data array.

A convenient and modern way of doing this is by using the built-in SPL iterators: an ArrayIterator for your data array and an InfiniteIterator around that so that you loop back to the start automatically as required. This way you also don't have to assume anything about your data array (such as if it is numerically indexed or not).

For example:

$dataIterator = new InfiniteIterator(new ArrayIterator($data));
$dataIterator->rewind();
foreach ($input as &$row) {
    $row['Brava'] = $dataIterator->current();
    $dataIterator->next();
}

// After iterating by reference (&$row) it is always a good idea to unset
// the reference so that you don't reuse it later on by mistake -- although
// this is not required and the program will work correctly without it.
unset($row);

See it in action.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.