Jump to content

[SOLVED] array sort question...


vbnullchar

Recommended Posts

I have this array..

 

<?php
$arr = array(
array(4,5,6),
array(99,23,42),
array(112,282,131),
array(22,282,131),
array(42,1282,1131),
array(12,282,131),
array(14,2,3),
array('b1','b2','b3'),
array('a','a2','a3')
);
?>

 

I need to sort the first character of the first value... desired output should be like this...

 

<?php
$arr = array(
array('a','a2','a3'),
array('b1','b2','b3'),
array(12,282,131),
array(14,2,3),
array(112,282,131),
array(22,282,131),
array(4,5,6),
array(42,1282,1131),
array(99,23,42)
);
?>

 

meaning arrays with values starts with "1" goes together.. etc.. 

i tried array_multisort but doesnt solve it because after sorting the order will be 12,13,99,112, the right order is 12,14,112,99

 

thanks

Link to comment
Share on other sites

this is the given array...

 

<?php
$arr = array(
   array(4,5,6),
   array(99,23,42),
   array(112,282,131),
   array(22,282,131),
   array(42,1282,1131),
   array(12,282,131),
   array(14,2,3),
   array('b1','b2','b3'),
   array('a','a2','a3')
);
?>

 

it doest matter if letters came first.. my concern is all the values with the same first characters goes together..

 

 

Link to comment
Share on other sites

okay I got it...

 

<?php

$csv1 = array(
array(4,5,6),
array(99,23,42),
array(112,282,131),
array(22,282,131),
array(42,1282,1131),
array(12,282,131),
array(14,2,3),
array('b1','b2','b3'),
array('a','a2','a3')
);

function __unset(&$arr){ unset($arr[0]); }

foreach($csv1 as $k => $value){ $v[] = array_merge((array) substr($value[0],0,1), $value ); }

array_multisort($v);
array_walk($v, "__unset");
print_r($v);

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.