PHP未定义的属性通知

The following array has repeating and non-repeating data.

$arr = array();     

//non repeating data
$data = new StdClass();
$data->name = $name;
$data->url = $url;
$arr[] = $data;

//repeating data
foreach($records as $rec) {
    $data = new StdClass();
    $data->text = $rec->text;
    $data->time = $rec->time;
    $arr[] = $data;
}

Now I want to retrieve the data of the array, so I'm trying this:

echo $arr[0]->name;
echo $arr[0]->url;

foreach ($arr as $rec) {
    echo $rec->text;
    echo $rec->time;
}

I get following notice:

Notice: Undefined property: stdClass::$text in xx.php on line 48

Line #48 is echo $rec->text;

How I can fix this notice instead of hiding it?

Your first object from the array has name and url and the rest have text and time.

You are getting Undefined property: stdClass::$text on the first object, because it does not have that property.

It's just because your static data object doesn't have text and time fields, you can fix this by this simple check:

foreach ($arr as $rec) {
    if(isset($rec->text))
        echo $rec->text;
    if(isset($rec->time))
        echo $rec->time;
}

Additionally to cornelb's answer you should do it like code below

$arr = array();     
foreach($records as $rec) {
    $data = new StdClass();
    $data->text = $rec->text;
    $data->time = $rec->time;
    $data->name = $name;
    $data->url = $url;
    $arr[] = $data;
}

or you can start printing values from first index of your array

for ($i=1; $i < count($rec); $i++) {
    echo $rec[$i]->text;
    echo $rec[$i]->time;
}