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