0

I want to create an array using two arrays. The first array contains Strings. The second array contains numbers. I want to create a third array such as for example:

1st array:
array:2 [▼
  0 => "Test"
  1 => "People"
]

2nd array:
array:2 [▼
  0 => "3"
  1 => "2"
]

3rd array:
array:5 [▼
  0 => "Test"
  1 => "Test"
  2 => "Test"
  3 => "People"
  4 => "People"

]

Can you help me?

6
  • foreach and array_fill? Commented Oct 21, 2019 at 13:21
  • "I want" is not a question. Show some attempts. Commented Oct 21, 2019 at 13:22
  • I tried to do a foreach, but i'm doing soime mistakes for sure. I don't know how to repeat those strings @AbraCadaver Commented Oct 21, 2019 at 13:23
  • @u_mulder Yes, sorry. My bad. I wanted to say that it is my goal, but i'm having some trouble Commented Oct 21, 2019 at 13:24
  • @AbraCadaver I tried to implode and str_repeat. Then explode into an array. But that's not very good for me.. Commented Oct 21, 2019 at 13:26

1 Answer 1

1

Try this

$firstArray = [
    0 => 'Test',
    1 => 'People'
];
$secondArray = [
    0 => '3',
    1 => '2'
];
$thirdArray = [];
foreach ($secondArray as $key => $array) {
    for ($i = 1; $i <= (int)$array; $i++) {
        $thirdArray[] = $firstArray[$key];
    }
}
var_dump($thirdArray);

output

array(5) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(4) "Test"
  [2]=>
  string(4) "Test"
  [3]=>
  string(6) "People"
  [4]=>
  string(6) "People"
}
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.