Jump to content

Best way to re-order this array?


fiasst

Recommended Posts

Hi, I've been trying to re-order an array and I can't seem to even do it let alone neatly. I've ran out of time and would appreciate any help!

 

$array = array(
  '0' => array(
    'filepath' => 'files/image_site1.png',
    'title' => 'Website',
  ),
  '1' => array(
    'filepath' => 'files/image_id.png',
    'title' => 'Identitity',
  ),
  '2' => array(
    'filepath' => 'files/image_site2.png',
    'title' => 'Website',
  )
)

 

I'm looking to re-order the arrays within the parent array depending on whether the 'filepath's contain a sub string of '_id' or '_site'.

 

I've been using:

$type = '_site';
foreach ($array as $item){
  if (preg_match("/$type/i", $item['filename'])){
    //add this $item to the top of array, somehow...
  }
}

 

So with $type set to '_site', I'd like to re-arrange the array so both arrays containing _site1.png and _site2.png appear at the top of the parent array. I.e:

$array('0' = array(...), '2' => array(...), '1' => array(...));

 

I hope I've explained it well enough.

 

Thanks,

Link to comment
Share on other sites

There are a number of different things that you could do.

 

An idea would be to split apart the different arrays (e.g. one containing just _site items, another containing just _id items, and the left-overs), sort those individually and then put them all back into one big array in whichever order you want.

 

Another idea is to use the existing array sorting functions which allow you to provide a custom comparison function to determine what gets sorted "higher" than other values.  To give you an example, this sorts your array alphabetically by filepath but pushes the _site items to the top (also in alphabetical order).

 

uasort($array, 'sorter');
print_r($array);

function sorter($a, $b) {
    $a_site = strpos($a['filepath'], '_site') !== FALSE;
    $b_site = strpos($b['filepath'], '_site') !== FALSE;
    
    if ($a_site && !$b_site) {
        // Only $a is site, move $a up
        return -1;
    } elseif (!$a_site && $b_site) {
        // Only $b is site, move $a down
        return 1;
    } else {
        // Both site, or both not site, compare alphabetically
        return strcmp($a['filepath'], $b['filepath']);
    }
}

 

The basic idea of a comparison function like that is to return less than zero (can be anything but is traditionally -1) if the first value is less than the second value (think: it is "lighter" so floats towards the top), zero if they are both considered the same, and greater than zero otherwise.

Link to comment
Share on other sites

Having had a long think I came up with this which seems to work too!

<?php

$arr = array(

0 => array('filename' => 'image_id.jpg'),

1 => array('filename' => 'image_site1.jpg'),

2 => array('filename' => 'image_site2.jpg'),

);

$type = "_site";

for ($i = 0; $i < count($arr); $i++){

$key = ((preg_match("/$type/i", $arr[$i]['filename'])) ? -$i : $i);

$newarr[$key] = $arr[$i]['filename'];

}

ksort($newarr);

?>

 

Not sure how clean a solution it is though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.