Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

array_multisort

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

array_multisort

Postby Knutsford » Tue Oct 02, 2012 4:22 am

I am trying do do a multidimensional array sort but it isn't working. This is my code. I want it sort
all the $TempItems[$i][0]'s then the $TempItems[$i][2]'s within that






while (!feof($files)) {
$record = fgets($files);
$record = trim(substr($record,1));
$record = substr($record,0,strlen($record) - 1);
$Items = split('","',$record);
if (trim($Items[2]) == "0") {
$i++;
$TempItems[$i][0] = trim($Items[0]);
$TempItems[$i][1] = trim($Items[1]);
$TempItems[$i][2] = trim($Items[3]);
}
}
print_r($TempItems);
echo "<br>";
echo "<br>";

array_multisort(
$TempItems[0], SORT_ASC,
$TempItems[2], SORT_NUMERIC, SORT_ASC
);
print_r($TempItems);


The first print_r gives




Array ( [0] => Array ( [0] => 23699923 [1] => http://images.portalimages.com/large/tp ... 530449.jpg [2] => 0 ) [1] => Array ( [0] => 23699923 [1] => http://images.portalimages.com/large/tp ... 530449.jpg [2] => 1 ) [2] => Array ( [0] => 23699923 [1] => http://images.portalimages.com/large/tp ... 530449.jpg [2] => 2 ) etc





After the sort I get




Array ( [0] => Array ( [0] => 0 [1] => 23699923 [2] => http://images.portalimages.com/large/tp ... 530449.jpg ) [1] => Array ( [0] => 23699923 [1] => http://images.portalimages.com/large/tp ... 530449.jpg [2] => 1 ) [2] => Array ( [0] => 2 [1] => 23699923 [2] => http://images.portalimages.com/large/tp ... 530449.jpg ) etc


Records 0 and 2 are wrong


It ends up as


Array ( [0] => Array ( [0] => 0 [1] => 23699923 [2] => http://images.portalimages.com/large/tp ... 530449.jpg ) etc


instead of


Array ( [0] => Array ( [0] => 23699923 etc



Why? The code looks ok to me but I presume I am confused somewhere. I know it starts off in the right order but I gatherr the input might always do


Thanks
Knutsford
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Mar 01, 2012 8:08 am

Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.