【问题标题】:Get specific values from multidimentional array locked to ID从锁定到 ID 的多维数组中获取特定值
【发布时间】:2011-10-19 02:46:28
【问题描述】:

也许标题有点混乱,但让我试着解释一下。

我有一个数据库(informix),并使用 PHP PDO,在该数据库中有一个包含页面样式表的表。 像这样看:

特殊 ID,完整的样式表(就像我们在 CSS 文件中制作的一样)。

目前我可以在多维数组中显示记录(见下面的代码和输出):

$query = $db -> prepare("select alpb_box_id, alpb_style::lvarchar as STYLESHEET  
from ao_link_page_boxes WHERE  alpb_li_id = 633 AND alpb_lngcode = '031' AND 
alpb_pageno = 1"); 

$query -> execute();

$result = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)) {

$result[] = $row;
}
$db = null;

echo "<pre>";
print_r($result);
echo "</pre>";

还有输出:

Array
(
[0] => Array
(
[ALPB_BOX_ID] => TXT-633-P1B1
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Swis721 BT; font-size:69px; font-style:normal; font-variant:normal; font-weight:normal; height:75px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:793px; word-spacing:0px; z-index:auto;
    )

[1] => Array
    (
        [ALPB_BOX_ID] => TXT-633-P1B2
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Swis721 BT\'; font-size:16px; font-style:italic; font-variant:normal; font-weight:normal; height:197px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:absolute; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:75px; vertical-align:baseline; white-space:normal; width:717px; word-spacing:0px; z-index:auto;
    )

[2] => Array
    (
        [ALPB_BOX_ID] => IMG-633-P1B3
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Times New Roman\'; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:207px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
    )

[3] => Array
    (
        [ALPB_BOX_ID] => IMG-633-P1B4
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Times New Roman; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
    )

)

现在我尝试做的是,从每个 ID(如:TXT-633-P1B1)中获取以下样式:

高度:75px;,宽度:793px;,顶部:0px;并离开:0px;。

并将它们存储到结果中,以便我可以在需要时重复使用和回显它们。

在这种情况下,我应该得到 4 个 ID 和 4 个样式项(包括数量 px)。

有没有人知道如何做到这一点。 我希望我正确地解释了我的问题。

谢谢


谢谢,很有帮助,我确实有一个问题。

如何将这些值添加到预制的 div 中。 例如:

<div id="TXT-633-P1B1" style="height:75px; width:793px; top:0px; left:0px;"></div>

这适用于该数组中的每个 ID。

非常感谢。

【问题讨论】:

    标签: php css database multidimensional-array pdo


    【解决方案1】:

    试试这个:

    $new_styles = array();
    $styles_to_collect = array('height', 'width', 'top', 'left');
    foreach($result as $r)
    {
            $new_entry = array();
            foreach($styles_to_collect as $stc)
            {
                    preg_match('/[;\s]\s*'.$stc.':\s*([\d]+(px)?)([;\s]|$)/', $r['STYLESHEET'], $matches);
                    $new_entry[$stc] = $matches[1];
            }
            $new_styles[$r['ALPB_BOX_ID']] = $new_entry;
    }
    

    产生($new_styles):

    Array
    (
        [TXT-633-P1B1] => Array
            (
                [height] => 75px
                [width] => 793px
                [top] => 0px
                [left] => 0px
            )
    
        [TXT-633-P1B2] => Array
            (
                [height] => 197px
                [width] => 717px
                [top] => 75px
                [left] => 0px
            )
    
        [IMG-633-P1B3] => Array
            (
                [height] => 188px
                [width] => 377px
                [top] => 207px
                [left] => 0px
            )
    
        [IMG-633-P1B4] => Array
            (
                [height] => 188px
                [width] => 377px
                [top] => 0px
                [left] => 0px
            )
    
    )
    

    编辑:根据 OP 的要求,我正在为我的答案添加更多内容。 要在内容中使用这些样式,您可以执行以下操作:

    $content = '';
    foreach($new_styles as $id=>$ns)
    {
            $inline_styles = '';
            foreach($ns as $name=>$value) $inline_styles .= $name.': '.$value.'; ';
            $inline_styles = trim($inline_styles);
            $content .= '<div id="'.$id.'" style="'.$inline_styles."\"></div>\n";
    }
    

    这会将 $content 设置为:

    <div id="TXT-633-P1B1" style="height: 75px; width: 793px; top: 0px; left: 0px;"></div>
    <div id="TXT-633-P1B2" style="height: 197px; width: 717px; top: 75px; left: 0px;"></div>
    <div id="IMG-633-P1B3" style="height: 188px; width: 377px; top: 207px; left: 0px;"></div>
    <div id="IMG-633-P1B4" style="height: 188px; width: 377px; top: 0px; left: 0px;"></div>
    

    【讨论】:

    • 谢谢,这很有帮助,我确实有一个问题。如何将这些值添加到预制的 div 中。例如:&lt;div id="TXT-633-P1B1" style="height:75px; width:793px; top:0px; left:0px;"&gt;&lt;/div&gt; 这适用于该数组中的每个 ID。谢谢,我真的很感激。
    • @user999221 我在回答中添加了更多内容
    • 谢谢,我已经很接近了。我有最后一个问题。例如,当我想要这些值时,就像上面数组的顺序一样:value[1]value[2]value[3]..... 这与 div 样式无关,因为它们工作得很好,这是另外一回事。我希望你明白我的想法?再次感谢。
    • 只是为了澄清,这些值也可以在列表中使用(这就是为什么我在它们后面添加 []。
    • 我已经有了以下代码:function flatten($array) { if (!is_array($array)) { return array($array); } $result = array(); foreach ($array as $value) { $result = array_merge($result, flatten($value)); } return $result; } $arr = $new_styles; foreach (flatten($arr) as $key=&gt; $value) { echo "$key = $value &lt;br /&gt;"; } 这给了我 key = value,例如:0 = 75, 1 = 793。现在我想要的是使用 key 和value: $value[$key],但这给了我唯一的结果数组。
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2016-09-27
    • 2013-06-14
    相关资源
    最近更新 更多