Solved

Sorting a multi dimensional array.... case insensitive

Posted on 2008-10-09
2
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Bruce Pieterse
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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