dpv21589 2019-08-19 09:01
浏览 97

如何输出子阵列?

I have a problem to output subarray values properly. I use Codestar Framework to create options.

Here is my options code:

array(
    'id'     => 'rows',
    'type'   => 'group',
    'title'  => 'Rows',
    'accordion_title_auto'   => false,
    'accordion_title_prefix' => 'Row',
    'accordion_title_number' => true,
    'fields'  => array(
        array(
            'id'     => 'columns',
            'type'   => 'group',
            'title'  => 'Columns',
            'accordion_title_auto'   => false,
            'accordion_title_prefix' => 'Column',
            'accordion_title_number' => true,
            'fields'    => array(
                array(
                    'id'      => 'column-color',
                    'type'    => 'select',
                    'title'   => 'Color',
                    'options' => array(
                        'red'    => 'Red',
                        'blue'   => 'Blue',
                        'green'  => 'Green',
                    ),
                ),
                array(
                    'id'       => 'column-price',
                    'type'     => 'select',
                    'title'    => 'Price',
                    'options'  => array(
                        'low'     => 'Low',
                        'medium'  => ' Medium',
                        'high'    => ' High',
                    ),
                ),
            ),
        ),
    ),

),

I have done it so far and it works. But how to output color and price? I can't figure it out how to do it correctly.

$rows = $options['rows'];
foreach ( $rows as $id => $name ) { 
   echo '<div class="row-'. $id .'">';
   foreach ( $name['columns'] as $id_col => $name_col ) {
      echo '<div class="column-'. $id_col .'">';
      echo'...';
      echo '</div>';
   }
   echo '</div>';
}

<div class="row-0">
   <div class="column-0">...</div>
   <div class="column-1">...</div>
</div>
<div class="row-1">
   <div class="column-0">...</div>
</div>

I want to output look like this:

<div class="row-0">
    <div class="column-0 color-red">
        Price - Low
    </div>
    <div class="column-1 color-green">
        Price - Medium
    </div>
</div>

</div>
<div class="row-1">
    <div class="column-0 color-blue">
        Price - High
    </div>
</div>

Can anyone help me with this? I will be very grateful, thanks

UPDATE

This code displays all values:

foreach ( $rows as $id => $name ) {
    echo '<div class="row-'. $id .'">';
    foreach ( $name['columns'] as $id_col => $name_col ) {
        echo '<div class="column-'. $id_col .' color-'. $name_col['column-color'] .'">';
            echo $name_col['column-content'];
        echo '</div>';
    }
    echo '</div>';
}

I just don't know how to make 'column-content' output not an option key string but a key value/label

  • 写回答

1条回答 默认 最新

  • drnf09037160 2019-08-19 09:27
    关注

    You can try something like this:

    $options = array(
        'id'     => 'rows',
        'type'   => 'group',
        'title'  => 'Rows',
        'accordion_title_auto'   => false,
        'accordion_title_prefix' => 'Row',
        'accordion_title_number' => true,
        'fields'  => array(
            array(
                'id'     => 'columns',
                'type'   => 'group',
                'title'  => 'Columns',
                'accordion_title_auto'   => false,
                'accordion_title_prefix' => 'Column',
                'accordion_title_number' => true,
                'fields'    => array(
                    array(
                        'id'      => 'column-color',
                        'type'    => 'select',
                        'title'   => 'Color',
                        'options' => array(
                            'red'    => 'Red',
                            'blue'   => 'Blue',
                            'green'  => 'Green',
                        ),
                    ),
                    array(
                        'id'       => 'column-price',
                        'type'     => 'select',
                        'title'    => 'Price',
                        'options'  => array(
                            'low'     => 'Low',
                            'medium'  => ' Medium',
                            'high'    => ' High',
                        ),
                    ),
                ),
            ),
        ),
    );
    
    
    $fields = $options['fields'];
    
    
        print_r( $fields );
    
    
    foreach ( $fields as $key => $field ) {
    
        $color_options = $field['fields'][0]['options'];
    
        $price_options = $field['fields'][1]['options'];
    
        print_r( $price_options );  
    }
    

    which will generate something like this:

    Array
    (
        [0] => Array
            (
                [id] => columns
                [type] => group
                [title] => Columns
                [accordion_title_auto] => 
                [accordion_title_prefix] => Column
                [accordion_title_number] => 1
                [fields] => Array
                    (
                        [0] => Array
                            (
                                [id] => column-color
                                [type] => select
                                [title] => Color
                                [options] => Array
                                    (
                                        [red] => Red
                                        [blue] => Blue
                                        [green] => Green
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [id] => column-price
                                [type] => select
                                [title] => Price
                                [options] => Array
                                    (
                                        [low] => Low
                                        [medium] =>  Medium
                                        [high] =>  High
                                    )
    
                            )
    
                    )
    
            )
    
    )
    Array
    (
        [low] => Low
        [medium] =>  Medium
        [high] =>  High
    )
    
    评论

报告相同问题?