As per documentation, I've tried below code to find size of array in codeigniter
echo element('size', $get, NULL);
but it ended up showing following error
( ! ) Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\cinifb_ci\system\helpers\array_helper.php on line 46
I've tried to load content of $get into another array variable, but it continued showing the above error.
Please suggest me the alternative ways along with solving this.
I've tried to using native PHP solutions like
echo size_array($get);
but It ended up in
Fatal error: Call to undefined function size_array()
Does this mean I'm not supposed to use native PHP functions in CodeIgniter
count($array);
That's the native function to get the size of an array ;)
Within CodeIgniter, this expression:
element('size', $get, null)
Only works if $get is an array and it has an index 'size'; if it is an array, it would be more likely that you meant this:
count($get);
However, in your case, $get is actually an object, stdClass to be exact; determining the size of that object requires another step:
count(get_object_vars($get));
Example:
$completed_requests = $this->db->where('status' => SOLICITACAO_FINALIZADA)
->where('trip_occurred' => OCORREU)
->count_all_results('transport_requests');
->count_all_results('TABLE_NAME');
Related
I'm retrieving the email list from a MySQL database along side with the IDs of the users it gets something like this
Array (
[0] => Array ( [ID] => 1 [Email] => email1 )
[1] => Array ( [ID] => 2 [Email] => email2 )
)
and while trying to test for the value of the last email "email2" I used
end(end($array_sample));
this used to work on my old server running PHP 5.0 and stopped at the new one running PHP 5.6
Was there something I did wrong or is it a php version?
I basically changed the whole approach to get the site to do what it was meant to do any how, but I still would like to learn about the end(end(array)) issue.
end() function needs to get array by reference, so it can't be a result of other function, because you get following error:
Only variables should be passed by reference
To avoid it assign result of inner end() to variable and then use end() on this variable:
$tmp = end($array);
$result = end($tmp);
And you probably don't get any error in previous version of PHP due to error_reporting set to quiet them.
According to documentation:
Prior to PHP 5.4.0 E_STRICT was not included within E_ALL, so you
would have to explicitly enable this kind of error level in PHP <
5.4.0.
As far as I know, your code should had never worked:
Only variables should be passed by reference
As documentation explains:
This array is passed by reference because it is modified by the
function. This means you must pass it a real variable and not a
function returning an array because only actual variables may be
passed by reference.
What has changed is the severity of the error. It has been a fatal error, a strict standards notice and a regular notice. Between 4.3.0 and 5.0.4 is just failed silently.
Most likely the error went unnoticed until you upgraded and an actual error message was triggered.
You have an end within another end, the inner one returns last element of array, the outer one is expecting an array not a single value
I am using CSV Import Pro to import products into my store using OpenCart.
The other day I was importing products which was going fine. Then after an import the extension keeps giving me this fatal error.
I've tried contacting support for damn near 5 days and I haven't gotten much from them. I really need to get this fixed.
Fatal error: Cannot use object of type stdClass as array in /home/content/71/11151671/html/admin/view/template/tool/csv_import.tpl on line 234
Usually there are tabs at the top that let me navigate to the other pieces of the module.
It would not let me post images
With the CSV Import Pro you want to go to the admin/controller/tool/csv_import.php
Search for this line of code:
$this->data[$key] = json_decode($this->data[$key];
and replace it with:
$this->data[$key] = json_decode($this->data[$key], true);
By adding the true at the end, you are basically telling the script you want your data to be in an array format, rather than an object.
Hope this helps ;)
Peter
Without code sample it's really just guessing, but my first impression is that you're trying to access some members of an object that was processed with json_decode. This will, by default, turn arrays into objects, to be more exact instances of PHP's StdClass. Either try accessing the members using object notation ($obj->member) or use the 2nd optional parameter to json_decode in which case the returned will be an associative array instead of an object.
See PHP doc for json_decode.
If you split a JSON-object, try to add a second parameter to the function json_decode:
$json = json_decode($string, true);
Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use
<?php echo $variable(); ?>
to output the function with the same name.
I'm using Codeigniter. It has a function in its Form helper to output a text field, which is
<?php form_input(); ?>
I have a variable
<?php $instance['taxon_field'] = 'form_input'; ?>
If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo
$instance['taxon_field']()
I get a warning:
Message: Illegal string offset 'taxon_field'
and a fatal error:
Call to undefined function p()
I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.
Where am I doing wrong?
The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p.
The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules, so the whole expression gets you the first letter of the string.
Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.
Use call_user_func()
call_user_func($instance['taxon_field']);
The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:
<?php
$bigger_array = array(
0 => array(
'field_one' => 'value_one',
'field_two' => 'value_two',
'field_three' => 'new_function'
),
1 => array(
'field_one' => 'new_value_one',
'field_two' => 'new_value_two',
'field_three' => 'echo'
)
);
function new_function()
{
echo 'New function called.';
}
foreach($bigger_array as $instance)
{
$name = $instance['field_three'];
$name('Hello World!');
}
?>
This will output the following:
New function called.
Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69
In other words, the newly defined function works fine, but the built-in 'echo' doesn't.
This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.
Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?
I'm working with some API integration and I have a limitation with understanding Objects.
Not my code, but here's what I have:
<?php
print_r(pingSample());
?>
The result on the browser is this:
PingResponse Object ( [PingResult] => 1 )
The function pingSample is not mine, it's from Docusign.
I want to just extract the "1", or if it's a bad result I'm sure it will return "0".
I'm not experienced with Object Oriented coding, yet. So, I'm assuming this is a simple example, in an API setting. But I'm not sure.
For those who want to laugh at my attempt:
$blah = pingSample();
echo $blah['PingResult'];
So far, nothing returns on the browser. Apache logs return this:
PHP Fatal error: Cannot use object of type PingResponse as array
How do I extract only the value of PingResult?
Object properties are accessed by using the pointer (arrow) notation, not array (bracket) notation.
$blah = pingSample();
echo $blah->PingResult;
Read more on Classes and Objects in PHP.
$blah = pingSample();
echo $blah->PingResult;
Try this :
echo $blah->PingResult;
Hey,
I was wondering if it was possible to pass an associative array as a parameter in a custom function. This is my scenario:
In the php file I set the array:
$dataArr = array('one'=>'1','two'=>'2','three'=>'3');
$tpl->assign('dataArr',$dataArr);
This is my custom function dulled down
function smarty_function_drawChart($params, &$smarty){
print_r($params);
}
This is my function call in the template
{drawChart data={$dataArr} title='Title of the Chart'}
The problem I am having is that if you notice where I print_r($params), that shows:
Array
(
[data] => Array
[title] => Title of the Chart
)
It seems to be passing the string 'Array' rather than the actual array. I have done debugging right before passing the $dataArr that shows {$dataArr.one} has a value. Once inside my custom function $params['data'].one does not exist.
Any ideas on what I am doing wrong?
Thanks
Levi
I am still not 100% sure why my code above didn't work. My thought is that the brackets work just as an 'echo' would do in php, which is why the string 'Array' was being passed into the function. I was able to get it to work by simple removing the brackets around the $dataArr variable.
This was my original call:
{drawChart data={$dataArr} title='Title of the Chart'}
This is my new call that works
{drawChart data=$dataArr title='Title of the Chart'}