Json_encode多个数组

json_encode multiple arrays

本文关键字:数组 encode Json      更新时间:2023-09-26

我试图使用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??
                    }           
                }
            });

像这样修改你的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]

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

你可以把它们放在对象中然后json_encode,然后你可以使用jquery与dataType:'json'和解析他们回到浏览器

如果您想返回整个结构

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