PHP Sending Array to URL using JSON Encode/Decode

Submitted by: 
Language: 
Visitors have accessed this post 410 times.

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.  
  15. $encode=json_encode($myarray);
  16. $url=rawurlencode($encode);
  17.  
  18.  
  19. ?>
  20. <h4>Our Array:</h4> <?php print_r($myarray); ?>
  21. <h4>Result:</h4>
  22. <ul>
  23. <?php
  24. foreach ($myarray as $data):
  25. ?>
  26. <li><?php echo $data['fruit'] ?>: <?php echo $data['quantity'] ?> </li>
  27.  
  28. <?php
  29. endforeach;
  30. ?>
  31. </ul>
  32. <h3>Send via:</h4>
  33. <a href="goto.php?myarray=<?php echo $url; ?>"><?php echo htmlentities('<a> Tag'); ?></a>
  34. <a href="header.php?myarray=<?php echo $url; ?>">PHP Header</a>
  35. <a href="script.php?myarray=<?php echo $url; ?>">Script Location</a>
  36. </body>
  37. </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.  
  4. //encode again
  5. $encode=json_encode($myarray);
  6. $data=rawurlencode($encode);
  7.  
  8. //send via header
  9. header('location: goto.php?myarray='.$data);
  10. ?>

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.  
  4. //encode again
  5. $encode=json_encode($myarray);
  6. $data=rawurlencode($encode);
  7.  
  8. //send via location
  9. ?>
  10. <script>
  11. var data='<?php echo $data; ?>';
  12. window.location.href='goto.php?myarray='+data;
  13. </script>
  14.  
  15. <?php
  16.  
  17. ?>

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.  
  16. <?php
  17. endforeach;
  18. ?>
  19. </ul>
  20. <a href="index.php">Back</a>
  21. </body>
  22. </html>

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.