• We just launched and are currently in beta. Join us as we build and grow the community.

PHP Sending Array to URL using JSON Encode/Decode

AlvoErrado2

Tech Trends Analyst
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
134
Likes
34
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial features a way to send array to url using json encode/decode. Of course, there are a number of ways to send array to url but for me, I found this efficient when it comes to sending arrays. Also, I've included three ways to send to url which are via href, php header and script location. So, let's get started.

Creating Sample Array

First step is to create our sample array with the 3 ways to send to url. We name this as our "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Sending Array to URL using JSON Encode/Decode</title>
  5. </head>
  6. <body>
  7. <?php
  8. $myarray

    =

    array

    (
  9. array

    (

    'fruit'

    =>

    'Apple'

    ,

    'quantity'

    =>

    10

    )

    ,
  10. array

    (

    'fruit'

    =>

    'Orange'

    ,

    'quantity'

    =>

    20

    )

    ,
  11. array

    (

    'fruit'

    =>

    'Strawberry'

    ,

    'quantity'

    =>

    50

    )

    ,
  12. array

    (

    'fruit'

    =>

    'Mango'

    ,

    'quantity'

    =>

    30

    )

    ,
  13. )

    ;

  14. $encode

    =

    json_encode

    (

    $myarray

    )

    ;
  15. $url

    =

    rawurlencode

    (

    $encode

    )

    ;


  16. ?>
  17. <h4>Our Array:</h4> <?php

    print_r

    (

    $myarray

    )

    ;

    ?>
  18. <h4>Result:</h4>
  19. <ul>
  20. <?php
  21. foreach

    (

    $myarray

    as

    $data

    )

    :
  22. ?>
  23. <li><?php

    echo

    $data

    [

    'fruit'

    ]

    ?>

    : <?php

    echo

    $data

    [

    'quantity'

    ]

    ?>

    </li>

  24. <?php
  25. endforeach

    ;
  26. ?>
  27. </ul>
  28. <h3>Send via:</h4>
  29. <a href="goto.php?myarray=<?php

    echo

    $url

    ;

    ?>

    "><?php

    echo

    htmlentities

    (

    '<a> Tag'

    )

    ;

    ?>

    </a>
  30. <a href="header.php?myarray=<?php

    echo

    $url

    ;

    ?>

    ">PHP Header</a>
  31. <a href="script.php?myarray=<?php

    echo

    $url

    ;

    ?>

    ">Script Location</a>
  32. </body>
  33. </html>

Creating our Header Page

Next we create a header page that will direct us to our goto page. The purpose of this is to showcase the way to send via php header. We name this as "header.php".

  1. <?php
  2. $myarray

    =

    json_decode

    (

    $_GET

    [

    'myarray'

    ]

    ,

    true

    )

    ;

  3. //encode again
  4. $encode

    =

    json_encode

    (

    $myarray

    )

    ;
  5. $data

    =

    rawurlencode

    (

    $encode

    )

    ;

  6. //send via header
  7. header

    (

    'location: goto.php?myarray='

    .

    $data

    )

    ;
  8. ?>

Creating our Script Page

If we have a page for header, we are also going to create a pge for script that will also redirect as to our goto page. We name this as our "script.php".

  1. <?php
  2. $myarray

    =

    json_decode

    (

    $_GET

    [

    'myarray'

    ]

    ,

    true

    )

    ;

  3. //encode again
  4. $encode

    =

    json_encode

    (

    $myarray

    )

    ;
  5. $data

    =

    rawurlencode

    (

    $encode

    )

    ;

  6. //send via location
  7. ?>
  8. <script>
  9. var data='<?php

    echo

    $data

    ;

    ?>

    ';
  10. window.location.href='goto.php?myarray='+data;
  11. </script>

  12. <?php

  13. ?>

Creating our Goto Page

Lastly, we create our goto page. This page will retrieve our array that we send to url. We name this as "goto.php".

  1. <?php

    $myarray

    =

    json_decode

    (

    $_GET

    [

    'myarray'

    ]

    ,

    true

    )

    ;

    ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>PHP Sending Array to URL using JSON Encode/Decode</title>
  6. </head>
  7. <body>
  8. <h4>Our Array:</h4> <?php

    print_r

    (

    $myarray

    )

    ;

    ?>
  9. <h4>Result:</h4>
  10. <ul>
  11. <?php
  12. foreach

    (

    $myarray

    as

    $data

    )

    :
  13. ?>
  14. <li><?php

    echo

    $data

    [

    'fruit'

    ]

    ?>

    : <?php

    echo

    $data

    [

    'quantity'

    ]

    ?>

    </li>

  15. <?php
  16. endforeach

    ;
  17. ?>
  18. </ul>
  19. <a href="index.php">Back</a>
  20. </body>
  21. </html>

That ends this tutorial. Hope this helps. Happy coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top