Jump to content

Sorting arrays


fezzik

Recommended Posts

I need help with how to accomplish the following...

I have a multidimesional array with an unlimited number of rows and 5 columns called $myArray.The array is created from a database query. I would like to create 2 different displays from this array that are printed to a page. The first display would be grouping all rows which share the same value for a specified column (i.e. $myArray[$row][0]) and then printing them. Similar to:

// array data
$myArray[1][0] = "sifl";
$myArray[1][1] = "apple";
$myArray[2][0] = "olly";
$myArray[2][1] = "orange";
$myArray[3][0] = "sifl";
$myArray[3][1] = "pear";
$myArray[4][0] = "sifl";
$myArray[4][1] = "banana";

// printed output would look like

sifl
apple
pear
banana

olly
orange

The second display would be similar but it would group rows which shared the same values for specified 2 columns. If anyone has the time to help me possibly create 2 functions to accomplish this task I would appreciate it. Any information is appreciated.

Best,

Fezzik
Link to comment
Share on other sites

Sure!

So I have a multidimensional array of many rows and 5 columns per row. The data is similar to:

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Jazz";
$myArray[0][2] = "Porsche";
$myArray[0][3] = "White";
$myArray[0][4] = "Steering Realignment";

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Optimus Prime";
$myArray[0][2] = "Diesel";
$myArray[0][3] = "Red and Blue";
$myArray[0][4] = "Catalytic Converter";

$myArray[0][0] = "Decepticons";
$myArray[0][1] = "Starscream";
$myArray[0][2] = "Flying Machine";
$myArray[0][3] = "Red";
$myArray[0][4] = "Girly Voice";

$myArray[0][0] = "Decepticons";
$myArray[0][1] = "Megatron";
$myArray[0][2] = "Pea Shooter";
$myArray[0][3] = "Purple and Black";
$myArray[0][4] = "Everything";

I would like to create a function that will cycle through the array and print something similar to (grouping every row by the value of the 1st column):

Autobots
-----------------
Bumblebee
Jazz
Optimus Prime

Decepticons
-----------------
Megatron
Starscream

Thus grouping and ordering the information that is printed by the value of the first column of the row. Here is my coding attempt:

[code]
// region
$previousRegion = "";
$currentRegion = "";
// company
$currentCompany = "";
$previousCompany = "";
// counter
$tradeCounter = 0;
foreach ($tradeInfo as $book) {
foreach ($book as $key => $value){
// region
if ( $key == 0 ) {
$currentRegion = $value;
if ( $currentRegion != $previousRegion ) {
if ( $tradeCounter != 0 ) { echo "<br />"; }
echo "<strong>".$value."</strong><br /><br />";
$previousCompany = "";
}
}
// company
if ( $key == 1 ) {
$currentCompany = $value;
if ( $currentCompany != $previousCompany ) { echo "<span style=\"font-weight:bold;color:#5d5d5d;\">".$value."</span><br /><br />"; }
}
if ( !empty($value) && $key != 0 && $key != 1) {
echo "$value <br />\n";
}
if ( $key == 0 ) { $previousRegion = $value; }
if ( $key == 1 ) { $previousCompany = $value; }
if ( $key == 4 ) { echo "<br />"; }
$tradeCounter ++;
}
}
[/code]

The variable names in my code do not reflect the sample data which I provided in this post. This code is just a reflection of my attempt to sort the array using foreach() and print the information how I would like it arranged. I hope this is clearer. Please let me know if you need additional details. Thanks for your response.

Cheers,

Fezzik
Link to comment
Share on other sites

You are mixing keys with values; sorting will be nigh on impossible like this, unless you have a whitelist of keys.

Reorganise your array:

[code]<?php

$array = array(
    'Autobots' => array(
        'Bumblebee',
        'Optimus Prime',
        /* etc */
    ),
    'Decepticons' => array(
        'Galvatron',
        'Starscream',
        /* etc */
    )
);

?>[/code]

then you can reference them as...
[code]<?php

foreach ($array['Autobots'] as $autobot)
{
    echo $autobot . ' transform!' . chr(10);
}

foreach ($array['Decepticons'] as $decepticon)
{
    echo $decepticon . ' transform!' . chr(10);
}

?>[/code]
Link to comment
Share on other sites

try[code]<?php
$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[1][0] = "Autobots";
$myArray[1][1] = "Jazz";
$myArray[1][2] = "Porsche";
$myArray[1][3] = "White";
$myArray[1][4] = "Steering Realignment";

$myArray[2][0] = "Autobots";
$myArray[2][1] = "Optimus Prime";
$myArray[2][2] = "Diesel";
$myArray[2][3] = "Red and Blue";
$myArray[2][4] = "Catalytic Converter";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Starscream";
$myArray[3][2] = "Flying Machine";
$myArray[3][3] = "Red";
$myArray[3][4] = "Girly Voice";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Megatron";
$myArray[3][2] = "Pea Shooter";
$myArray[3][3] = "Purple and Black";
$myArray[3][4] = "Everything";
function gr($group_key,$data_key,$aray) {
foreach ($aray as $a) $b[$a[$group_key]] .= " => $a[$data_key]<br />";
foreach ($b as $c => $d) $output .= "$c<br />------------<br />$d<br />";
return $output;
}

print gr(0,1,$myArray);
?>[/code]
Link to comment
Share on other sites

Thanks sasa! This function is splendid! I've been trying to modify your function so that it lists all the $data_key's associated with the $group_key but have been unsuccessful. Is this easily accomplished with the function you provided?

Cheers,

Fezzik
Link to comment
Share on other sites

[code]<?php
$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[1][0] = "Autobots";
$myArray[1][1] = "Jazz";
$myArray[1][2] = "Porsche";
$myArray[1][3] = "White";
$myArray[1][4] = "Steering Realignment";

$myArray[2][0] = "Autobots";
$myArray[2][1] = "Optimus Prime";
$myArray[2][2] = "Diesel";
$myArray[2][3] = "Red and Blue";
$myArray[2][4] = "Catalytic Converter";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Starscream";
$myArray[3][2] = "Flying Machine";
$myArray[3][3] = "Red";
$myArray[3][4] = "Girly Voice";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Megatron";
$myArray[3][2] = "Pea Shooter";
$myArray[3][3] = "Purple and Black";
$myArray[3][4] = "Everything";
function gr($group_key,$data_key,$aray) { //reorganize array
if (!is_array($data_key)) $data_key = array($data_key);
foreach ($aray as $a) {
$o1 = array();
foreach ($data_key as $e) $o1[] = $a[$e];
$b[$a[$group_key]][] = $o1;
}
return $b;
}
function out($a) { //output array
foreach ($a as $b => $c) {
$out .= $b."\n".'<table border="3">'."\n";
foreach ($c as $d) {
$out .= '<tr>'."\n";
foreach ($d as $e) $out .= "\t<td width=\"150\">$e</td>\n";
$out .= '</tr>'."\n";
}
$out .= '</table>'."\n<br />\n\n";
}
return $out;
}
$a = array(1,3,4,2);
$b = gr(0,$a,$myArray);
//print_r($b);
$c = out($b);
echo $c;
?>[/code]
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.