简体   繁体   中英

How to echo specific key value from an object into an array value?

I am trying to create a multidimensional array using data from some other arrays and objects. This is my php code:

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

The output is:

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
                        )

                )

        )

)

I need to echo only value of [value] from the sddClass Objects for [urun_resmi_fieldx], I don't want to output [image_title] [image_alt] but I don't know how. When I try the code below I can get a specific row correctly but I don't know how many rows there will be.

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

This is the output I need:

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
                        )

                )

        )

)

My question is how I can loop this [value]s from sddClass Objects into array value. Thanks.

Here is my Suggestion:

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

            )

Since this urum_resmi_fieldx Object contains an Array of Image Data, you could as well loop through the Array and access the properties you desire, like so:

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

Finally in summary; here is what may be possible:

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

Problem:

I need to echo only value of [value] from the sddClass Objects for [urun_resmi_fieldx]...

Solution:

// 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 />";
        }
    }
}

Edited:

Based on your requirement, the solution would be like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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