【问题标题】:Objects Array Get Specific Object and display together PHPObjects Array 获取特定对象并一起显示 PHP
【发布时间】:2018-11-10 05:35:32
【问题描述】:

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

我在我的数据库中保存了一个包含对象的 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 )     
)

以下是案例:

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

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

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

我在代码中所做的如下

   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. 这就是我得到的

但我只需要创建一次住宿或所有相同的Title,并且所有Info 在该div 下具有Title == 'Accommodation'

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

总而言之,我需要在一个 div 中显示所有具有相同 [Title][Info],并为每个唯一的“[Title]”创建不同的 div

提前致谢

【问题讨论】:

  • &lt;h4&gt;&lt;?=$value-&gt;Title?&gt;&lt;/h4&gt; 只需删除速记 echo php 标签,您已经在 php 空间中,只需连接变量
  • 是的,我知道,编辑错误。谢谢
  • 不清楚,您输出所有Title 具有所需值的数据。你还需要什么?
  • 不应该是$value-&gt;Title == 'Accommodation',在你的代码中$d是未定义的
  • 尝试检查它是否进入内部。

标签: php json foreach


【解决方案1】:

要求:在连续的记录流中显示“嵌套组”。 在这里,我们有一个嵌套组,由一个名为“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', ),     
    );    

【讨论】:

  • 谢谢@Ryan Vincent。尤其是大解释,非常感谢
【解决方案2】:

您好,请这样做以获得每个结果的唯一 div。对于第 2 个问题(如果 $obj->Title == 'foo' 或以某种方式在 foreach 循环之前检查数组有哪些数据并将它们分开,php 中是否有任何内置函数来获取所有 $obj->Info ?) 我会晚点找到解决办法的:)

$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>";

【讨论】:

  • 我需要为每个相同的标题在一个 div 中显示所有信息。也许在另一条记录中,有一个或没有住宿标题和多个位置或任何标题。
  • 可能是这样。从 foreach 外部回显打开和关闭潜水
  • 没有 AramGrig。您的代码仅显示多个中的第一个。我想在一个 div 中显示所有具有相同 $value->TItle 的 $value->Info。对不起,如果我的问题让你理解了别的东西:D
  • 是的,我不明白。抱歉耽误您的时间
  • 感谢您的宝贵时间,非常感谢。祝你有美好的一天
猜你喜欢
  • 2021-08-09
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多