Tag Archives: explode

Explode a multi-dimensional array (PHP)

php-banner

Sometimes you need to explode more than just a single dimension array.  In the case of a project I worked on recently, I was given this data, taken from MySQL in the form of a string.

bromsgrove:27.83|crawley:153.34|manchester:119.5|peterborough:125.62|

So, this is four locations, with a respective distance.

The trick with splitting them up, using PHP explode(), is to do it in two parts.  In this case, I first want to split them into units of warehouse location and distance, which is achieved like this:

$firstArray= explode('|',$distanceData->distance);

This will then produce an array that looks like this:

array (size=5)
  0 => string 'bromsgrove:27.83' (length=16)
  1 => string 'crawley:153.34' (length=14)
  2 => string 'manchester:119.5' (length=16)
  3 => string 'peterborough:125.62' (length=19)
  4 => string '' (length=0)

The problem with this, of course, is that the delimiter is added to the end of every location that is concatenated onto the end of the string when the data is being produced, so the next step is to get rid of the last character, which [in this case and many others], will always be tagged onto the end of a string like this.  We can do this with PHP substr().

$distanceData = substr($distanceData,0,-1);

So now when the array is produced, it doesn’t have the empty slot at the end.

array (size=4)
  0 => string 'bromsgrove:27.83' (length=16)
  1 => string 'crawley:153.34' (length=14)
  2 => string 'manchester:119.5' (length=16)
  3 => string 'peterborough:125.62' (length=19)

We now have 4 arrays that all need exploding, so it’s simply a case of cycling through each with a foreach loop and creating a new exploded array each time:

foreach($firstArray as $key => $data)
{
   $distance[$key]= explode(':',$data);
}

Which will give the final result we are looking for, a fully split-up, two-dimensional array, created from a delimited string.

array (size=4)
  0 => 
    array (size=2)
      0 => string 'bromsgrove' (length=10)
      1 => string '27.83' (length=5)
  1 => 
    array (size=2)
      0 => string 'crawley' (length=7)
      1 => string '153.34' (length=6)
  2 => 
    array (size=2)
      0 => string 'manchester' (length=10)
      1 => string '119.5' (length=5)
  3 => 
    array (size=2)
      0 => string 'peterborough' (length=12)
      1 => string '125.62' (length=6)

Although this array is OK, it does still need some work to make it super easy to use.  This can be done by whittling it down further, whilst already inside the foreach loop.

First, initialise an array:

$hubDistance= array();

Then build this array as you loop through using the data provided from the explode.

foreach ($firstArray as $key => $data) {
    $distance[$key] = explode(':', $data);
    $hubDistance[$distance[$key][0]] = (float)$distance[$key][1];
}

This will strip out any of the unnecessary levels in the array and leave you with something like this:

array (size=4)
  'bromsgrove' => float 27.83
  'crawley' => float 153.34
  'manchester' => float 119.5
  'peterborough' => float 125.62

Which is much better!

I’ve explicitly called float on the numbers for this project, but PHP will typically sort this out for you.

Update:

I keep needing to use this on various projects, so I’ve whittled it down to a function if anybody needs to use it:

function stringToArray($string)
{

    /*
     * This relies on the string being in the format
     * a1:a1a¬b1:b1a¬c1:c1a¬ [etc]
     * Individual separator = :
     * Group / cluster separator = ¬
     */

    $string = mb_substr($string,0 ,-1);

    $firstArray = explode("¬",$string);

    $cleanArray = array();

    foreach ($firstArray as $key => $data) {
        $innerArray[$key] = explode(':', $data);
        $cleanArray[$innerArray[$key][0]] = $innerArray[$key][1];
    }

    return $cleanArray;

} 

So if you input a string like this :

$inputString = "sw4:33¬sw5:44¬swnh4:23¬swnh5:0¬mw4:223¬mw5:233¬mwnh4:433¬mwnh5:45¬"

Then use the function:

var_dump(stringToArray($inputString);

You’ll end up with something that looks like this:

array (size=8)
  'sw4' => string '33' (length=2)
  'sw5' => string '44' (length=2)
  'swnh4' => string '23' (length=2)
  'swnh5' => string '0' (length=1)
  'mw4' => string '223' (length=3)
  'mw5' => string '233' (length=3)
  'mwnh4' => string '433' (length=3)
  'mwnh5' => string '45' (length=2)

Probably no use to anybody, but it’s there if you need it 🙂
 

Share this post:
read more