0

So I have a string of comma-separated IDs like so:

$requestids = "22952,22957,22962,22968,22978,22980,22998,23001,23114,23118,23196,23198,23240,23280,23436,23440,23448,23465,23467,23470,23565,23619,23685,23687,23689,23701,23706,23708,23710,23713,23718,23720,23725,23732,23734,23745,23748,23751,23762,23781,23793,23856,23858,23860,23894,23896,23937,23943,23945,23947,24074,25295,25296,25297,25298,25299,25300,25301,25303,"

I can use $requestIdsArray = explode(",",trim($requestids,',')); to break this string into an array, with each ID being a new value, but what I'd really like to do is create an array where each value contains groups of 20 IDs. Like this:

array(
[0] => 22952,22957,22962,22968,22978,22980,22998,23001,23114,23118,23196,23198,23240,23280,23436,23440,23448,23465,23467,23470,
[1] => 23565,23619,23685,23687,23689,23701,23706,23708,23710,23713,23718,23720,23725,23732,23734,23745,23748,23751,23762,23781,
[2] => 23793,23856,23858,23860,23894,23896,23937,23943,23945,23947,24074,25295,25296,25297,25298,25299,25300,25301,25303,
)

How can this be achieved in php?

Brispo
  • 45
  • 1
  • 7
  • 1
    I'm curious why you'd want to do this. It's adding extra code / processing requirements. – Mech Sep 23 '20 at 18:51
  • 2
    Array of arrays of array of strings? – u_mulder Sep 23 '20 at 18:52
  • @mech I'm trying to pass these IDs to Stripe at metadata, but they only allow each key/value pair to have 500 characters for each value, so I need to break a long list of ID into individual key/values containing no more than 500 characters in the value. – Brispo Sep 23 '20 at 18:58
  • No problem with a simple regex. `$matches = []; preg_match_all('/(\d+,){1,20}/', $requestids, $matches); var_dump($matches[0]);` – Markus Zeller Sep 23 '20 at 19:07

0 Answers0