Solved

Sorting a multi dimensional array.... case insensitive

Posted on 2008-10-09
2
494 Views
Last Modified: 2012-06-27
Hi all,
I have managed to cobble together a bit of code (SNIPPET 1) to sort a multi dimensional array from php.net
However the sort is case sensitive, so upper case are sorted before lower case.

I also found SNIPPET 2, which is supposed to make the sort case insensitive, but cant work out how to get it working with SNIPPET 1... any ideas?
SNIPPET 1
<?
 
$data[] = array('volume' => 'Adam', 'edition' => 1,'target' => 'pie');
$data[] = array('volume' => 'Eve', 'edition' => 2, 'target' => 'applies');
$data[] = array('volume' => 'Zoozoo', 'edition' => 3, 'target' => 'hope');
$data[] = array('volume' => 'Mika', 'edition' => 4,'target' => 'love');
$data[] = array('volume' => 'Moosha', 'edition' => 5,'target' => 'mope');
$data[] = array('volume' => 'Blk', 'edition' => 6,'target' => 'mash');
 
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}
 
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_ASC, $edition, SORT_ASC, $data);
 
print_r($data);
?> 
 
 
SNIPPET 2
 
<?php
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
 
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
 
print_r($array);
?>

Open in new window

0
Comment
Question by:fox_statton
2 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 22677065
Try
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = strtolower($row['volume']);
    $edition[$key] = $row['edition'];
}

Open in new window

0
 
LVL 7

Expert Comment

by:brucepieterse
ID: 22677245

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = ucwords($row['volume']);
    $edition[$key] = $row['edition'];
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is wrong with this PDO Delete Query? 2 18
Wordpress Pagination 1 30
PHP: Best way to scan folders and process files 10 42
AJAX Wordpress Not Reading Variable 2 32
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question