There are two arrays:
$arr = [1,2,3,4,5] $var = [100,200,300,400,500] How to implement such a conclusion
1 100 2 3 4 400 5 500 Thank you, the input data may be different. Please do not customize this case.
There are two arrays:
$arr = [1,2,3,4,5] $var = [100,200,300,400,500] How to implement such a conclusion
1 100 2 3 4 400 5 500 Thank you, the input data may be different. Please do not customize this case.
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
It is not clear by what principle you disappeared in output 200 and 300 from the second array. If you just need to sort by the "string" principle:
$arr = [1,2,3,4,5]; $var = [100,400,500]; $sum = array_merge($arr,$var); sort($sum, SORT_STRING); print_r($sum); At the exit:
Array ( [0] => 1 [1] => 100 [2] => 2 [3] => 3 [4] => 4 [5] => 400 [6] => 5 [7] => 500 ) In order to solve a problem, it is necessary to fully formulate it. You may know it completely, but we do not. It is completely unclear from the text of the question, according to which rules you arrange the order of elements from arrays.
For the solution you just need to sort through the arrays and set the conditions. For example, if the condition is great random:
<?php $arr = [1,2,3,4,5]; $var = [100,200,300,400,500]; while(count($var) > 0 && count($arr) > 0){ echo array_shift($arr) + "<br>"; while(count($var) > 0){ if(rand(1,3) == 2) break; //привет, я условие рандом echo array_shift($var) + "<br>"; } } ?> You just need to substitute your values / order / additional conditions / maybe do something else, based on the full text of the task.
PS This code modifies the original arrays, if you do not want this, you will have to create variables with the current position and check whether it has crossed the array boundaries.
Source: https://ru.stackoverflow.com/questions/511826/
All Articles