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
https://forums.phpfreaks.com/topic/152712-solved-array-sort-question/
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..

 

 

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.