I got the following array (the array is retrieved through a db query). Now, my question is, how do I get a single element like e_domains from the array mentioned below:
stdClass Object
(
[id] => 1
[uni_origin] => Aachen
[e_domains] => rwth-aachen.de
)
I got the output shown above by running the following line of codes:
if ($results ) {
foreach ( $results as $result ){
echo'<pre>'; print_r($result) ;
}
}
First off, that's not an array, that's an object. Like it says: "stdClass Object".
Access object properties like this:
$object->property_name
In your case, it would be:
$result->e_domains
There are much more to learn on the subject, like static properties, visibility etc. In your case, the above example will work.
Read more about classes and objects in the manual: http://php.net/manual/en/language.oop5.basic.php
Try this:
$e_domains = mysql_result(mysql_query("SELECT id FROM games LIMIT 1"),0);
Hope it helpt.
Related
I am having a problem trying to pass a variable from the controller to the view. I think I am doing a mistake somewhere, I can't find it since I am new to it.
My controller Method is :
public function paymentdetail($order_code)
{
$this->load->model('Payment_Model');
$paymentDetail = $this->Payment_Model->getpaymentdetail($order_code);
$orderDetail = $this->Payment_Model->getOrderDetail($order_code);
// echo print_r($paymentDetail);
$datas['content'] = $this->load->view('payment/paymentDetail',
array('paymentDetail' => $paymentDetail,
'orderDetail' => $orderDetail), true);
$this->load->view('layouts/main_template', $datas);
}
The following model function getpaymentdetail() returns an array (result_array()) and this is the one I am concerned about. If I can work this one out then I can also work with other model methods.
$this->Payment_Model->getpaymentdetail($order_code);
When I type <?php echo $paymentDetail['column_name']; ?> in view file(PaymentDetail.php) I get an error
Undefined index: column_name
Why do I get this error?
model function getpaymentdetail() returns an array (result_array())
those arrays are normally structured like this:
Array
(
[0] => Array
(
[ID] => 3120
[column_name] => col1
)
)
hence you cannot access column_name via <?php echo $paymentDetail['column_name']; ?> as this index doesn't exist in your array structure.
if you move one index deeper, then it will work: <?php echo $paymentDetail[0]['column_name']; ?>
Attention, if you expect more than 1 row to be returned, above will only access the first result (indexed 0) row! You'd need a foreach loop to get all results, see Generating Query Results - result arrays
I am trying to print a number from an array. But when i do this:
echo $results[0][0];
i get error.I tried to print the whole array using print_r() function
echo print_r($results);
Then i get this result:
Array ( [0] => stdClass Object ( [lastOrderProcessedNumber] => 109089875875875 ) ) 1
I just need to print "109089875875875" this number
How can i do that?
Thank you in advance
print_r() is a great way to inspect the contents of a variable. What it is showing you is that your variable holds an array whose first element (at index 0) is an object with an lastOrderProcessedNumber attribute. In PHP, you use -> to access object properties, so you should be able to retrieve the 109089875875875 value like this:
$results[0]->lastOrderProcessedNumber
As you can see from the print_r result, the indexed result is an object:
// $results is an 'Array' (access with square brackets)
Array
(
// Index 0 is an Object (access with arrow operator)
[0] => stdClass Object
(
[lastOrderProcessedNumber] => 109089875875875
)
)
This means you have to access the property through the arrow operator, like so:
$results[0]->lastOrderProcessedNumber
If you're expecting to only have one result, or to grab the first result from $results, you can make use of reset:
reset($results)->lastOrderProcessedNumber
If you see $results is an array of objects, that means that $results[0] is an object, not an array, so you can't access its attributes as an array but instead as an object. Like this:
$results[0]->lastOrderProcessedNumber;
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;
}
Using json_decode, I've ended up with an object that looks like this:
$data->foo->bar->1234567->id
I want to access id. There are two problems, both with the number 1234567:
It's an illegal property name.
The number will differ each time, and I can't predict what the number will be. I need a way of accessing id, even when I don't know the number.
I know I can overcome problem (1) with curly braces, but I don't know how to overcome (2). I don't want to use get_object_vars, because the object is likely to be very large, and that function is very slow.
My current solution is simply
foreach ($data->foo->bar as $id); but that feels rather hacky. Is there a better way?
From my comment above, using json_decode(,true) and then resetting.
The example json array looks like:
Array (
[foo] => Array (
[bar] => Array (
[1234567] => Array (
[id] => 1234
)
)
)
)
The code:
<?php
$data = json_decode('{"foo":{"bar":{"1234567":{"id":1234}}}}', true);
reset($data['foo']['bar']);
$number = key($data['foo']['bar']);
echo $data['foo']['bar'][$number]['id'];
Output: 1234
In case you don't need the whole array anymore and only want to get the id you can get it like this:
<?php
$data = json_decode('{"foo":{"bar":{"1234567":{"id":1234}}}}', true);
echo array_shift($data['foo']['bar'])['id'];
Only works if the unknown key is the first element of bar. array_shift removes the element from $data.
The raw output of my object is when I do a print_r is:
Array ( [0] => stdClass Object ( [COUNT(*)] => 3 ) )
How do I get to the 3?
This object is the result of a sql query inside wordpress with the $wpdb class.
I am a noob at PHP objects. Also would like to know
where do I learn to do object parsing stuff like this?
What kind of object is this? Why is it wrapped in an Array?
UPDATE: here's the source code:
global $wpdb;
$post_count = $wpdb->get_results("SELECT COUNT(*) FROM $wpdb->posts");
print_r($post_count);
It's not an object. It's an array containing an object. In that specific example, assuming that the variable is named $variable, you'd do this:
echo $variable[0]->{'COUNT(*)'};
I found the solution:
global $wpdb;
$post_count = $wpdb->get_results("SELECT COUNT(*) as postcount FROM $wpdb->posts");
print_r($post_count[0]->postcount);
I needed to use an alias.