I have this XML:
<JobReference> <Type>STANDARD</Type> <Title>N° ANCIEN DOSSIER</Title> <Reference/>
And am running this PHP code:
$xmljobreference = simplexml_load_string ($GetJobResult->JobReferences->JobReference->Title );
$referenceanciendossier = $GetJobResult->JobReferences->xpath( "//JobReference[#Title ='N° ANCIEN DOSSIER']/Reference" );
But I am getting this error:
Fatal error:call to undefined method stdClass::xpath() on line 190
If I run var_dump($GetJobResult) I get:
public 'JobReferences' =>
object(stdClass)[149]
public 'JobReference' =>
array (size=7)
0 =>
object(stdClass)[150]
...
1 =>
object(stdClass)[151]
...
2 =>
object(stdClass)[152]
public 'JobSeq' => int 920179
Well, as your var_dump shows you, JobReferences is built up by a JobReference which itself is an array with 7 items.
So assuming that you want to run the xpath function on all the array items, you need to run a foreach loop. Something like this should work:
$array = $GetJobResult->JobReferences->JobReference;
$results = array();
foreach($array as $reference){
array_push($results, $reference->xpath( "//JobReference[#Title ='N° ANCIEN DOSSIER']/Reference" ));
}
var_dump($results);
Related
i am fetching data from two tables as below :
$form['form']=json_decode($this->db->get_where('forms',array(
'id' => $id
))->result()[0]->form_sections);
This gives me following result on printing :
Array
(
[form] => Array
(
[0] => Personal_Information
[1] => Education_
[2] => Professional_Life
)
)
Now i have details against each of these array indexes that i am trying to assign them as below
foreach ($form['form'] as $value){
$value->details=$this->db->get_where('formdata',array(
'form_section' => $value
))->result();
}
Which gives me following error
Message: Attempt to assign property 'details' of non-object
can someone please help me to sort out the issue , i have to assign the index and loop in it in my view
This should help understand and clear up the message:
// Custom data
$Person_Info = 404; // Could be any non-object
$array = array("form" => array( 0 => $Person_Info) );
foreach( $array['form'] as $value ){
$results = new stdClass();
$results->name = "User A";
$value->details = $results; // $results are you fetch request
}
// Output: Attempt to assign property 'details' of non-object in php<8.0
// Output: Uncaught Error: Attempt to assign property "details" on int php>=8.0
Try to print_r or var_dump your $value and see what you are getting. It's not an object for some of your array item.
Edit: If ever in doubt about the data. Use if (is_obejct($var)) php.net/is_object or similar checks based on what you need.
I'm passing an ACF std class object of values to a function as a value and when trying to read it in the function, it shows up as an object but when I try to access a property Notice: Trying to get property "type" of non-object
function createACFProductLicenses($acfData, $propertyString){
$newData = array();
if(isset($acfData->{$propertyString})){
$data = (array)$acfData->{$propertyString};
foreach ($data as $key => $value) {
print_r($value);
// prints out
// stdClass Object
// (
// [type] => standard
// [item_id] => 727
// )
print_r($value->type); // errors out -> Notice: Trying to get property 'type' of non-object
}
}
}
// example of how I'm calling it:
// $product['acf'] is an stdClass of acf properties
createACFProductLicenses($product['acf'], 'product_licenses');
The "foreach" set the data structure to $key=>$value or [type] is a $key and "standard" is a $value so you can't print $value->type because the object is actually 'type' => 'standard'.
Also, know that the "foreach" will run your code for each item on the object like for ana array, so you can't just print a specific value when you have different $key.
If you want to display all the items use this:
print_r($key.':'.$value) // this will print: type : standard
Or if you want to only print the [Type] item Try this out:
insted of this
if(isset($acfData->{$propertyString})){
$data = (array)$acfData->{$propertyString};
foreach ($data as $key => $value) {
print_r($value->type); // errors out -> Notice: Trying to get property 'type' of non-object
}
}
}
Try this
if(isset($acfData->{$propertyString})){
$data = (array)$acfData->{$propertyString};
print_r($data['type']);
}
}
I hope this will help, let me know if it didn't work.
So I'm trying to echo specific values from the array, but I get message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/drilldown.php
Line Number: 92
that many times.
Heres the result of print_r of the array:
Array ( [0] => Array ( [productCode] => S10_1949 [productName] => 1952 Alpine Renault 1300 [productLine] => Classic Cars [productScale] => 1:10 [productVendor] => Classic Metal Creations [productDescription] => Turnable front wheels; steering function; detailed interior; detailed engine; opening hood; opening trunk; opening doors; and detailed chassis. [quantityInStock] => 7305 [buyPrice] => 98.58 [MSRP] => 214.3 [image] => S10_1949.jpg ) )
View Code:
$follow = $p['0'];
foreach($follow as $item) {
echo $item->productCode;
}
Model Code:
public function getProduct($p)
{
$this->db->where('productCode', $p);
$resultset = $this->db->get('products');
return $resultset->result_array();
}
Controller Code:
function drillDown()
{
$productID=$this->uri->segment(3);
$data['p']=$this->littlemodel->getProduct($productID);
$this->load->view('drillDown',$data);
}
Can anybody please help?
I believe you just want one row from the database.
You should be using
return $resultset->row()
So in the model, try this:
public function getProduct($p)
{
$this->db->where('productCode', $p);
return $this->db->get('products')->row();
}
Keep the controller code the same.
In the view, use this:
echo $p->productCode;
This is just an optimized way of fetching your requirements. In case of any trouble, do let me know. Will be happy to help.
My situation is quite simple, but i'm still looking for a nice and short solution for it.
Here is my case:
I receive a soap response object which is my be different from a call to another.
Sometimes, these properties are objects themselves and may have properties we have to get. For this, an array is set for each type of call to select the data wanted and discard the rest.
By example, in a call we receive an object like this:
(I made a code easy to test by mocking the received object)
$objTest = new stdClass();
$objTest->Content1 = "";
$objTest->Content2 = new stdClass();
$objTest->Content2->prop1=1;
$objTest->Content2->prop2=2;
$objTest->Content2->prop3=3;
$objTest->Content3 = 3;
$objTest->Content4 = array('itm1'=>1, 'itm2'=>'two');
i want to check if $objTest->Content2->prop3 exist, but i don't know at the right moment i am looking for this because what i'm looking for is in the associative array.
The array for the call look like:
$map = array('Content3','Content2->prop3');
From now i am able to get the content of the Content3 property by doing this:
foreach ($map as $name => $value) {
if (isset($object->$name)) {
echo "$value: ". json_encode($object->$name)."\n";
}
}
But not for the other because of the reference "->".
Now my question:
Is there a way to get an unknown property of an unknown object as displayed above?
This is a result of the previous test:
Dump of objTests:
object(stdClass)[1]
public 'Content1' => string '' (length=0)
public 'Content2' => object(stdClass)[2]
public 'prop1' => int 1
public 'prop2' => int 2
public 'prop3' => int 3
public 'Content3' => int 3
public 'Content4' => array (size=2)
'itm1' => int 1
'itm2' => string 'two' (length=3)
Trying to access the proprerty prop3 of the content2 of the object with a string:
Standard way to get the value : $objTest->Content2->prop3
Result : 3
Test string: "Content3"
Result: 3
Test astring: "Content2->prop3"
( ! ) Notice: Undefined property: stdClass::$Content2->prop3
Hope i put everything to help understand my situation!
Thanks!
I don't know of a built-in PHP function that does this, but a function could be used to break up the string of properties and iterate through them to find the value of the last one in the string.
function get_property($object, $prop_string, $delimiter = '->') {
$prop_array = explode($delimiter, $prop_string);
foreach ($prop_array as $property) {
if (isset($object->{$property}))
$object = $object->{$property};
else
return;
}
return $object;
}
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 />';
}