0

I have an array like below and would like to create a <html> form out of it. I tried to accomplish that but I can't seem to figure out how to loop through the inner/nested array, that have the values that i want to use.

Can anyone help me out? Just to be clear, I would like a html output like:

<form method="get">
    <h2>I have</h2>
    <input type="checkbox" value="own studio">own studio<br>
    <input type="checkbox" value="mobile studio">mobile studio<br>
    <input type="checkbox" value="makeup artist">makeup artist<br>
    <h2>Customers</h2>
    <select>
        <option value="Private">Private</option>
        <option value="Business">Business</option>
    </select>
</form>

Function

<?php
function get_listing_cfs() {
global $wpdb; 
$serialized=$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='va_form'"); 
$array=unserialize($serialized);
$source = array_filter($array, function($el) {
    return !(
            $el['props']['label'] == 'Exclude' ||
            $el['props']['label'] == 'Exclude 2'
            );
});
//echo '<pre>'.print_r($source, true).'</pre>';
$toreturn = '<form method="get">';
    foreach ($source as $key => $item) {
    $toreturn .= '<h2>'.$source[$key]['props']['label'].'</h2>';
        if ($source[$key]['type'] == 'select'){
        $toreturn .= '<select>';
        // Scan through inner loop
            foreach ($item as $value =>$data) {
               $toreturn .='<option value="'.$value['props']['options'].'">'.$value['props']['options'].'</option>';
            }
        $toreturn .='</select>';    
        }
        if ($source[$key]['type'] == 'checkbox'){
        // Scan through inner loop
            foreach ($item as $value =>$data) {
               $toreturn .='<input type="checkbox" value="'.$value['props']['options'].'">'.$value['props']['options'].'<br />';
            }
        }
    }
    $toreturn .= '</form>';
return $toreturn;
}
?>

Array

Array
(
    [0] => Array
        (
            [id] => app_i_have
            [type] => checkbox
            [props] => Array
                (
                    [required] => 0
                    [label] => I have
                    [tip] => 
                    [options] => Array
                        (
                            [0] => Array
                                (
                                    [baseline] => 0
                                    [value] => mobile studio
                                )

                            [1] => Array
                                (
                                    [baseline] => 0
                                    [value] => own studio
                                )

                            [2] => Array
                                (
                                    [baseline] => 0
                                    [value] => makeup artist
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => app_customers
            [type] => select
            [props] => Array
                (
                    [required] => 0
                    [label] => Customers
                    [tip] => 
                    [options] => Array
                        (
                            [0] => Array
                                (
                                    [baseline] => 0
                                    [value] => Private
                                )

                            [1] => Array
                                (
                                    [baseline] => 0
                                    [value] => Business
                                )

                        )

                )

        )

)
2
  • Where are you having trouble? Besides not making good use of your loop variables, you've got nested looping. Whats not working? Commented Oct 30, 2017 at 16:07
  • Hi, I am not very known with arrays. The problem is with the nested arrays. I am trying to get the [value] from the array(where it says '// Scan through inner loop'), but it is not working. Commented Oct 30, 2017 at 16:09

1 Answer 1

1

You're not considering your data structure at all.

foreach ($item as $value =>$data) {
    $toreturn .='<option value="'.$value['props']['options'].'">'.$value['props']['options'].'</option>';
}

In this, $item is

[props] => Array
    (
        [required] => 0
        [label] => I have
        [tip] => 
        [options] => Array

So looping through it would be looping through the properties, not the values. You want to loop $item['options'].

Over all, if you're not experienced with array (a base and necessary type in PHP), I suggest you go back and do some starter material before trying something like this.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for clarifying a bit, but i still don't know how to loop through the nested arrays and it is not really up to me to go back. This is the array i get from the database. Quite figured out how to return the array data that i want, but the nested array. Any help is greatly appreciated.
I put the stuff you need to loop through above. You need to loop through the array of options you want to display, not just the group. The structure you're getting back from the database is fine; you just need to actually loop through the values you want. If you're not following why you need to loop through $items['options'], I'd say you really need to go back and learn the basics first.
Hi, thanks. Yes i do know why i should loop through the $item[options] but i don't understand why. The amount of options differ per array..How can i get the different option values?
Why should you loop through options? Because you're trying to display the options... What does the number of options matter? You're looping. You're telling me you know why you should loop through options and asking me how you can get the different options at the same time.
Different options as in different values (options for the form).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.