How to make a JSON object with PHP array for jQuery autocomplete?

This php code creates JSON object like ” availableTags “

	var availableTags = [
	{key: "1",value: "NAME 1"},{key: "2",value: "NAME 2"},{key: "3",value: "NAME 3"},{key: "4",value: "NAME 4"},{key: "5",value: "NAME 5"}
	 ];

$Acc_Name_List is your PHP array which have key and value. You can manipulate as you want, make function or add into your class.

            var availableTags = [
                $value_name)
                {
                  if ($key != 0 && $key != "" && $value_name != "")
                  {
                   $list_key = "key: ". '"'.$key.'"';
                   $list_value = "value: ". '"'.$value_name.'"';
                   // $string .= $list_value.',';
                     $string .="{". $list_key.','.$list_value."},";
                  }
                }
                $string = substr($string, 0, -1);
               // echo $result;
                echo $string;
                ?>
Share

Leave a Reply

Your email address will not be published. Required fields are marked *