B
B
Bur Ov2018-09-15 19:32:49
PHP
Bur Ov, 2018-09-15 19:32:49

How to properly group a php array?

Good afternoon. I sit, racking my brains on how to group such an array:

array(
  "/home/fol1/test.txt",
  "/home/fol1/Browsers/test2.txt",
  "/home/fol2/Browsers/test3.txt",
  "/home/fol2/Browsers/ttre/test2.txt"
)

In this one:
аrrау(

  "fol1" => array (
  "/home/fol1/test.txt",
  "/home/fol1/Browsers/test2.txt"
  ),

  "fol2" => array (
   "/home/fol2/Browsers/test3.txt",
  "/home/fol2/Browsers/ttre/test2.txt"
  )
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-09-15
@burov0798

foreach ($arr as $el) {
  $grouped[explode('/', $el)[2]][] = $el;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question