2

I am trying to read a perl associative array in my PHP script. For this firstly I am trying to convert perl hash to JSON using JSON module.

Below is the perl script I am using to convert associative array to JSON. The file toc.pl has the associative array "asc_array".

use JSON;
$j = new JSON;

require'toc.pl';

print encode_json \%asc_array; 

The asc_array looks like

%asc_array  = (
    "1"            => 'Introduction',
    "1.1"          => 'Scope',
    "1.2"          => 'Purpose',
    "2"            => 'Terminology',
    "2.1"          => 'Definitions',
    "2.2"          => 'Service Primitives',
    "2.3"          => 'Abbreviations',
    "2.4"          => 'Acronyms',
);

Here I am facing an issue and that is after converting it to JSON the order of associative array elements change.

So my question is How can I keep the order of elements even after converting it to JSON?

And, after converting it to JSON I am reading the JSON in PHP script.

Is there any better way to read perl associative array in PHP script?

10
  • 1
    An associative array (hash) haven't any order. Commented Sep 18, 2014 at 11:05
  • 1
    Why do you want to have a specific order? Commented Sep 18, 2014 at 11:06
  • 1
    stackoverflow.com/a/4920304/223226 So you can't rely on hash keys order, nor on javascript object properties order. Use array instead. Commented Sep 18, 2014 at 11:08
  • @M42 : I am storing table of contents with section number as keys and title as value. I used perl associative array for that. While looping over it in perl it was in order but, after converting to JSON the order changed. So what would you suggest to solve the above? Commented Sep 18, 2014 at 11:47
  • 1
    What Perl resources are you reading where you are picking up the term "associative arrays"? Since Perl 5 was release twenty years ago, they have been called "hashes" in Perl. Whatever resources you are using seem to be horribly out of date. Commented Sep 18, 2014 at 12:05

2 Answers 2

2

Use JSON->canonical to sort the keys

use JSON;

my %asc_array = (
    "1"   => 'Introduction',
    "1.1" => 'Scope',
    "1.2" => 'Purpose',
    "2"   => 'Terminology',
    "2.1" => 'Definitions',
    "2.2" => 'Service Primitives',
    "2.3" => 'Abbreviations',
    "2.4" => 'Acronyms',
);

print JSON->new->canonical(1)->encode( \%asc_array ), "\n";

Output:

{"1":"Introduction","1.1":"Scope","1.2":"Purpose","2":"Terminology","2.1":"Definitions","2.2":"Service Primitives","2.3":"Abbreviations","2.4":"Acronyms"}
Sign up to request clarification or add additional context in comments.

1 Comment

Please note: canonical does an alphabetical sort. Therefore 10.2 will come before 2.
1

Since your hash keys are easily sortable, why not just sort the data once it's been received by the PHP script?

i.e.

use JSON::PP;
my %asc_array  = ( 
    "1"    => 'Introduction',
    "1.1"  => 'Scope',
    "1.2"  => 'Purpose',
    "2"    => 'Terminology',
    "2.1"  => 'Definitions',
    "2.2"  => 'Service Primitives',
    "2.3"  => 'Abbreviations',
    "2.4"  => 'Acronyms',
);

my $json = JSON::PP->new;
print $json->encode(\%asc_array);

Then, in your PHP script:

# replace this with whatever method you're using to get your JSON
# I've jumbled up the order to demonstrate the power of sorting.
$json = '{"2.3":"Abbreviations", "2.1":"Definitions", "1":"Introduction", "2.4":"Acronyms", "1.1":"Scope", "2":"Terminology", "2.2":"Service Primitives", "1.2":"Purpose"}';

$decoded = json_decode( $json, true );
ksort($decoded);

print_r($decoded);

Output:

Array
(
    [1] => Introduction
    [1.1] => Scope
    [1.2] => Purpose
    [2] => Terminology
    [2.1] => Definitions
    [2.2] => Service Primitives
    [2.3] => Abbreviations
    [2.4] => Acronyms
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.