如何将对象中的特定键值回显到数组值?

时间:2016-05-01 09:37:27

标签: php arrays loops object

我正在尝试使用来自其他一些数组和对象的数据创建一个多维数组。这是我的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] s从sddClass对象循环到数组值。 感谢。

2 个答案:

答案 0 :(得分:0)

这是我的建议:

<?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;

答案 1 :(得分:0)

<强>问题:

  

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

<强>解决方案:

COL

<强>编辑:

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

// 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 />";
        }
    }
}
相关问题