Jump to content

Recommended Posts

I'm having some trouble with an array I am ordering I've tried a few sorting options, the latest is usort. Currently, it's looking at the first character of each number and ordering based on that, rather than treating the numeric characters as a full number.

I've tried adding substr but it hasn't made any difference...

The array I'm getting back at the moment looks like this:

[0] => Array
    (
        [0] => 1 The Street, The City, The County
        [1] => FriA
    )

[1] => Array
    (
        [0] => 10 The Street, The City, The County
        [1] => FriB
    )

[2] => Array
    (
        [0] => 11 The Street, The City, The County
        [1] => FriA
    )


The first value is the address, the second is a schedule. 2 The Street is currently displaying after 19, rather than 1.

Here is the code:

function cmp( $a, $b ) {
    return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) );
    if ( $a == $b ) {
        return 0;
    }
    return ( $a < $b ) ? -1 : 1;
}

foreach ( $json[ 'candidates' ] as $value ) {
    $address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ];
    $code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ];
    $a[] = array( $address, $code );
    usort($a, "cmp");
    $index++;
}

echo '<pre>';print_r( $a );echo '</pre>';

Any help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/
Share on other sites

Firstly

function cmp( $a, $b ) {
    return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) );
    if ( $a == $b ) {
        return 0;
    }
    return ( $a < $b ) ? -1 : 1;
}

The last 4 lines of that function are redundant and never executed. All you need is the "return strcmp(...)"

 

Secondly, you should sort the array *after* you have built it. You are currently sorting inside the loop each time you add a new element.

18 minutes ago, mac_gyver said:

 

 

Thanks so much for your help guys, as you can probably tell I'm new at this!

I've made the changes that you've suggested but now the order is randomised - I'm sure it's something I'm missing, so apologies in advance, I can tell it's pretty close...

Just now, benanamen said:

Were is this array coming from in the first place, a database? I smell an XY problem.

Hi, it's coming from an ESRI locatorhub REST API. Here's the latest code -

function cmp($a, $b)
{
  $a = (int)$a;
  $b = (int)$b;

  return $a - $b;
}

foreach ( $json[ 'candidates' ] as $value ) {
	$address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ];
	$code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ];
	$a[] = array( $address, $code );
	$index++;
}
usort($a, 'cmp');
echo '<pre>';print_r( $a );echo '</pre>';

 

Works for me. Have you checked your data?

$arr = [
            [ 0 => '1 The Street, The City, The County',    1 => 'FriA'   ],
            [ 0 => '21 The Street, The City, The County',   1 => 'FriA'   ],
            [ 0 => '10 The Street, The City, The County',   1 => 'FriB'   ]
        ];
usort ($arr, function($a,$b) { 
                return (int)$a[0] - (int)$b[0]; 
                });
echo '<pre>', print_r($arr, 1), '</pre>';

/***** RESULTS ******/

        Array
        (
            [0] => Array
                (
                    [0] => 1 The Street, The City, The County
                    [1] => FriA
                )

            [1] => Array
                (
                    [0] => 10 The Street, The City, The County
                    [1] => FriB
                )

            [2] => Array
                (
                    [0] => 21 The Street, The City, The County
                    [1] => FriA
                )

) 

 

  • Thanks 1
6 minutes ago, Barand said:

Works for me. Have you checked your data?


$arr = [
            [ 0 => '1 The Street, The City, The County',    1 => 'FriA'   ],
            [ 0 => '21 The Street, The City, The County',   1 => 'FriA'   ],
            [ 0 => '10 The Street, The City, The County',   1 => 'FriB'   ]
        ];
usort ($arr, function($a,$b) { 
                return (int)$a[0] - (int)$b[0]; 
                });
echo '<pre>', print_r($arr, 1), '</pre>';

/***** RESULTS ******/

        Array
        (
            [0] => Array
                (
                    [0] => 1 The Street, The City, The County
                    [1] => FriA
                )

            [1] => Array
                (
                    [0] => 10 The Street, The City, The County
                    [1] => FriB
                )

            [2] => Array
                (
                    [0] => 21 The Street, The City, The County
                    [1] => FriA
                )

) 

 

Ok, now it's working. Just tweaked the structure to match your example and it's working like a dream. Thank you so so much

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.