关联数组中的关联数组

时间:2015-08-08 00:20:05

标签: php arrays foreach get associative-array

我是PHP新手,我正在尝试在命名数组中应用命名数组。我已经能够应用每个绳索名称(即“Poly Soak”)以及img,title,blurb,desc等,但是我很难尝试将$specTable应用到表格中。我也试图申请$applications$benefits但没有运气。

我正在使用GET命令生成每个页面。页面填充如下:

water-rescue-ropes.php?value = aqua-d
water-rescue-ropes.php?value = poly-soak ... etc

function strip_bad_chars( $input ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
return $output; } 

if (isset($_GET['value'])) {
$waterRescueRope = strip_bad_chars( $_GET['value'] );
$rope = $waterRescueRopes[$waterRescueRope];}

以下是 已编辑的 数组信息:

$waterRescueRopes = array(
"poly-soak" => array(
    'img' => "3P",
    'title' => "Poly Soak™",
    'blurb' => "Pelican’s 16 strand kernmantle construction is a...",
    'desc' => "Polypropylene Water Rescue Rope",
    'construction' => "16 Strand",
    'cover' => "Polypropylene",
    'core' => "Polypropylene",
        array(
            array(
                'sku'      => "3P-12Y",
                'diameter' => "3/8"",
                'tensile' => 3000,
                'weight' => 3.4,
                ),
            array(
                'sku'      => "3P-14Y",
                'diameter' => "7/16"",
                'tensile' => 3380,
                'weight' => 4.7,
                ),                  
        ),

        array("Water Rescue", "Canyoneering"),

        array("Lightweight", "Buoyant", "High Visibility"),         
),


"poly-soak-economy" => array(
    'img' => "FQK",
    'title' => "Poly Soak™ Economy",
    'blurb' => "All the great benefits and features of our Poly Soak rope but at a lower cost.",
    'desc' => "Polypropylene Economy Water Rescue Rope",
    'construction' => "16 Strand",
    'cover' => "Polypropylene",
    'core' => "Polypropylene",
        array(
            array(
                'sku'      => "FQK-502",
                'diameter' => "1/8"",
                'tensile' => 2000,
                'weight' => 1.5,
                ),
        ),

        array("Water Rescue"),

        array("Economical", "Lightweight", "Buoyant"),      
),

"poly-soak-2" => array(
    'img' => "3PN",
    'title' => "Poly Soak™",
    'blurb' => "This UL certified polypropylene rope is approved for...",
    'desc' => "NFPA® Water Rescue Rope",
    'construction' => "24 Strand",
    'cover' => "Polypropylene",
    'core' => "Polypropylene",
        array(
            array(
                'sku'      => "3PN-12Y",
                'diameter' => "3/8"",
                'tensile' => 4200,
                'weight' => 3.05,
                ),
        ),

        array("Water Rescue", "Canyoneering", "Mooring"),

        array("Bright Colors for Greater Visibility", "Supple for Compact Storage", "Certified to the Highest Standard", "Buoyant"),        
),

"aqua-d" => array(
    'img' => "3q",
    'title' => "Aqua-D™",
    'blurb' => "This water rescue rope has a blended Dyneema® and polypropylene core. With a multifilament polypropylene cover, this rope floats and has near zero water retention. It is extremely strong, light 'weight', and has a no-stretch braid that meets the needs of coastal and river rescue professionals worldwide.",
    'desc' => "Dyneema® Polypropylene Water Rescue Rope",
    'construction' => "16 Strand",
    'cover' => "Polypropylene",
    'core' => "Dyneema®",
        array(
            array(
                'sku'      => "3Q-08Y",
                'diameter' => "1/4"",
                'tensile' => "3200",
                'weight' => "1.7",
                ),
            array(
                'sku'      => "3Q-10Y",
                'diameter' => "5/16"",
                'tensile' => "3400",
                'weight' => "2.3",
                ),
            array(
                'sku'      => "3Q-12Y",
                'diameter' => "3/8"",
                'tensile' => "4950",
                'weight' => "3.4",
                ),
        ),

        array("Minimal Water Retention", "Strong & Lightweight", "Buoyant"),

        array("Water Rescue"),      
), );

其他附加功能:

 function get_specs($array = false)
    {
        if (!is_array($array))
            return array();
        elseif(empty($array))
            return array();

        foreach($array as $polytype => $polydata) {
                $new[$polytype]['specs']        =   (!empty($polydata[0]))? $polydata[0]:array();
                $new[$polytype]['applications'] =   (!empty($polydata[1]))? $polydata[1]:array();
                $new[$polytype]['benefits']     =   (!empty($polydata[2]))? $polydata[2]:array();
            }

        return $new;
    }

以下是 已编辑的 specTable:

<?php function display_specs($spec = array(),$name = false)
    {
        if(!empty($spec[$name]['specs'])) {
                ob_start();
                foreach($spec[$name]['specs'] as $specs) {
                ?> 
                <tr> 
                    <td><?php echo $specs['sku']; ?></td>
                    <td><?php echo $specs['diameter']; ?></td>
                    <td><?php echo $specs['tensile']; ?></td>
                    <td><?php echo $specs['weight']; ?></td>
                </tr>
                <?php
                    }

                $data   =   ob_get_contents();

            }
    } ?>

// Isolates the specs from the main array
$specs  =   get_specs($waterRescueRopes);

// Alternately, you can loop through the main array and get all 
<?php foreach($specs as $polytype => $spec) {
    echo display_specs($specs,$_GET['value']);
}
 ?>

已编辑 应用程序(优势在类似列表中):

<?php function display_applications($spec = array(),$name = false)
    {
        if(!empty($spec[$name]['applications'])) {
                ob_start();
                foreach($spec[$name]['applications'] as $apps) {
                ?> 
                <li><?php echo $apps; ?></li>
                <?php
                    }

                $data   =   ob_get_contents();
                return $data;
            } 
    }
?>
 <tr valign='top'>
     <td class="td-item-cell"><article>
         <div class="rel_product_wrapper">
             <h3>Applications</h3>
             <ul>
                 <li>
                     <?php
                     foreach($specs as $polytype => $spec) {
                     echo display_applications($specs,$_GET['value']);
                     }
                     ?>
                 </li>
             </ul>
         </div>
     </article></td>
 </tr>

我目前让他们填充我需要的东西但多次(四种类型的绳子,我假设四次)。

感谢您的时间!我很感激! :)

1 个答案:

答案 0 :(得分:0)

删除数组中的变量,然后创建一个函数来单独提取所需的那些方面。你正在做什么不起作用的原因是每次你在每个数组上写$specTable时,你重新分配/覆盖先前分配的$specTable到当前数组,直到最后它被分配到最后一个主阵列中的数组。这就是为什么只有一个出现的原因。您需要将这些方面保存到新阵列:

功能:将功能放在页面顶部或您在此页面上包含的其他文件中以供使用。最好包括使用类似include("function.specs.php")的内容,因为如果需要,您可以在不同页面上重复使用这些功能。

    function get_specs($array = false)
        {
            if (!is_array($array))
                return array();
            elseif(empty($array))
                return array();

            foreach($array as $polytype => $polydata) {
                    $new[$polytype]['specs']        =   (!empty($polydata[0]))? $polydata[0]:array();
                    $new[$polytype]['applications'] =   (!empty($polydata[1]))? $polydata[1]:array();
                    $new[$polytype]['benefits']     =   (!empty($polydata[2]))? $polydata[2]:array();
                }

            return $new;
        }

    function display_specs($spec = array(),$name = false)
        {
            if(!empty($spec[$name]['specs'])) {
                    ob_start();
                    foreach($spec[$name]['specs'] as $specs) {
                    ?> 
                    <tr> 
                        <td><?php echo $specs['sku']; ?></td>
                        <td><?php echo $specs['diameter']; ?></td>
                        <td><?php echo $specs['tensile']; ?></td>
                        <td><?php echo $specs['weight']; ?></td>
                    </tr>
                    <?php
                        }

                    $data   =   ob_get_contents();
                    return $data;
                }
        }

    // For sake of ease, you can do an application one by itself,
    // Normally, you could just do one class/method that handles all the
    // the different arrays
    function display_applications($spec = array(),$name = false)
        {
            if(!empty($spec[$name]['applications'])) {
                    ob_start();
                    foreach($spec[$name]['applications'] as $apps) {
                    ?> 
                    <tr> 
                        <td><?php echo $apps; ?></td>
                    </tr>
                    <?php
                        }

                    $data   =   ob_get_contents();
                    return $data;
                }
        }

使用页面上的功能

    // Isolates the specs from the main array
    $specs  =   get_specs($waterRescueRopes);
    // To select one product out of the bunch, just use the name from the array.
    // Note, you need the $specs array as the first argument
    echo display_specs($specs,"poly-soak");
    echo display_applications($specs,"poly-soak");
    // Alternately, you can loop through the main array and get all: ?>
<table>
<?php
foreach($specs as $polytype => $spec) {
        echo display_specs($specs,$polytype);
        echo display_applications($specs,$polytype);
    }
?>
</table>

应用程序部分:因为该函数已经为您完成了循环,所以您只需要回显它:

<tr valign='top'>
     <td class="td-item-cell"><article>
         <div class="rel_product_wrapper">
             <h3>Applications</h3>
             <ul>
                 <li>
                     <?php echo display_applications($specs,$_GET['value']); ?>
                 </li>
             </ul>
         </div>
     </article></td>
 </tr>

RAW print_r($ specs)为您提供:

Array
(
    [poly-soak] => Array
        (
            [specs] => Array
                (
                    [0] => Array
                        (
                            [sku] => 3P-12Y
                            [diameter] => 3/8&quot;
                            [tensile] => 3000
                            [weight] => 3.4
                        )

                    [1] => Array
                        (
                            [sku] => 3P-14Y
                            [diameter] => 7/16&quot;
                            [tensile] => 3380
                            [weight] => 4.7
                        )

                )

            [applications] => Array
                (
                    [0] => Water Rescue
                    [1] => Canyoneering
                )

            [benefits] => Array
                (
                    [0] => Lightweight
                    [1] => Buoyant
                    [2] => High Visibility
                )

        )

    [poly-soak-economy] => Array
        (
            [specs] => Array
                (
                    [0] => Array
                        (
                            [sku] => FQK-502
                            [diameter] => 1/8&quot;
                            [tensile] => 2000
                            [weight] => 1.5
                        )

                )

            [applications] => Array
                (
                    [0] => Water Rescue
                )

            [benefits] => Array
                (
                    [0] => Economical
                    [1] => Lightweight
                    [2] => Buoyant
                )

        )

    [poly-soak-2] => Array
        (
            [specs] => Array
                (
                    [0] => Array
                        (
                            [sku] => 3PN-12Y
                            [diameter] => 3/8&quot;
                            [tensile] => 4200
                            [weight] => 3.05
                        )

                )

            [applications] => Array
                (
                    [0] => Water Rescue
                    [1] => Canyoneering
                    [2] => Mooring
                )

            [benefits] => Array
                (
                    [0] => Bright Colors for Greater Visibility
                    [1] => Supple for Compact Storage
                    [2] => Certified to the Highest Standard
                    [3] => Buoyant
                )

        )

    [aqua-d] => Array
        (
            [specs] => Array
                (
                    [0] => Array
                        (
                            [sku] => 3Q-08Y
                            [diameter] => 1/4&quot;
                            [tensile] => 3200
                            [weight] => 1.7
                        )

                    [1] => Array
                        (
                            [sku] => 3Q-10Y
                            [diameter] => 5/16&quot;
                            [tensile] => 3400
                            [weight] => 2.3
                        )

                    [2] => Array
                        (
                            [sku] => 3Q-12Y
                            [diameter] => 3/8&quot;
                            [tensile] => 4950
                            [weight] => 3.4
                        )

                )

            [applications] => Array
                (
                    [0] => Minimal Water Retention
                    [1] => Strong &amp; Lightweight
                    [2] => Buoyant
                )

            [benefits] => Array
                (
                    [0] => Water Rescue
                )
        )
)