retrieving array value having symbol as object using foreach - php

I would like to retrieve the field names in the database, one of which named 'wardno/houseno'. When i tried to retrieve it as object using foreach i'm getting
My code,
$this->db->select('*');
$this->db->from('tbl_application');
$this->db->where('app_no', '1/ML/201314/TVM');
$q=$this->db->get();
foreach($q->result() as $row)
echo $row->wardno/houseno;
The error shows,
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$wardno
Filename: controllers/admin_sona.php
Line Number: 19
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant houseno - assumed 'houseno'
Filename: controllers/admin_sona.php
Line Number: 19
A PHP Error was encountered
Severity: Warning
Message: Division by zero
Filename: controllers/admin_sona.php
Line Number: 19
Is it possible to retrieve the database value with symbol as object?

Can you please try to give
foreach($q->result_array() as $row)
echo $row['wardno/houseno'];// or even echo $row['wardno\/houseno'];
instead of
foreach($q->result() as $row)
echo $row->wardno/houseno;
Give it a try!
----EDIT------
As you dont want it to be converted into an array, try
echo $row[17]; //Just try. I am not sure where it hurts the code. In case of error, let me know in comments

try to change this:
$row->wardno
to this:
$row['wardno']
Same thing for houseno
$row['houseno']
don't know if you have afield named wardno/houseno inside your database, I thinked that you have two different fields. If is the same field I advise you to use _ instead of / inside your database.
If is the same field try this:
echo $row['wardno/houseno'];
to convert array to object try this:
function arrayToObject($array) {
if (!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
$arr = arrayToObject($your_array);

Related

Php class property get and set

I have the following codes.
<?php
class Reg {
private $pros = array();
public function __set($key,$val) {
$this->pros($key)= $val;
}
public function __get($key) {
return $this->pros($key);
}
}
$reg= new Reg;
$reg->tst="tst";
echo $reg->tst;
?>
But when executing this script I got the error following.
Fatal error : can't use method return value in write context in line 5
I believe that to add an element to array is possible like the above.
$array = array();
$array('key')='value';
Please make clear that I was wrong.
Thanks
The is because of you are trying to set a functions return value. $this->pros($key) means calling pros($key) function. Not setting a value to $pros array.
The syntax is wrong. Setting values to array be -
$array['index'] = 'value';
Change
$this->pros($key)= $val; -> $this->pros[$key]= $val;
and
return $this->pros[$key];
Working code
$this->pros[$key] = $value;
OR
$keys = array($key);
$this->pros = array_fill_keys($keys,$value);
The array_fill_keys() function fills an array with values, specifying keys.
Syntax:
array_fill_keys(keys,value);

php notice (trying to get property of non-object) error

So, I have following php for wp:
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = $userInfo->display_name; //this one
}
I am getting an error for $usersNames.
"PHP Notice: Trying to get property of non-object in /functions.php on line"
What is going on?
Any suggestions?
Thanks
EDIT:
So, for $userIDs, I have an array of user ids. Then I am trying to get user display name etc for individual ids.
Your function get_userdata() will return False on failure, WP_User object on success.
And error Trying to get property of non-object means that this function has returned false.
Simply check if $userInfo is not empty
$userInfo = get_userdata($userId);
if (!empty($userInfo)) {
$usersNames[] = $userInfo->display_name;
}
you need judge whether $userInfo return the right obj
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = isset($userInfo->display_name)?$userInfo->display_name:''; //this one
}

CakePHP: Cannot use a scalar value as an array

I am trying this code:
My Code snippet
public function beforeFind($query) {
$query = parent::beforeFind($query);
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}
But I am getting this error
Error
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
As said by commentor that $query is not an array, and beforeFind is returning boolean. So the following line is having problem.
$query = parent::beforeFind($query);
The correction is
There is no use of calling parent method as it is overridden it to implement own logic.
so the correct code is
public function beforeFind($query) {
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}

What is the correct way to push into an array that is a property of an object?

What is the correct way to push into an array that is a property of an object?
Error:
Fatal error: Cannot use [] for reading in ... on line ..
Code:
class StreamResponse
{
public $messages ;
function __construct($post)
{
$strippost = stripslashes_deep( $_POST );
foreach ($strippost['messages'] as $message)
{
$m = json_decode($message, true);
$this->$messages[] = $m; //<-- This line is the line with the issue
}
}
}
Don't use the dollar sign before messages when you are using $this
$this->messages[] = $m;

Property of a non object

I'm trying to find out why I'm getting a Trying to get property of non-object. I'm not completely skilled with objects and arrays but i'm trying. Here's the code and the error message. Any ideas on how to fix this issue?
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 208 and 215
function getSubMenuPages()
{
$this->db->select('site_menu_structures.id');
$this->db->from('site_menu_structures');
$this->db->where('site_menu_structures.short_name', 'mainnav');
$query = $this->db->get();
$menu_id = $query->row()->id;
$this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, is_category');
$this->db->from('site_menu_structures_links');
$this->db->where('site_menu_structures_links.menu_structure_id', $menu_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$linksArray->{$key}->child_links;
$this->db->select('site_menu_structures_links_children.link_name');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray->{$key}->child_links = $query->result();
}
}
}
}
return $linksArray;
}
My guess would be $linksArray is an array, not an object so the line
$linksArray->{$key}->child_links;
will not work. In any case, this line does nothing so why have it at all?
Where you assign a value to this "property", try this instead
$linksArray[$key]->child_links = $query->result();
"Trying to get property of non-object"
this type of errors only exist if you try to treat a variable as an object instance and you actually failed to create that instance successfully try to check this part of the code if this is really an object or not:
$linksArray->{$key}->child_links
this is on the bottom of your code.

Categories