Objects Array获取特定对象并一起显示PHP

时间:2018-05-31 08:55:10

标签: php json foreach

我在这里遇到问题,需要一些帮助。

我在数据库中保存了一个带有对象的json_aray数组。

解码后print_r我得到了这个结果。

Array ( 
[0] => stdClass Object ( 
    [Title] => Image 
    [Info] => info
    [ImageURL] => url.jpg ) 
[1] => stdClass Object ( 
    [Title] => Image 
    [Info] => info
    [ImageURL] => url.jpg )     
[2] => stdClass Object ( 
    [Title] => Accommodation 
    [Info] => info
    [ImageURL] => image.jpg ) 
[3] => stdClass Object ( 
    [Title] => Accommodation 
    [Info] => info
    [ImageURL] => image.jpg ) 
[4] => stdClass Object ( 
    [Title] => Accommodation 
    [Info] => info
    [ImageURL] => image.jpg ) 
[5] => stdClass Object ( 
    [Title] => Image 
    [Info] => info
    [ImageURL] => image.jpg )     
[6] => stdClass Object ( 
    [Title] => Location 
    [Info] => info
    [ImageURL] => image.jpg )     
)

所以这是 CASE:

我正在尝试为每个不同的Object->Title创建一个div,例如

我尝试使用foreach循环检查,但结果是创建了5个不同的divs而不是一个。

如果$obj->Info或以某种方式在$obj->Title == 'foo'循环之前检查数组有哪些数据并将它们分开,那么php中是否有任何内置函数可以获取所有foreach

我在代码中所做的是

   while($row = sqlsrv_fetch_array($get_details)) {
        $a = $row['additionalInformation'];
        $b = json_decode($a);
        $c = $b->AdditonalInfo;
        foreach ($c as $d){ 
            echo '<div class="class">';
            if(($d->Title !== 'Image')){
                echo '<h2>'.$d->Title.'</h2>';
                echo '<p>'.$d->Info.'</p>';
            }
            echo "</div>";
        }
    }

更新

  1. 使用实际的代码更改我的代码

  2. 这就是我得到的

  3. enter image description here

    但是,我需要创建一次住宿或每次Title只有InfoTitle == 'Accommodation'在该div下的所有[Info]

    如果我的问题很清楚,请告诉我,以便我可以更新。

    总而言之,我需要在一个div中显示具有相同[Title]的所有SELECT * FROM positions WHERE place_id IN (SELECT ID FROM place WHERE Title='آرایشگاه'),并为每个唯一的&#39; [标题]&#39; <创建不同的div /强>

    提前致谢

2 个答案:

答案 0 :(得分:2)

要求:在连续的记录流中显示“嵌套组”。 这里我们有一个由名为'Title'的字段指示的嵌套组。

必须对输入记录进行排序,以便每个组中的所有记录在一起并按所需顺序排列。

现在,首先想到的是使用'foreach'循环。这不是最清晰的方法,因为您无法通过查看当前记录来检测组的结尾。此外,foreach循环在循环结束时读取下一条记录。所以,你最终会尝试找出你在小组中的位置,知道该怎么做。

我使用了一种名为“预读”的技术。我们的想法是在循环之前读取记录并在处理完当前记录后立即读取下一条记录。 “技巧”是您处理循环内组中的所有记录。

所以,逻辑是迭代:

  • 群组的流程开始 - 已经拥有群组的第一条记录 - 重要。
  • 处理属于该组的所有详细记录 - 在此循环内读取记录。
  • 处理组的结尾 - 当前记录是下一组的第一个
  • 为每个小组重复。

它使代码更容易理解。

我已将所有单独的操作拆分为功能,以便您轻松修改各个操作。

请注意,代码与数据结构相匹配。代码中总有一个位置可以为该特定数据项执行必需的操作。

Full working Source Code at eval.in

运行代码

outputAllGroups($src);
exit();

处理所有组

function outputAllGroups(&$inputList) 
{
    reset($inputList);  
    $currentDetails = current($inputList); // read the first record 

    while ($currentDetails !== false) { // not end of records

        // start the title group
        $currentTitle = $currentDetails->Title; 
        outputGroupHeader($currentDetails);

        // process all records in the group
        while (    $currentDetails !== false 
                && $currentDetails->Title === $currentTitle) { 

            outputDetails($currentDetails);

            $currentDetails = readNextDetails($inputList); // may end group
        }

        // end the title group 
        outputGroupFooter($currentTitle);
    }
}

各个行动的功能

function outputGroupHeader($details)
{
    echo '<div class="TitleGroup">'. "<!-- Start Group: {$details->Title} -->". PHP_EOL
       . '<div class="Title">'. $details->Title .'</div>' . PHP_EOL; 
}

function outputGroupFooter($title)
{
    echo '</div>'. "<!-- End Group: {$title}  -->". PHP_EOL;
}

function outputDetails($details)
{
    echo '<div class="details">'. PHP_EOL,  
            $details->Info . PHP_EOL,  
            $details->ImageURL .PHP_EOL,
         '</div>' . PHP_EOL;            
}

function readNextDetails(&$inputList)
{
    $allOk = next($inputList); // advance next
    return $allOk !== false ? current($inputList) : $allOk; // advance next
}

输出

<div class="TitleGroup"><!-- Start Group: Image -->
<div class="Title">Image</div>
<div class="details">
info1
url1.jpg
</div>
<div class="details">
info2
url2.jpg
</div>
</div><!-- End Group: Image  -->
<div class="TitleGroup"><!-- Start Group: Accommodation -->
<div class="Title">Accommodation</div>
<div class="details">
info3
image3.jpg
</div>
<div class="details">
info4
image4.jpg
</div>
<div class="details">
info5
image5.jpg
</div>
</div><!-- End Group: Accommodation  -->
<div class="TitleGroup"><!-- Start Group: Image -->
<div class="Title">Image</div>
<div class="details">
info6
image6.jpg
</div>
</div><!-- End Group: Image  -->
<div class="TitleGroup"><!-- Start Group: Location -->
<div class="Title">Location</div>
<div class="details">
info7
image7.jpg
</div>
</div><!-- End Group: Location  -->

数据

$src = Array ( 
    '0' => (object) array( 
        'Title' => 'Image', 
        'Info' => 'info1',
        'ImageURL' => 'url1.jpg', ), 
    '1' => (object) array( 
        'Title' => 'Image', 
        'Info' => 'info2',
        'ImageURL' => 'url2.jpg', ),     
    '2' => (object) array( 
        'Title' => 'Accommodation', 
        'Info' => 'info3',
        'ImageURL' => 'image3.jpg', ), 
    '3' => (object) array( 
        'Title' => 'Accommodation', 
        'Info' => 'info4',
        'ImageURL' => 'image4.jpg', ), 
    '4' => (object) array( 
        'Title' => 'Accommodation', 
        'Info' => 'info5',
        'ImageURL' => 'image5.jpg', ), 
    '5' => (object) array( 
        'Title' => 'Image', 
        'Info' => 'info6',
        'ImageURL' => 'image6.jpg', ),     
    '6' => (object) array( 
        'Title' => 'Location', 
        'Info' => 'info7',
        'ImageURL' => 'image7.jpg', ),     
    );    

答案 1 :(得分:0)

嗨,请这样做以获得每个结果独特的div。对于2个问题(是否有任何内置函数在PHP中获取所有$ obj-&gt;信息如果$ obj-&gt;标题==&#39; foo&#39;或以某种方式检查foreach循环之前什么数组有的数据并将它们分开?)我会找到一些解决方案:)

$a = $row['additionalInformation'];
$b = json_decode($a);
$c = $b->AddInfo;
$createDivArray = array();
echo '<div class="style1">'; 
foreach ($c as $value){
   if (in_array($value->Title, $createDivArray)) {
      continue;
   }
   $createDivArray[] = $value->Title; 

   echo "<h4>$value->Title</h4>";
   echo "<p>$value->Info</p>";
}
echo "</div>";