脚本宝典收集整理的这篇文章主要介绍了php – 将json数据输入智能建议插件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
array('image' => 'assets/images/fruITs/apple.jpg','description' => 'One of America\'s favorite fruits.'),'AvoCADo' => array('image' => 'assets/images/fruits/avocado.jpg','description' => 'The avocado is a dense,evergreen tree,shedding many leaves in early sPRing.'),'Banana' => array('image' => 'assets/images/fruits/banana.jpg','description' => 'Bananas are fast-growing herbaceous PErennials arising From underground rhizomes.'),'GooseBerry' => array('image' => 'assets/images/fruits/gooseBerry.jpg','description' => 'Gooseberries are deciduous shrubs.'),'Grape' => array('image' => 'assets/images/fruits/grape.jpg','description' => 'Grapes come in large clusters.'),'Jackfruit' => array('image' => 'assets/images/fruits/jackfruit.jpg','description' => 'The jackfruit tree is handsome and stately.'),'Mango' => array('image' => 'assets/images/fruits/mango.jpg','description' => 'Mango trees make handsome landscape specimens and shade trees.'),'Papaya' => array('image' => 'assets/images/fruits/papaya.jpg','description' => 'The papaya is a short-lived,fast-growing,woody,large herb to 10 or 12 feet in height. It is also regarded by some as being delicIoUs.'),'Peach' => array('image' => 'assets/images/fruits/peach.jpg','description' => 'These are great in the summertime.'),'Pear' => array('image' => 'assets/images/fruits/pear.jpg','description' => 'Pears are delicIoUs fruits.'),'Pineapple' => array('image' => 'assets/images/fruits/pineapple.jpg','description' => 'The pineapple plant is a herbaceous perennial,2-1/2 to 5 ft.'),'Rose Apple' => array('image' => 'assets/images/fruits/rose_apple.jpg','description' => 'The rose apple is a highly decorative evergreen large shrub.'),'Tamarind' => array('image' => 'assets/images/fruits/tamarind.jpg','description' => 'The bright green,pinnate foliage is dense and feathery in appearance.'),'White Sapote' => array('image' => 'assets/images/fruits/white_sapote.jpg','description' => 'The white sapote forms a medium to very large evergreen tree.'),); $vegetables = array( 'Alfalfa' => array('image' => 'assets/images/fruits/alfalfa.jpg','description' => 'One cup of raw,sprouted alfalfa seeds,contains 1.32 grams of protein.'),'Artichoke' => array('image' => 'assets/images/fruits/artichoke.jpg','description' => 'One medium artichoke cooked with no added salt has 3.47 grams protein.'),'Asparagus' => array('image' => 'assets/images/fruits/asparagus.jpg','description' => 'Half cup (about 6 spears) cooked with no added salt contains 2.16 grams of protein.'),'Broccoli' => array('image' => 'assets/images/fruits/broccoli.jpg','description' => 'Half cup of broccoli,cooked with no added salt contains 1.86 grams protein.'),'Carrots' => array('image' => 'assets/images/fruits/carrots.jpg','description' => 'Half cup cooked with no added salt contains 0.59 grams protein.'),'Celery' => array('image' => 'assets/images/fruits/celery.jpg','description' => 'One cup of celery,cooked,boiled,drained with no added salt has 1.25 grams protein.'),'Corn' => array('image' => 'assets/images/fruits/corn.jpg','description' => 'One large ear of yellow corn,cooked with no salt contains 4.02 grams protein.'),'Green Pepper' => array('image' => 'assets/images/fruits/green_pepper.jpg','description' => 'One small raw pepper contains 0.64 grams protein.'),'Mushroom' => array('image' => 'assets/images/fruits/mushroom.jpg','description' => 'Half a cup of raw mushrooms contains 1.08 grams of protein.'),'Onion' => array('image' => 'assets/images/fruits/onion.jpg','description' => 'One small onion cooked without salt contains 0.82 grams protein.'),'Potato' => array('image' => 'assets/images/fruits/potato.jpg','description' => 'One medium baked potato without salt contains 4.33 grams of protein.'),'Spinach' => array('image' => 'assets/images/fruits/spinach.jpg','description' => 'One cup of raw spinach contains 0.86 grams of protein.'),'Squash' => array('image' => 'assets/images/fruits/squash.jpg','description' => 'One cup of sliced summer squash,boiled with no added salt contains 1.87 grams of protein.'),); ?>
这个数组被传递到’seArch_multiple.PHP’文件中,我希望将其作为JSON传递:
array(),'vegetables' => array()); foreach ($fruits as $name => $data) { if (strIPOs($name,$q) !== false) { $results['fruits'][$name] = $data; } } foreach ($vegetables as $name => $data) { if (stripos($name,$q) !== false) { $results['vegetables'][$name] = $data; } } /* Get the data into a format that Smart Suggest will read (see documentation). */ $final_fruits = array('header' => array(),'data' => array()); $final_fruits['header'] = array( 'title' => 'Fruits',# Appears at the top of this category 'num' => count($results['fruits']),# Displayed as the total number of results. 'limit' => 5 # An arbitrary number that you want to limit the results to. ); foreach ($results['fruits'] as $name => $data) { $final_fruits['data'][] = array( 'Primary' => $name,# Title of result row 'secondary' => $data['description'],# Description below title on result row 'image' => $data['image'],# Optional URL of 40x40px image 'onclick' => 'alert(\'You clicked on the '.$name.' fruit!\');',# JavaScript to call when this result is clicked on 'fill_text' => strtolower($name) # Used for "auto-complete fill style" example ); } $final_vegetables = array('header' => array(),'data' => array()); $final_vegetables['header'] = array( 'title' => 'Vegetables',# Appears at the top of this category 'num' => count($results['vegetables']),# Displayed as the total number of results. 'limit' => 5 # An arbitrary number that you want to limit the results to. ); foreach ($results['vegetables'] as $name => $data) { $final_vegetables['data'][] = array( 'primary' => $name,# Optional URL of 40x40px image 'onclick' => 'alert(\'You clicked on the '.$name.' vegetable!\');',# JavaScript to call when this result is clicked on 'fill_text' => strtolower($name) # Used for "auto-complete fill style" example ); } /* Output JSON */ $final = array($final_fruits,$final_vegetables); header('Content-type: application/json'); echo json_encode($final); die(); ?>
我想知道如何将json数据传递到’search_multiple.PHP’文件中.例如:
<?PHP $fruits = '{ "Apple":{"image":"assets/images/fruits/apple.jpg","description":"One of America\'s favorite fruits."},"Avocado":{"image":"assets/images/fruits/avocado.jpg","description":"The avocado is a dense,shedding many leaves in early spring."} "Banana":{"image":"assets/images/fruits/banana.jpg","description":"Bananas are fast-growing herbaceous perennials arising from underground rhizomes."},"GooseBerry":{"image":"assets/images/fruits/gooseBerry.jpg","description":"Gooseberries are deciduous shrubs."} "Grape":{"image":"assets/images/fruits/grape.jpg","description":"Grapes come in large clusters."},"Jackfruit":{"image":"assets/images/fruits/jackfruit.jpg","description":"The jackfruit tree is handsome and stately."} "Mango":{"image":"assets/images/fruits/mango.jpg","description":"Mango trees make handsome landscape specimens and shade trees."},"Papaya":{"image":"assets/images/fruits/papaya.jpg","description":"The papaya is a short-lived,large herb to 10 or 12 feet in height. It is also regarded by some as being delicIoUs."} "Peach":{"image":"assets/images/fruits/peach.jpg","description":"These are great in the summertime."},"Pear":{"image":"assets/images/fruits/pear.jpg","description":"Pears are delicIoUs fruits."},"Pineapple":{"image":"assets/images/fruits/pineapple.jpg","description":"The pineapple plant is a herbaceous perennial,2-1/2 to 5 ft."} "Rose Apple":{"image":"assets/images/fruits/rose_apple.jpg","description":"The rose apple is a highly decorative evergreen large shrub."},"Tamarind":{"image":"assets/images/fruits/tamarind.jpg","description":"The bright green,pinnate foliage is dense and feathery in appearance."} "White Sapote":{"image":"assets/images/fruits/white_sapote.jpg","description":"One cup of sliced summer squash,boiled with no added salt contains 1.87 grams of protein."},}'; $vegetables = '{ "Alfalfa":{"image":"assets/images/fruits/apple.jpg","Artichoke":{"image":"assets/images/fruits/avocado.jpg",shedding many leaves in early spring."} "Asparagus":{"image":"assets/images/fruits/banana.jpg","Broccoli":{"image":"assets/images/fruits/gooseBerry.jpg","description":"Gooseberries are deciduous shrubs."} "Carrots":{"image":"assets/images/fruits/grape.jpg","Celery":{"image":"assets/images/fruits/jackfruit.jpg","description":"The jackfruit tree is handsome and stately."} "Corn":{"image":"assets/images/fruits/mango.jpg","Green Pepper":{"image":"assets/images/fruits/papaya.jpg",large herb to 10 or 12 feet in height. It is also regarded by some as being delicIoUs."} "Mushroom":{"image":"assets/images/fruits/peach.jpg","Onion":{"image":"assets/images/fruits/pear.jpg","Potato":{"image":"assets/images/fruits/pineapple.jpg",2-1/2 to 5 ft."} "Spinach":{"image":"assets/images/fruits/rose_apple.jpg","Squash":{"image":"assets/images/fruits/tamarind.jpg",pinnate foliage is dense and feathery in appearance."} }'; ?>
文档是在http://jamesskidmore.com/scripts/smartsuggest/
我已经在http://kabeerpc.tk举办了我目前的进展.
<?PHP $fruits_json = '{ "Apple":{"image":"assets/images/fruits/apple.jpg",evergreen tree."}}'; $vegetables_json = '{ "Alfalfa":{"image":"assets/images/fruits/pear.jpg","description":"An american alfafa."},"Artichoke":{"image":"assets/images/fruits/peach.jpg","description":"An American artichoke."}}'; // Decode input JSON $fruits = json_decode($fruits_json,true); $vegetables = json_decode($vegetables_json,true); $q = isset($_GET['q']) ? $_GET['q'] : ''; $final = array( _construct_smartSuggestion('fruits',$fruits,$q),_construct_smartSuggestion('vegetables',$vegetables,$q) ); header('Content-type: application/json'); die(json_encode($final)); function _construct_smartSuggestion($name,$described,$q) { $data = array(); foreach ($described as $dname=>$d) { // Search for the search query if (stripos($dname,$q) === false && stripos($d['description'],$q) === false) { continue; } $data[] = array('primary' => $dname,'secondary' => $d['description'],'image' => $d['image']); } $header = array('title'=>$name,'num'=>count($data),'limit'=>5); return array('header'=>$header,'data'=>$data); }
确保您的JSON序列化有效.您的示例JSON在许多条目(例如Avocado)之后缺少逗号.
Here’s a live demo.搜索美国人将在那里产生四个可用结果中的三个.
以上是脚本宝典为你收集整理的php – 将json数据输入智能建议插件全部内容,希望文章能够帮你解决php – 将json数据输入智能建议插件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。