Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would <code>explode</code> the values based on the / and then use <code>array_intersect_assoc</code> to detect the common elements and ensure they have the correct corresponding index in the array. The resulting array could be recombined to produce the common path.</p> <pre><code>function getCommonPath($pathArray) { $pathElements = array(); foreach($pathArray as $path) { $pathElements[] = explode("/",$path); } $commonPath = $pathElements[0]; for($i=1;$i&lt;count($pathElements);$i++) { $commonPath = array_intersect_assoc($commonPath,$pathElements[$i]); } if(is_array($commonPath) return implode("/",$commonPath); else return null; } function removeCommonPath($pathArray) { $commonPath = getCommonPath($pathArray()); for($i=0;$i&lt;count($pathArray);$i++) { $pathArray[$i] = substr($pathArray[$i],str_len($commonPath)); } return $pathArray; } </code></pre> <p>This is untested, but, the idea is that the <code>$commonPath</code> array only ever contains the elements of the path that have been contained in all path arrays that have been compared against it. When the loop is complete, we simply recombine it with / to get the true <code>$commonPath</code></p> <p><strong>Update</strong> As pointed out by Felix Kling, <code>array_intersect</code> won't consider paths that have common elements but in different orders... To solve this, I used <code>array_intersect_assoc</code> instead of <code>array_intersect</code></p> <p><strong>Update</strong> Added code to remove the common path (or tetris it!) from the array as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload