繁体   English   中英

关联数组中的关联数组

[英]Associative Array within Associative Array

我是PHP新手,正在尝试在命名数组中应用命名数组。 我已经能够将每个绳索名称(即“ Poly Soak”)与img,标题,blurb,desc等一起应用,但是尝试将$specTable应用于表时遇到了困难。 我也试图应用$applications$benefits运气不好。

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

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

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>

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

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

删除数组中的变量,然后创建一个函数以分别提取您需要的那些方面。 之所以不起作用,是因为每次在每个数组上写入$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
                )
        )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM