json_encode多个数组

时间:2013-06-03 16:08:08

标签: javascript jquery ajax json

我正在尝试使用ajax和json从这个php中提取数组。我对前端的ajax很熟悉,但是我需要将每个这些php数组放入一个javascript数组中,这样我就可以制作一个列表。任何帮助都会很棒。感谢

<?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);

?>

这是ajax调用

$.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??
                    }           
                }
            });

4 个答案:

答案 0 :(得分:1)

像这样修改你的PHP:

删除echo '{}';行,并将行echo json_encode($arr);替换为

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

然后在您的Javascript中,您可以使用result.colors[3]result.dogs[5]

答案 1 :(得分:0)

为什么不使用数组的json_encode: http://php.net/manual/en/function.json-encode.php

答案 2 :(得分:0)

你可以将它们放在对象中,然后是json_encode,然后你可以使用jquery和dataType:'json'并在浏览器上解析它们

答案 3 :(得分:0)

如果要将整个结构返回为

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

然后你可以使用

<?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;            
   });

如果要将密钥传递给AJAX调用,请使用以下

<?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] );
   });