Reputation: 2473
I need to build some 500 - 800 arrays (or subarrays), which contains both static text and variables. Each array could have different texts and variables, even though it will be common that some text and variables are repetitive.
Question:
Is there a practically way to reuse the array structure and produce the 500 - 800 arrays? Would I need build 2 separate arrays as source to inject the data? (one array to hold all variables, one array to hold all text with clear indicator which text belongs to whichs planned array)?
The result could also be a multidimensional array.
<?php
$id_swedish = 'SEK';
// Array
$unit_1 = [
'id' => [
$id_swedish,
'static_text_1',
'static_text_2',
'static_text_3',
'...',
'static_text_100',
]
];
// Array
$unit_2 = [
'id' => [
$id_swedish,
'other_text_1',
'other_text_2',
'other_text_3',
'...',
'other_text_100',
]
];
print_r($unit_1);
print_r($unit_2);
Wanted result
Array
(
[id] => Array
(
[0] => SEK
[1] => static_text_1
[2] => static_text_2
[3] => static_text_3
[4] => ...
[5] => static_text_100
)
)
Array
(
[id] => Array
(
[0] => SEK
[1] => other_text_1
[2] => other_text_2
[3] => other_text_3
[4] => ...
[5] => other_text_100
)
...etc up to 500 - 800 arrays.
Upvotes: 0
Views: 32
Reputation: 3757
Why not using array_merge, to glue repeating static text and variables with new array items? So you have one array that holds the common data:
$base_data = [
$id_swedish,
'common_static_text_1',
...
];
And then during generation of those arrays, you use array_merge to merge them with base/default data:
$unit_1_id = [
'static_text_1',
'static_text_2',
'static_text_3',
'...',
'static_text_100',
];
$unit_1 = [
array_merge($base_data, $unit_1_id)
];
Upvotes: 0