I want to add new property for each object. My code is shown below:
function getCitationDataofquotes($var1){
$query = "SELECT `id`,`title`,`source`,`content`,`page`,`city`,`publisher` FROM `#__gpo_citations_quotes`";
$query .= " WHERE `id`='" . $var1 . "'";
if (!empty($query))
{
$some= array();
$this->_db->setQuery($query);
$data = $this->_db->loadObject();
return $data;
}
$value = $datapages->getCitationDataofquotes($var1);
$getdataofcitation['citations'][] = $value;
Then my output of this return data is below
{id: "12091",
title: "Homicide Victims by Year and Jurisdiction, 1989–90 to 2010–12",
source: "Homicide in Australia: 2010–11 to 2011–12: National Homicide Monitoring Program report",
content: " ACT = Australian Capital Territory]",
page: "p. 37",
city: "Canberra",
publisher: "Australian Institute of Criminology"
},
{
id: "8901",
title: "test",
source: null,
content: "",
page: null,
city: null,
publisher: null
},
Now I want to add new property for each object. I want this output for each object.
{ id: "8901",
title: "test",
source: null,
content: "",
page: null,
city: null,
publisher: null ,
**new property: something**}
First of all, your $query is vulnerable to Sql-injection. BTW there are multiple ways to achieve the results you want. e.g you can add properties in the query instead:
SELECT 'some value' As `some_field` ...
If you want to add it in the php code, you can add it dynamically as @madalinivascu says above:
If it is an object:
$myObject->myNewProperty = 'My new value';
# if it is dynamically and you don't know the name ...
$myObject->{"myNewProperty"} = 'My new value';
If it is an array:
$myArray['myNewProperty'] = 'My new value';
Update this code when value return with multiple object with array
$value[] = $datapages->getCitationDataofquotes($var1);
for($i=0; $i<count($value); $i++)
{
$value[$i]->newprop = 'new content';
}