【问题标题】:How to echo specific key value from an object into an array value?如何将对象中的特定键值回显到数组值中?
【发布时间】:2016-08-26 04:46:51
【问题描述】:

我正在尝试使用来自其他一些数组和对象的数据创建一个多维数组。这是我的 php 代码:

<?php

    $items = $cck->getItems();

    $out = array();
    foreach( $items as $item ) {
        $out[] = array( 
                        "art_id" => $item->getValue('art_id'),
                        "art_title" => $item->getValue('art_title'),
                        "urun_resmi_fieldx" => $item->getValue('urun_resmi_fieldx')

                        );
     } 
     ?>
    <pre><?php print_r($out); ?></pre>

输出是:

Array
(
    [0] => Array
        (
            [art_id] => 308
            [art_title] => 3D Katı Modelleme
            [urun_resmi_fieldx] => Array
                (
                )

        )

    [1] => Array
        (
            [art_id] => 311
            [art_title] => Dişli paslanmaz çelik kollektörler
            [urun_resmi_fieldx] => Array
                (
                    [0] => stdClass Object
                        (
                            [value] => images/311/urun.jpg
                            [image_title] => urun.jpg
                            [image_alt] => urun.jpg
                        )

                    [1] => stdClass Object
                        (
                            [value] => images/311/slide-1.jpg
                            [image_title] => slide-1
                            [image_alt] => slide-1
                        )

                )

        )

)

我只需要从 [urun_resmi_fieldx] 的 sddClass 对象中回显 [value] 的值,我不想输出 [image_title] [image_alt] 但我不知道如何输出。当我尝试下面的代码时,我可以正确获取特定的行,但我不知道会有多少行。

"urun_resmi_fieldx" => $item->getValue('urun_resmi_fieldx')["0"]->value

这是我需要的输出:

Array
(
    [0] => Array
        (
            [art_id] => 308
            [art_title] => 3D Katı Modelleme
            [urun_resmi_fieldx] => Array
                (
                )

        )

    [1] => Array
        (
            [art_id] => 311
            [art_title] => Dişli paslanmaz çelik kollektörler
            [urun_resmi_fieldx] => Array
                (
                    [0] => stdClass Object
                        (
                            [value] => images/311/urun.jpg
                        )

                    [1] => stdClass Object
                        (
                            [value] => images/311/slide-1.jpg
                        )

                )

        )

)

我的问题是如何将这个 [value] 从 sddClass 对象循环到数组值中。 谢谢。

【问题讨论】:

    标签: php arrays loops object


    【解决方案1】:

    这是我的建议:

    <?php
    
        $items      = $cck->getItems();
        $out        = array();
        $uRun       = array();
        $counter    = 0;
    
        foreach( $items as $item ) {
            $uRun[] = $item->getValue('urun_resmi_fieldx');
            $out[]  = array(
              "art_id"              => $item->getValue('art_id'),
              "art_title"           => $item->getValue('art_title'),
              "urun_resmi_fieldx"   => $uRun[$counter]
    
            );
            $counter++;
        }
        //
    
         ?>
        <pre><?php print_r($uRun); //PRINTS THE "URUN_RESMI_FIELDX" OBJECT....?></pre>
        <pre><?php //print_r($out); ?></pre>
    
        <!-- PRINTS A NUMERICALLY INDEXED ARRAY OF OBJECTS REPRESENTING THE URUN_RESMI_FIELDX OBJECT -->
        Array
                (
                    [0] => stdClass Object
                        (
                            [value] => images/311/urun.jpg
                            [image_title] => urun.jpg
                            [image_alt] => urun.jpg
                        )
    
                    [1] => stdClass Object
                        (
                            [value] => images/311/slide-1.jpg
                            [image_title] => slide-1
                            [image_alt] => slide-1
                        )
    
                )
    

    由于这个 urum_resmi_fieldx 对象包含一个图像数据数组,您也可以循环访问该数组并访问您想要的属性,如下所示:

        <?php
            $htmlPhotos = "<div class='image-box'>";
            foreach($uRun as $intKey=>$objPix){
                $htmlPhotos .= "<img alt= '" . $objPix->image_alt . "' title='" . $objPix->image_title . "' src='" . $objPix->value .  "' class='thumbnail' />";
            }
            $htmlPhotos .= "</div>";
            //DISPLAY ALL IMAGES ASSOCIATED WITH THIS ARTICLE/ITEM...
            echo $htmlPhotos;
    

    最后总结一下;这是可能的:

    <?php
    
        //AND HERE IS HOW YOUR DISPLAY MIGHT TYPICALLY LOOK LIKE:
    
    
        $items      = $cck->getItems();
        $out        = array();
        $uRun       = array();
        $counter    = 0;
        $output     = "<table cellpadding='0' cellspacing='0' class='pdt-table' />";
        $output    .= "<thead class='pdt-table-head'>";
        $output    .= "<tr>";
        $output    .= "<th class='pdt-head-cel'><p class='emphasis'>Article Title</p></th>";
        $output    .= "<th class='pdt-head-cel'><p class='emphasis'>Article ID</p></th>";
        $output    .= "<th class='pdt-head-cel'><p class='emphasis'>Article Images</p></th>";
        $output    .= "</tr>";
        $output    .= "</thead>";
        $output    .= "<tbody>";
    
        foreach( $items as $item ) {
            $uRun[]     = $item->getValue('urun_resmi_fieldx');
            $out[]      = array(
              "art_id"              => $item->getValue('art_id'),
              "art_title"           => $item->getValue('art_title'),
              "urun_resmi_fieldx"   => $uRun[$counter]
    
            );
            $output    .= "<tr class='pdt-row'>";
            $output    .= "<td class='pdt-cell'>" . $item->getValue("art_title")    . "</td>";
            $output    .= "<td class='pdt-cell'>" . $item->getValue("art_id")       . "</td>";
            $output    .= "<td class='pdt-cell'>";
    
            $htmlPhotos = "<div class='image-box'>";
            foreach($uRun[$counter] as $intKey=>$objPix){
                $htmlPhotos .= "<img alt= '" . $objPix->image_alt . "' title='" . $objPix->image_title . "' src='" . $objPix->value .  "' class='thumbnail' />";
            }
            $htmlPhotos .= "</div>";
            $output     .= $htmlPhotos;
            $output     .= "</td>";
            $output     .= "</tr>";
            $counter++;
        }
        $output    .= "</tbody>";   
        $output    .= "</table>";   
    
    
        //DISPLAY ALL ARTICLES/ITEMS IN A TABLE (INCL. ASSOCIATED ITEM-IMAGES)
        echo $output;
    

    【讨论】:

    • "art_id" 和 "urun_resmi_fieldx" 不在同一级别,因此在同一个 foreach 中循环它们不会达到我想要的效果。顺便说一句,我不需要 html,我的输出将是 json。
    【解决方案2】:

    问题:

    我只需要从 [urun_resmi_fieldx] 的 sddClass 对象中回显 [value] 的值...

    解决方案:

    // Suppose $out is the original array
    foreach($out as $value){
        if(count($value['urun_resmi_fieldx'])){
            foreach($value['urun_resmi_fieldx'] as $v){
                echo $v->value . "<br />";
            }
        }
    }
    

    已编辑:

    根据您的要求,解决方案如下:

    foreach($out as $key => $value){
        if(count($value['urun_resmi_fieldx'])){
            foreach($value['urun_resmi_fieldx'] as $k => $v){
                $out[$key]['urun_resmi_fieldx'][$k] = (object)array('value' => $v->value);
            }
        }
    }
    
    // display $out array
    var_dump($out);
    

    【讨论】:

    • foreach 不能用作数组值。 $items 是源数组,$out 是输出数组。
    • @MelihTaş foreach 不能用作数组值,你的意思是什么?您是否使用此代码 sn-p 运行您的应用程序?
    • 是的,我试过但 $items 是对象,给了我这个错误。致命错误:不能在第 46 行的 C:\wamp\www\meka\templates\seb_json\positions\json\list\json.php 中使用 CCK_Item 类型的对象作为数组
    • @MelihTaş 这就是问题所在,您将$items 传递给foreach 循环。将数组$out 传递给循环,我在回答中也说过这件事。
    • @MelihTaş 我已经更新了我的答案。请参阅我回答的 edited 部分。希望这能解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 2018-08-04
    • 2015-09-16
    • 2019-09-25
    • 1970-01-01
    • 2020-06-14
    • 2022-08-03
    • 2016-12-06
    相关资源
    最近更新 更多