Question: Objectives:
I am trying to read certain parts of **any** json file by letting the user input the desired properties.

Say I have an object that looks like this:

```
"adverts": [
    {
    "id": "33655628",
    "companyInfo": {
            "companyName": "Company A",
            "homepage": "http://companya.com",
            "companyText": null
    },
    ...
]

```

I want to access the properties by assigning the property name or "path" by a variable.
Accessing the first level ($item->$\_id) works fine but how do I access a nested property *companyName* directly with by a variable such as

```
 $_name = "companyInfo->companyName";

```

Ex:

```
$_repeat = "adverts";
$_id = "id";
$_name = ??????

foreach($data->$_repeat as $item){
    var_dump($item->$_id);
    var_dump($item->$_name);
}

```

EDIT:
As clarification: I want this to be universal for **any** JSON object!

PRELIMINARY SOLUTION:
I got the desired results by looping as suggested by @Carlos:

```
$_title_a = explode(".",$_title);
$current = $document;
foreach($_title_a as $a){
    $current = $current->$a;
}
var_dump($current);

```

If someone has a better suggestion, I would be glad to hear it. Thanks everybody!

Answer: 
If you only have two events, you can do this with `min()`, `max()`, and some additional logic:

```
select ids, date, min(event) as event,
       (case when min(event) <> max(event) then max(event) end) as event2
from table t
group by ids, date;

```

This is standard SQL so it should work in any database.
try this first select ids and then fetch all column based on above id ....

```
<?php
    $query = $database->getRows("SELECT DISTINCT ids FROM table");  
    ?>

    <table>
    <?php foreach($query as $row){ ?>
            <tr>

                <td><?php echo $row['ids']; ?></td>

            <?php
            $id= $row["ids"];
            $sub_inner = $database->getRows("SELECT date,Event,Event2 FROM table where ids= :ids",
            array(':ids'=>$id));                        
            ?>

    <td><?php foreach($sub_inner as $list){ ?><?php echo $list['date']; ?></td><td><?php echo $list['Event']; ?><?php } ?></td> 
    <td><?php echo $list['Event2']; ?></td> 

    <?php } ?>

```