I have a array which I am printing using print_r.
<?php
print_r ($this)
?>
I get following result in my browser.
PackingListForm Object
(
[objShipment:protected] => Shipment Object
(
[objCustomFieldArray] =>
[intShipmentId:protected] => 38
[strShipmentNumber:protected] => 1035
[intTransactionId:protected] => 97
[intFromCompanyId:protected] => 1
[intFromContactId:protected] => 1
[intFromAddressId:protected] => 1
[intToCompanyId:protected] => 2
[intToContactId:protected] => 3
[intToAddressId:protected] => 2
[intCourierId:protected] => 1
[strTrackingNumber:protected] =>
[dttShipDate:protected] => QDateTime Object
)
)
Now I want to print / echo intTransactionId.
I have used following variable to echo the result, but I am getting undefined variable.
<?php
$noted = $this->objShipment->intTransactionId;
print_r ($noted);
?>
I am getting following php exception error in my browser.
Undefined GET property or variable in 'Shipment' class: intTransactionId
Line 33: $noted = $this->objShipment->intTransactionId;
My question is how can I echo / print value of intTransactionId?
intTransactionId is a protected property which means that you can't access it outside of the class itself (or parennt class or child class).
The exception, I think, is thrown in a __get magic method defined in Shipment (or one of its parent classes). This method is called when trying to access an unset property (or non-accesible property).
Please check this behaviour.
First try to convert it into array and then it will be much easier :),
$newArray = (array)$this;
print_r($newArray);//to see what can you get from there
// get_object_vars
$newArray = get_object_vars($object);
The object is from class PackingListForm, have look on that class if you have access and see if there is any get() function.
Related
I have an empty array in a remote file but intend to momentarily add and alter objects in it. However, after adding an initial first set of objects, the array does not accept any more values. My error log reports unexpected 'Object' (T_STRING), expecting ')' meaning it regards the keyword "Object" as a string imputed by me so I guess the problem originates from my array structure. Here is the code I used in adding the objects
include 'all_users.php';
$francis_udeh = new Admin("francis_udeh");
$all_users['francis_udeh'] = $francis_udeh;
$victor_nwafor = new Member("victor_nwafor");
$all_users['victor_nwafor'] = $victor_nwafor;
$print_arr = print_r($all_users, TRUE);
$updated_arr = "<?php \n \$all_users = $print_arr; \n?>";
file_put_contents('all_users.php', $updated_arr);
returns the following in the remote file
<?php
$all_users = Array
(
[francis_udeh] => Admin Object
(
[name] => francis udeh
[pagename] => francis.udeh
[can_comment] => 1
[can_view_announcements] => 1
[profile_pic] => /blog/accounts/assets/user.png
[can_delete_comment] => 1
)
[victor_nwafor] => Member Object
(
[name] => victor nwafor
[pagename] => victor.nwafor
[can_comment] => 1
[can_view_announcements] => 1
[profile_pic] => /blog/accounts/assets/user.png
)
);
?>
(which, by the way, is what I want). However, when I try
include 'all_users.php';
$raheem_sadiq = new Member("raheem_sadiq");
$all_users['raheem_sadiq'] = $raheem_sadiq;
$print_arr = print_r($all_users, TRUE);
$updated_arr = "<?php \n \$all_users = $print_arr; \n?>";
file_put_contents('all_users.php', $updated_arr);
it returns the error I posted earlier resulting in the array not changing. What am I doing wrong?
You include all_users.php at the beginning of code, but after first file_put_contents() it's a not correct php code in this file.
As #Indra already mentioned: the output given by print_r() is human readable, but not valid php code. If you don't have the possibilty to pass the data via a data storage (like mysql), it might be a workaround to put it to the file with serialize(). Alternatively, you could also use json (as your objects seem to be data access objects of some same kind) and then instantiate the objects remotely.
Hope this helps,
Greetings
I have a PHP application with a function that was built to expect information from an API call. However, I'm trying to use this function by passing in information that mimics the API data.
I struggle a bit with arrays and this seems to be an object within an array.
I can access the array that the api provides, so when I use the following code ($triggers is the array the api call returns):
print("<pre>".print_r($triggers,true)."</pre>");
I get the following output:
Array
(
[0] => stdClass Object
(
[triggerid] => 18186
[status] => 0
[value] => 0
)
This is the beginning of the function:
function iterate_triggers($triggers){
$trigger_id_values = array();
foreach($triggers as $trigger) {
//Necessary to show human readable status messages.
$check_status = array(0=>"Up", 1=>"Down", 2=>"Degraded", 3=>"Maintenance");
array_push ($trigger_id_values, [$trigger->triggerid, $trigger->value]);
So if I wanted to pass this function a [triggerid] => 18186 and [value] => 1 how would i do that?
Currently I'm trying:
iterate_triggers(array(0 => array("triggerid" => 18186,"status" => 0,"value" => 1,)));
but this gives me a "Trying to get property of non-object" error. Please be kind to me, I've done my best to research and structure this on my own to no avail.
The easiest way is to cast the assoc array just to an object
In your case this would be
iterate_triggers(array(0 => (object)array("triggerid" => 18186,"status" => 0,"value" => 1,)));
You are currently creating and passing an array that contains an array, while your function expects an array of objects.
You should create your object beforehand, then construct your parameter array, and pass it to your function.
$obj = new \stdClass();
$obj->triggerid = 18186;
$obj->status = 0;
$obj->value = 1;
$arr = array($obj);
iterate_triggers($arr);
This comment on php.net, and the rest of that object documentation, may be useful to you.
By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: <?php $genericObject = new stdClass(); ?>
The error message you are seeing with your own code is caused by $trigger->triggerid inside the function, when $trigger is an array instead of an object as the function expects. Object properties are accessed using $someObject->propertyName notation, while array elements are accessed using $someArray['keyName']
I suddenly stuck here:
$source = (object) array(
'field_phone' => array(
'und' => array(
'0' => array(
'value' => '000-555-55-55',
),
),
),
);
dsm($source);
$source_field = "field_phone['und'][0]['value']";
dsm($source->{$source_field}); //This notation doesn't work
dsm($source->field_phone['und'][0]['value']); //This does
dsm() is Drupal developer function for debug printing variables, objects and arrays.
Why $source object doesn't understand $obj->{$variable} notation?
Notice: Undefined property: stdClass::$field_phone['und']['0']['value']
Because your object does not have a property that is named "field_phone['und'][0]['value']". It has a property that is named "field_phone" which is an array which has an index named "und" which is an array which has an index 0 and so on. But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. It just looks for the property of the given name on the given object, nothing more. It's not like copy and pasting source code in place of $var there.
Starting from a variable called $data, which is an associative array which includes an object, and whose printed value is this:
Array
(
[item] => stdClass Object
(
[id] => 1
[tipo] => 0
[idioma] => es
[nombre] => Artículo de prueba
[titulo] => Esto es un artículo de prueba
[alias] => articulo-de-prueba
[texto] => Lorem ipsum etc etc
[url] =>
[video] =>
[fecha_c] => 2012-11-27 10:50:37
[fecha_m] => 2012-11-27 17:00:00
[fecha_p] => 2012-11-28 00:00:00
[destacado] => 0
[status] => 1
)
[imagenes] => Array
(
)
)
I need to filter its value and assign it to another array, this way:
protected function load_form($data = '') {
$this->load->helper('form');
// If item data have been sent, pass it to the form view to edit it.
// Else display empty form for new item.
if (! empty($data)) {
// Data can be an associative array with an object and another array or just an object
if (array_key_exists('item', $data)) {
$this->_vars['item'] =& $data['item'];
}
else {
$this->_vars['item'] =& $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
$view = $this->load->view(ADMIN_FORMS_PATH . $this->_controller . '_form', $this->_vars, true);
/*DEBUG*/ echo $view; // just for debugging purposes
}
The first assignment generates these errors:
A PHP Error was encountered Severity: Notice Message: Undefined index:
item Filename: core/Admin_Controller.php Line Number: 205
A PHP Error was encountered Severity: Notice Message: Object of class
stdClass could not be converted to int Filename:
core/Admin_Controller.php Line Number: 205
It behaves like the item index doesn't exist, and it does. Also, it tries to convert the object to an integer.
Why does it happen and what should I do to fix it?
EDIT:
I was doing &= instead of =&. That's the reason of the errors.
Anyway, the problem persists and the code seems to stop.
EDIT2:
Trying to redefine the problem. It might be something related to CodeIgniter, so I've added the whole function, including CodeIgniter functions.
The load_form() method can be invoked from a request to create a new item, in which case $data is empty, or from a request to edit a given item (in $data). In the first case (creation), the debug line is executed, but not in the second case (edition).
This may be your issue. Near the end you have"$datos['imagenes'];" where it should be "$data['imagenes'];"
if (! empty($data)) {
// Data can be an object, or an array with object + array of images
if (array_key_exists('item', $data)) {
$this->_vars['item'] &= $data['item'];
}
else {
$this->_vars['item'] = $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
Problem solved. It was too simple.
It was inside the view. I was trying to echo:
$item['id']
instead of
$item->id
The item data is being retrieved as an object, not an array. It was causing the problem silently, without any warning, notice or error.
Anyway I appreciate your quick help.
Here's an example of an array that is returned by CakePHP's find() method:
Array
(
[Tutor] => Array
(
[id] => 2
[PersonaId] => 1
)
)
The official documentation shows how to fetch records, but does not show how to iterate through them or even read out a single value. I'm kind of lost at this point. I'm trying to fetch the [id] value within the array. Here's what I've tried:
// $tutor is the array.
print_r($tutor[0]->id);
Notice (8): Undefined offset: 0
[APP\Controller\PersonasController.php, line 43]
Notice (8): Trying to
get property of non-object [APP\Controller\PersonasController.php,
line 43]
I've also tried:
// $tutor is the array.
print_r($tutor->id);
Notice (8): Trying to get property of non-object [APP\Controller\PersonasController.php, line 44]
The -> way of accessing properties is used in objects. What you have shown us is an array. In that example, accessing the id would require
$tutor['Tutor']['id']
Official PHP documentation, "Accessing array elements with square bracket syntax":
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]); //"bar"
var_dump($array[42]); //24
var_dump($array["multi"]["dimensional"]["array"]); //"foo"
?>
The returned value is an array, not an object. This should work:
echo $tutor['Tutor']['id'];
Or:
foreach($tutor as $tut){
echo $tut['Tutor']['id'] . '<br />';
}