How to solve php recursive functions

Source: Internet
Author: User
According to the idea, the php recursive function returns a two-dimensional array with two values using a test array. But it is worth reading an array. isn't it possible to write the red part like this? How should I write it? Solution ==================================== the code is as follows ==== ====== php recursive function solution
Originally, according to the idea, the recursive function returns a two-dimensional array with two values using a test array. But it is worth reading an array. isn't it possible to write the red part like this? How should I write it? Solution
==================================== The code is as follows: ========================================================== ======================
$ Strr = array ("Thursday 301 _ Phoenix Mercury _ Chicago Sky _ main victory _ 2.10", "Tuesday 302 _ Minnesota bobcat _ Indiana fanatic _ main victory _ 1.20 ", "Tuesday 302 _ Minnesota bobcat _ Indiana fanatic _ Kewin _ 2.92 ");

// This is the way to split the array -----
Function A ($ str ){
// Define a two-dimensional array to store the logically separated array
$ Newstr = array ();
For ($ I = 0; $ I $ E1 = explode ("_", $ str [$ I]);
$ E2 = explode ("_", $ str [$ I + 1]);
If ($ e1 [0]. $ e1 [1]. $ e1 [2] = $ e2 [0]. $ e2 [1]. $ e2 [2]) {
// Split the string into two arrays and call A ($ str) recursively)
A (array_splice ($ str, $ I, $ I ));
A (array_splice ($ str, $ I + 1, $ I + 1 ));
}
}
Array_push ($ newstr, $ str );
Return $ newstr;
}
//------------------------
$ Back = A ($ strr );
Print_r ($ back );
?>
========================================================== ========================================================== ======================================

------ Solution --------------------
No recursion is needed
PHP code
  $ Oddses) {// $ newList [] = array (); $ newList = _ combinate ($ newList, $ oddses);} return $ newList ;} function _ combinate ($ arr, $ newItems) {$ newArr = array (); if (empty ($ arr) {foreach ($ newItems as $ item) {$ newArr [] = array ($ item);} return $ newArr;} foreach ($ newItems as $ newItem) {foreach ($ arr as $ oldItems) {$ oldItems [] = $ newItem; $ newArr [] = $ oldItems;} return $ newArr;} echo'
'; Print_r (B ($ strr); exit;
------ Solution --------------------
It's just that your data is not suitable. just sort the order.
I don't know what you want to do! Just guess your thoughts based on your code. If you leave your overall idea, it will be meaningless.
PHP code
$ Str_2 = array ("a_a_a_1", "a_a_a_2", "B _ B _ B _1", "B _ B _ B _ B _2", "c_c_c_1", "a_a_a_3 "); // ------------------------ $ back = A ($ str_2); print_r ($ back); // This is the method for splitting arrays ----- function A ($ str) {sort ($ str ); // sort and put the similarity together. // define a two-dimensional array to store the array $ newstr = array (); for ($ I = 0; $ I
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.