In php, the number of m is n. What should I do if it exceeds the memory?

Source: Internet
Author: User
Below is the code I found online: {code ...} {code ...} output result: Array ({code ...}) my $ arr has hundreds of elements and exceeds the memory after execution. What should I do? Can someone help improve the combined functions? I am weak in this regard. I don't know how to do this? The code I found online is as follows:

function getCombinationToString($arr,$m){    $result = array();    if ($m ==1)    {        return $arr;    }    if ($m == count($arr))    {        $result[] = implode(',' , $arr);        return $result;    }    $temp_firstelement = $arr[0];    unset($arr[0]);    $arr = array_values($arr);    $temp_list1 = getCombinationToString($arr, ($m-1));    foreach ($temp_list1 as $s)    {        $s = $temp_firstelement.','.$s;        $result[] = $s;    }    unset($temp_list1);    $temp_list2 = getCombinationToString($arr, $m);    foreach ($temp_list2 as $s)    {        $result[] = $s;    }    unset($temp_list2);    return $result;}
$arr=['a','b','c','d','e','f'];getCombinationToString($arr,3);

Output result:

Array
(

[0] => a,b,c[1] => a,b,d[2] => a,b,e[3] => a,b,f[4] => a,c,d[5] => a,c,e[6] => a,c,f[7] => a,d,e[8] => a,d,f[9] => a,e,f[10] => b,c,d[11] => b,c,e[12] => b,c,f[13] => b,d,e[14] => b,d,f[15] => b,e,f[16] => c,d,e[17] => c,d,f[18] => c,e,f[19] => d,e,f

)
My $ arr has hundreds of elements and exceeds the memory after execution. What should I do? Can someone help improve the combined functions? I am weak in this regard. I don't know how to do this?

Reply content:

The code I found online is as follows:

function getCombinationToString($arr,$m){    $result = array();    if ($m ==1)    {        return $arr;    }    if ($m == count($arr))    {        $result[] = implode(',' , $arr);        return $result;    }    $temp_firstelement = $arr[0];    unset($arr[0]);    $arr = array_values($arr);    $temp_list1 = getCombinationToString($arr, ($m-1));    foreach ($temp_list1 as $s)    {        $s = $temp_firstelement.','.$s;        $result[] = $s;    }    unset($temp_list1);    $temp_list2 = getCombinationToString($arr, $m);    foreach ($temp_list2 as $s)    {        $result[] = $s;    }    unset($temp_list2);    return $result;}
$arr=['a','b','c','d','e','f'];getCombinationToString($arr,3);

Output result:

Array
(

[0] => a,b,c[1] => a,b,d[2] => a,b,e[3] => a,b,f[4] => a,c,d[5] => a,c,e[6] => a,c,f[7] => a,d,e[8] => a,d,f[9] => a,e,f[10] => b,c,d[11] => b,c,e[12] => b,c,f[13] => b,d,e[14] => b,d,f[15] => b,e,f[16] => c,d,e[17] => c,d,f[18] => c,e,f[19] => d,e,f

)
My $ arr has hundreds of elements and exceeds the memory after execution. What should I do? Can someone help improve the combined functions? I am weak in this regard. I don't know how to do this?

You can generate an output.

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.