0

I have 2 arrays that I need to combine into 1 array.

The $languages Array should hold a [line] for each [language] array and only its own matching [language] value.

First array:

$languages = Array (
                   [english] => Array ()
                   [germany] => Array ()
                   [russia] => Array ()
             );

Second array:

$data = Array (
              [line0] => Some text
              [english0] => English text
              [germany0] => Dutch text
              [russia0] => Russian text

              [line1] => Some more text
              [english1] => English some more text
              [germany1] => Dutch some more text
              [russia1] => Russian some more text

              .......
              (key's increment by 1)
        ); 

What a combined array should look like:

$languages = Array (
                [english] = Array (
                                [line0] => Some text
                                [english0] => English text

                                [line1] => Some more text
                                [english1] => English some more text

                                ........
                                (key's increment by 1)
                         )
                [germany] = Array (
                                [line0] => Some text
                                [germany0] => Dutch text

                                [line1] => Some more text
                                [germany1] => Dutch some more text

                                ......
                                (key's increment by 1)
                         )
                [russia] = Array (
                               [line0] => Some text
                               [russia0] => Russian text

                               [line1] => Some more text
                               [russia1] => Russian some more text

                               ........
                               (key's increment by 1)
                         )
             );
2
  • So what have you tried in order to get this structure? Commented Feb 5, 2016 at 12:25
  • 2
    You are starting off with some badly structured data i.e. the second array and you want to keep that bad structure in the resulting array. Have another think about how you create the second array Commented Feb 5, 2016 at 12:29

3 Answers 3

3

refere below solution for your desired result:

$languages = Array(
    'english' => Array(),
    'germany' => Array(),
    'russia' => Array()
);

$data = Array(
    'line0' => 'Some test',
    'english0' => 'English test',
    'germany0' => 'Dutch test',
    'russia0' => 'Russian test',

    'line1' => 'Some more test',
    'english1' => 'English some more test',
    'germany1' => 'Dutch some more test',
    'russia1' => 'Russian some more test',

);

$new_array = array();
foreach ($languages as $k => $v) {
    foreach ($data as $key => $value) {
        if (strpos($key, 'line') !== false || strpos($key, $k) !== false)
            $new_array[$k][$key] = $value;
    }
}

print_r($new_array);

output:

Array
(
    [english] => Array
        (
            [line0] => Some test
            [english0] => English test
            [line1] => Some more test
            [english1] => English some more test
        )

    [germany] => Array
        (
            [line0] => Some test
            [germany0] => Dutch test
            [line1] => Some more test
            [germany1] => Dutch some more test
        )

    [russia] => Array
        (
            [line0] => Some test
            [russia0] => Russian test
            [line1] => Some more test
            [russia1] => Russian some more test
        )

)

But as suggested in comment you should create better format. For example refer below example:

$new_array = array();
foreach ($languages as $k => $v) {
    foreach ($data as $key => $value) {
        $textarr = str_split($key, strlen($key) - 1);
//        print_r($textarr);
        if (strpos($key, 'line') !== false || strpos($key, $k) !== false)
        $new_array[$k][$textarr[0]][$textarr[1]] = $value;

    }
}

print_r($new_array);

Output:

Array
(
    [english] => Array
        (
            [line] => Array
                (
                    [0] => Some test
                    [1] => Some more test
                )

            [english] => Array
                (
                    [0] => English test
                    [1] => English some more test
                )

        )

    [germany] => Array
        (
            [line] => Array
                (
                    [0] => Some test
                    [1] => Some more test
                )

            [germany] => Array
                (
                    [0] => Dutch test
                    [1] => Dutch some more test
                )

        )

    [russia] => Array
        (
            [line] => Array
                (
                    [0] => Some test
                    [1] => Some more test
                )

            [russia] => Array
                (
                    [0] => Russian test
                    [1] => Russian some more test
                )

        )

)

Hope this will helps you.

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

2 Comments

This will be the faster and clear way to get an output as compare to my code :) Good logic @Chetan Ameta
That did help me. Thanks !
1

Just for trying's sake. For correct way see Chetan's answer

$newArray = array();
$finalArray = array();

$thirdArray = array('line','english','germany','russia'); // for formatting 2nd array

//$data is secondArray

$countData = count($data);
for($i=0;$i<$countData/4;$i++)
{
    foreach ($thirdArray as $key => $value) {
        $newArray[$i][$value.$i] =$data[$value.$i]; //formatting array
        }
}

foreach($newArray as $key=>$val)
{
    foreach($languages as $k=>$v){
            $finalArray[$k][line.$key] = $newArray[$key][line.$key];
            $finalArray[$k][$k.$key] = $newArray[$key][$k.$key];
    }

}


print_r($finalArray); // outputs desired results

Comments

0

Assumption: (key's increment by 1) from the $data array. If not this code will give error of undefined index.

$languageReference : Used as $languages Reference as I don't want to save the result in same array, but if you want you can.

$reference : Saving reference of the $data array as end() function will move internal pointer to end of the array so don't want to deal with the original array.

$languageReference = $languages;
$reference = $data;
end($reference);
$lastKey = key($reference);
$count = (int) $lastKey[strlen($lastKey) - 1];
foreach ($languageReference as $lang => $langData) {
    for ($index = 0; $index <= $count; $index++) {
        $languageReference[$lang]['line' . $index] = $data['line' . $count];
        $languageReference[$lang][$lang . $index] = $data[$lang . $count];
    }
}

echo "<pre>";
var_dump($languageReference);

output:

array(3) {
  ["english"]=>
  array(4) {
    ["line0"]=>
    string(14) "Some more text"
    ["english0"]=>
    string(22) "English some more text"
    ["line1"]=>
    string(14) "Some more text"
    ["english1"]=>
    string(22) "English some more text"
  }
  ["germany"]=>
  array(4) {
    ["line0"]=>
    string(14) "Some more text"
    ["germany0"]=>
    string(20) "Dutch some more text"
    ["line1"]=>
    string(14) "Some more text"
    ["germany1"]=>
    string(20) "Dutch some more text"
  }
  ["russia"]=>
  array(4) {
    ["line0"]=>
    string(14) "Some more text"
    ["russia0"]=>
    string(22) "Russian some more text"
    ["line1"]=>
    string(14) "Some more text"
    ["russia1"]=>
    string(22) "Russian some more text"
  }
}

Comments

Your Answer

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