I have a problem with modifying an array.
foreach ($page->getResults() as $lineItem) {
print_r($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations());
}
This code gives a result:
Array
(
[0] => Google\AdsApi\Dfp\v201611\Location Object
(
[id:protected] => 2250
[type:protected] => COUNTRY
[canonicalParentId:protected] =>
[displayName:protected] => France
)
)
I'm trying to add another, [1] , same type of object to this array.
I made a class to create and add an object:
class Location{
public function createProperty($propertyName, $propertyValue){
$this->{$propertyName} = $propertyValue;
}
}
$location = new Location();
$location->createProperty('id', '2792');
$location->createProperty('type', 'COUNTRY');
$location->createProperty('canonicalParentId', '');
$location->createProperty('displayName', 'Turkey');
array_push($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations(), $location);
Then, if I pass this into print_r() function
print_r($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations());
It shows the same result.
In the end, I need to send this updated whole $lineItem to this function
$lineItems = $lineItemService->updateLineItems(array($lineItem));
But seems like before sending I can't properly add an object to the array.
Thanks in advance.
PHP returns arrays as a value instead of as a reference. This means you must set the modified value back somehow.
Looking at the library apparently in question, there seems to be setExcludedLocations method for that purpose.
So your code should be something like:
$geo_targeting = $lineItem->getTargeting()->getGeoTargeting();
$excluded_locations = $geo_targeting->getExcludedLocations();
array_push($excluded_locations, $location);
$geo_targeting->setExcludedLocations($excluded_locations);
Related
I don't know how to work with such object, i need to get first and second one status value, tryed to convert it to json, but it gives me nothing. I just don't get it how to open array with such "_data:MailWizzApi_Params:private" name.
Source:
// SEARCH BY EMAIL
$response = $endpoint->emailSearch($myConfig["LIST-UNIQUE-ID"], $_GET["email"]);
// DISPLAY RESPONSE
echo '<hr /><pre>';
print_r($response->body);
echo '</pre>';
I receive such answer
MailWizzApi_Params Object
(
[_data:MailWizzApi_Params:private] => Array
(
[status] => success
[data] => Array
(
[subscriber_uid] => an837jdexga45
[status] => unsubscribed
)
)
[_readOnly:MailWizzApi_Params:private] =>
)
In this case, you can't.
Because it's private field.
For public fields with "incorrect" names you can use snippet:
$name = '}|{';
$obj->$name;
So, let see to your property: [_data:MailWizzApi_Params:private].
It is private field of instance of MailWizzApi_Params class with _data name.
Let's google to it's implementation: Found
As you can see it has toArray public method. Just use it.
print_r($response->body->toArray());
It has ArrayAccess implemented also. So, $response->body['status'] or $response->body['data'] will works.
Thank you guys for fast answers, here is my dumb way if reading status value
(thanks, #Jose Manuel Abarca Rodríguez)
$toJson = json_encode((array)$response->body);
$toJson = str_replace(array("\u0000MailWizzApi_Params\u0000_"), "", $toJson);
So we receive a normal json:
{"data":{"status":"success","data":{"subscriber_uid":"an837jdexga45","status":"unsubscribed"}},"readOnly":false}
And now we need just to decode it
$json = json_decode($toJson, true);
echo $json['data']['status'];
I have an array in yii2, and ocassionally it's only 1 single object that is not empty (all other element of array is empty) and I don't know which one is it. How can I either find the one that is not empty, or (my idea what I was trying), to create a new array, with array_filter (but I'm not sure if it works also with array of objects), to have only the one object in it.
if (count($ttepk) == 1) {
$ttep_filtered[] = array_filter($ttepk);
$id = $ttep_filtered[0]->id;
}
But it was also not working. I get the error message: PHP Notice – yii\base\ErrorException Trying to get property of non-object.
Before array_filter it looks like this:
Array
(
[3] => app\models\Model Object
(
after array_filter:
Array
(
[0] => Array
(
[3] => app\models\Model Object
(
So it seems, array_filter is not the one I need, or I use it the wrong way.
Can you please help me? Thank you!
You can try something like this
$filtered = array_filter($ttepk, function($item) {
return $item instanceof app\models\Model;
});
if (count($filtered) == 1) {
$id = reset($filtered)->id;
}
Im having a hard time to read some data that i get from joomla 2.5. First i have created a module that stores data on DB as a json. So first i read from DB linke:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = '. $db->quote('mod_products'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
and the result that i get as an array that contain objects, and each object has json data.
below is the arrray that i get from the query:
Array
(
[0] => stdClass Object
(
[params] => {
"product_name":"Sangiovese",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"1"
}
)
[1] => stdClass Object
(
[params] => {
"product_name":"Syrah",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"0",
}
)
[2] => stdClass Object
(
[params] => {
"product_name":"Merlot",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"0"
}
)
[3] => stdClass Object
(
[params] => {
"product_name":"Vermentino",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"White",
"isvisible":"0"
}
)
);
So what i want to do is to access the data within each param for examle:
PS: Array name is $results.
,
EX: i want to access product_name of each of the products that are on this array, or subtitle and so on.
so i did something like this, but its not working, i know i am not doing it right, but i hope someone can help me, and i would really appruciate it.
foreach( $results as $result )
{
echo $result->prams->product_name;
}
Error that shows when this code gets executed:
Notice: Trying to get property of non-object in
I really would need some advice on this.
Thank you!
Every item in your list is an object:
[0] => stdClass Object
[1] => stdClass Object
And every object has a params property which is a string containing JSON data.
You need to use json_decode built-in function to convert JSON string to an object or array.
Try this approach:
$paramsDecoded = json_decode($result->params, true);
print $paramsDecoded['product_name'];
Hello and thanks to all who helped,
I managed to make it functional.
So im gonna post here all the code for everyone else that passes on the same waters and needs help.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = '. $db->quote('mod_products') .' AND '. $db->quoteName('language') . ' <> '. $db->quote('en-GB'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
$count = count($results);
What i did to make the functionality i needed:
for ($i=0; $i < $count; $i++) {
$json = $results[$i]->params;
$product = json_decode($json);
// code here, example
echo $product->product_subtitle;
}
So, yes. I needed to decode using json_decode first, before using it on other parts of the code.
Thanks for helping. Hope this posts helps other developers who same as me, will have difficulties working with the way Joomla manipulates objects stored in database.
I'm attempting to create an array of objects in php and was curious how I would go about that. Any help would be great, thanks!
Here is the class that will be contained in the array
<?php
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $hotelinfo;
}
public function getprice(){
return $price;
}
}
And here is what I am attempting to do-
<?PHP
include 'file.php';
$hotelsdetail=array();
$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');
?>
The class attempting to create the array doesn't compile but is just a best guess as to how I can do this. Thanks again
What you should probably do is:
$hotelsDetail = array();
$details = new HotelDetails();
$details->setHotelInfo($rs);
$details->setPrice('150');
// assign it to the array here; you don't need the [0] index then
$hotelsDetail[] = $details;
In your specific case, the issue is that you should use ->, not .. The period isn't used in PHP to access attributes or methods of a class:
$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
Note that I capitalized the class, object, and function names properly. Writing everything in lowercase is not considered good style.
As a side note, why is your price a string? It should be a number, really, if you ever want to do proper calculations with it.
You should append to your array, not assign to index zero.
$hotelsdetail = array();
$hotelsdetail[] = new hoteldetails();
This will append the object to the end of the array.
$hotelsdetail = array();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
This would create an array with three objects, appending each one successively.
Additionally, to correctly access an objects properties, you should use the -> operator.
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
You can get the array of object by encoding it into json and decoding it with $assoc flag as FALSE in json_decode() function.
See the following example:
$attachment_ids = array();
$attachment_ids[0]['attach_id'] = 'test';
$attachment_ids[1]['attach_id'] = 'test1';
$attachment_ids[2]['attach_id'] = 'test2';
$attachment_ids = json_encode($attachment_ids);
$attachment_ids = json_decode($attachment_ids, FALSE);
print_r($attachment_ids);
It would render an array of objects.
output:
Array
(
[0] => stdClass Object
(
[attach_id] => test
)
[1] => stdClass Object
(
[attach_id] => test1
)
[2] => stdClass Object
(
[attach_id] => test2
)
)
So I do a print for my object: print_r ($objMailer);
And I get the following below:
mymailer Object
(
[_strRecipient:mymailer:private] =>
[_strBcc:mymailer:private] =>
[_strSubject:mymailer:private] =>
[_strEmail:mymailer:private] =>
[_arrData:mymailer:private] => Array
(
[full_name] => brian
[invitee_name] => test
[email] => test#testing.com
[captcha] => kqd2q9
)
[_arrAttachments:mymailer:private] =>
[_blnCaptcha:mymailer:private] => 1
[_arrErrors:mymailer:private] => Array
(
)
)
I need to echo/print out just the 'full_name' field? How can I go about doing this?
You can not trivially. As the print_r output shows that is within a private member.
You can either provide it from within your (?) mymailer object:
return $this->_arrData['full_name'];
or by using Reflection to make it accessible from the outside:
$refObj = new ReflectionObject($objMailer);
$refProp = $refObj->getProperty('_arrData');
$array = $refProp->getValue($objMailer);
echo $array['full_name'];
If you want to echo the value inside a method of mymailer class, you may use:
echo $this->_arrData['full_name'];
Since it's private you'll need to use a getter
The object you are referencing has an _arrData member variable which has private scope resolution, which means you cannot access it from outside the class. Chances are there is a public accessor which will allow you get the information you are after, but no way to tell unless you introspect the object itself.
I would suggest doing something like:
foreach (get_class_methods($mymailer) as $method) { echo 'M: ' . $method . '<br>'; } exit;
Then you can see the methods available to you, chances are there is a getData() method, with which you can do this:
$mailerData = $mymailer->getData();
var_dump($mailerData['full_name']);
There may even be a method to get the full name, something like this:
var_dump($mymailer->getFullname());