user1873632
user1873632

Reputation: 71

json_encode multiple arrays

I am trying to use ajax and json to pull arrays from this php. I am kind of familiar with the ajax on the front end, but i need to put each of these php arrays into a javascript array so i can make a list. Any help would be great. Thanks

<?php

    header('Content-Type: application/json');

    echo '{}';


    function getOptions($selection) {

        switch ($selection) {
            case 'colors':
                $arr = array(
                    0 => 'Red',
                    1 => 'Orange',
                    2 => 'Yellow',
                    3 => 'Green',
                    4 => 'Blue',
                    5 => 'Indigo',
                    6 => 'Violet'
                );
                break;
            case 'dogs':
                $arr = array(
                    0 => 'Labrador Retriever',
                    1 => 'Yorkshire Terrier',
                    2 => 'German Shepherd',
                    3 => 'Golden Retriever',
                    4 => 'Beagle',
                    5 => 'Dachshund',
                    6 => 'Boxer',
                    7 => 'Poodle',
                    8 => 'Shih Tzu',
                    9 => 'Miniature Schnauzer'
                );
                break;
            case 'fruits':
                $arr = array(
                    0 => 'Apples',
                    1 => 'Bananas',
                    2 => 'Cantaloupe',
                    3 => 'Grapefruit',
                    4 => 'Papaya',
                    5 => 'Mango',
                    5 => 'Strawberries',
                    6 => 'Watermelon'
                );
                break;
            case 'plants':
                $arr = array(
                    0 => 'Norfolk Island Pine',
                    1 => 'Peperomia',
                    2 => 'Grape Ivy',
                    3 => 'Fiddleleaf Fig',
                    4 => 'Snake Plant',
                    5 => 'English Ivy',
                    6 => 'Spider Plant',
                    7 => 'Hoya',
                    8 => 'Green Dracaena',
                    9 => 'Pothos'
                );
                break;
            default:
                $arr = array();
        }

        return $arr;

    }
echo json_encode($arr);

?>

Here is the ajax call

$.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: 'id=testdata',
                dataType: 'json',
                cache: false,
                success: function(result) {
                    var numbers = result
                    //not sure what to put here to pull separate arrays from the php
                    //should it be something like var colors = result.colors??
                    }           
                }
            });

Upvotes: 0

Views: 5483

Answers (4)

Ruan Mendes
Ruan Mendes

Reputation: 92294

If you want to return the whole structure as

{
  "colors": ["Red", "Orange", ...],
  "dogs": ["Lab", "Yorkie", ...], 
  ...
}

Then you can use

<?php
header('Content-Type: application/json');
$map = array(
    'colors' => array('Red', 'Orange', 'Yellow', 'Green', 'Blue'),
    'dogs' => array('Labrador Retriever', 'Yorkshire Terrier')
);
echo json_encode($map);

// js
$.ajax({
   type: 'POST',
   url: 'ajax.php',
   data: 'id=testdata',
   dataType: 'json',
   cache: false,
   success: function(result) {
        var numbers = result
        var colors = result.colors; // colors[0], colors[2]
        var dogs = results.dogs;            
   });

If you want to pass the key into the AJAX call, use the following

<?php
header('Content-Type: application/json');
$map = array(
    'colors' => array('Red', 'Orange', 'Yellow', 'Green', 'Blue'),
    'dogs' => array('Labrador Retriever', 'Yorkshire Terrier')
);
echo json_encode($map[$_POST['field']]);

// js
$.ajax({
   type: 'POST',
   url: 'ajax.php',
   data: 'field=colors',
   dataType: 'json',
   success: function(colors) {
        alert(colors[0] + colors[1] );
   });

Upvotes: 0

Paul
Paul

Reputation: 141877

Modify your PHP like so:

Remove the echo '{}'; line, and replace the line echo json_encode($arr); with

echo json_encode(array(
  'colors' => getSelections('colors'),
  'dogs'   => getSelections('dogs'),
  'fruits' => getSelections('fruits'),
  'plants' => getSelections('plants')
);

Then in your Javascript you can use result.colors[3], result.dogs[5], etc.

Upvotes: 1

sd1sd1
sd1sd1

Reputation: 1048

you can put them inside objects and then json_encode, then you can use jquery with dataType:'json' and parse them back on the browser

Upvotes: 0

abahet
abahet

Reputation: 10633

Why dont you just use json_encode of the array : http://php.net/manual/en/function.json-encode.php

Upvotes: 0

Related Questions