如何将API返回JSON的Beds字段转为唯一值数组并生成HTML复选框?
Hey there! No worries at all—let's walk through how to solve this step by step. You're already on the right track with fetching and parsing the JSON; we just need to tweak how you handle the bedroom values and generate the HTML.
Step 1: Extract, Deduplicate, and Convert Bedroom Values
Instead of looping through each item to grab the Beds values manually, we can use some handy PHP array functions to streamline this:
// Fetch and parse your JSON data (your existing code) $data = file_get_contents(endpoint); $parsed_json = json_decode($data, true); // 1. Extract all "Beds" values from the parsed array $all_beds = array_column($parsed_json, 'Beds'); // 2. Remove duplicate values $unique_beds = array_unique($all_beds); // 3. Convert string values to integers (matches your desired [1,2,3,4] format) $unique_bed_numbers = array_map('intval', $unique_beds); // Optional: Sort the array to ensure numbers are in ascending order sort($unique_bed_numbers);
Let me break down what each function does:
array_column(): Pulls all the values from the "Beds" key across every sub-array in$parsed_json—way more efficient than writing a custom foreach loop for this task.array_unique(): Filters out any duplicate entries, so you only get each bedroom count once.array_map('intval', ...): Converts each string value (like "1") to an integer (like 1) to match the clean numeric array format you wanted.sort(): Makes sure the numbers are in order, which creates a consistent, user-friendly UI.
Step 2: Generate the Checkbox HTML
Now that you have your clean, sorted array of bedroom numbers, you can loop through it to build your checkboxes:
// Initialize your output string $output = '<div id="property_floorplan" class="checkbox-container">'; $output .= '<label>Refine:</label>'; // Loop through each unique bedroom number foreach ($unique_bed_numbers as $bed_number) { // Optional: Handle pluralization (e.g., "1 Bed" vs "2 Beds") $bed_label = $bed_number . ' ' . ($bed_number === 1 ? 'Bed' : 'Beds'); // Add the checkbox markup to your output $output .= '<div class="checkbox-inline"> <label class="bedrooms"> <input type="checkbox" data-bedrooms="' . $bed_number . '">' . $bed_label . ' </label> </div>'; } // Close the container div $output .= '</div>'; // Output the final HTML (use this wherever you need it in WordPress) echo $output;
Full Combined Code
Here's the full code put together so you can drop it in directly:
// Fetch API data $data = file_get_contents(endpoint); $parsed_json = json_decode($data, true); // Process bedroom values $all_beds = array_column($parsed_json, 'Beds'); $unique_beds = array_unique($all_beds); $unique_bed_numbers = array_map('intval', $unique_beds); sort($unique_bed_numbers); // Build HTML output $output = '<div id="property_floorplan" class="checkbox-container">'; $output .= '<label>Refine:</label>'; foreach ($unique_bed_numbers as $bed_number) { $bed_label = $bed_number . ' ' . ($bed_number === 1 ? 'Bed' : 'Beds'); $output .= '<div class="checkbox-inline"> <label class="bedrooms"> <input type="checkbox" data-bedrooms="' . $bed_number . '">' . $bed_label . ' </label> </div>'; } $output .= '</div>'; // Render the output in WordPress echo $output;
This will generate exactly the unique bedroom checkboxes you're aiming for. If you hit any snags with integrating this into your WordPress setup, feel free to ask for more clarification!
内容的提问来源于stack exchange,提问作者RonE

