I am fetching json data from a table called properties. Column name is attr and it has size,bedrooms and prop-type in it.
$q= mysqli_query($connect,"SELECT * FROM properties");
$savemyval = array();
while($row= mysqli_fetch_assoc($q)){
$data = json_decode($row['attr']);
//var_dump($data);
if($proptpe == $data->proptype){
$savemyval[] = $row['id'];
}
}
Querying data like above if i var_dump this is what i get
object(stdClass)[3]
public 'bedrooms' => string '5' (length=1)
public 'proptype' => string 'residential' (length=11)
object(stdClass)[4]
public 'bedrooms' => string '4' (length=1)
public 'proptype' => string 'commercial' (length=10)
object(stdClass)[3]
public 'size' => string '16000' (length=5)
public 'prop-type' => string 'commercial' (length=10)
in var_dump i get proper data but when i try to get proprtype, if its more than 1 it gives me the error
PHP : Notice: Undefined property: stdClass::
if I use isset then there is no error but still it prints one result while dumping gives me more than 1 results
Because in your array you doesn't have proptype on second index
try like this
if(isset( $data['proptype']) && $proptpe == $data->proptype){
$savemyval[] = $row['id'];
}
Related
I'm having trouble finding out why is the following code not working.
I've a JSON answer from a remote server, containting the following data:
...., "UserId":{"50423":"Free Kkludkjta","54379":"Sjkllyu\u00e9lkj Nolla","67103":"Tswt\u00f3 BLLA","64469":"Uz\u00e1h G","46699":"RT\u00e1sdt UTSF","46873":"Tam\u00e1s XXXX"}, ...
(names are swapped, but since there are some special chars in the aswer I've kept those)
And I have the following code sniplet, which gives 'array index undefined for idx 64469':
$proj_schema = json_decode($proj_schema);
var_dump($proj_schema->UserId);
$avail_users = (array)$proj_schema->UserId;
var_dump($avail_users);
var_dump($avail_users[64469]);
The output is the following:
C:\wamp\www\j34\administrator\components\com_mycomponent\views\myview\view.html.php:113:
object(stdClass)[309]
public '50423' => string 'Name1' (length=14)
public '54379' => string 'Name2' (length=18)
public '67103' => string 'Name3' (length=12)
public '64469' => string 'Name4' (length=15)
public '46699' => string 'Name5' (length=11)
public '46873' => string 'Name6' (length=12)
C:\wamp\www\j34\administrator\components\com_mycomponent\views\myview\view.html.php:115:
array (size=6)
'50423' => string 'Name1' (length=14)
'54379' => string 'Name2' (length=18)
'67103' => string 'Name3' (length=12)
'64469' => string 'Name4' (length=15)
'46699' => string 'Name5' (length=11)
'46873' => string 'Name6' (length=12)
Notice: Undefined offset: 64469 in C:\wamp\www\j34\administrator\components\com_mycomponent\views\myview\view.html.php on line 116
If I walk through the array with foreach and write out the element based on 'if ($key == 64469)' it echos the name, but I cannot access the name by array key.
Results are same with PHP 5.6.32 (I know) and PHP 7.1.0. Result is the same if I put the id in "" or '';
What am I missing with the casting?
You are using the JSON as an object and not as an array. Therefore, the key does not exist, because it is a property.
Decode with true as 2nd argument to get an array instead of an object.
$proj_schema = json_decode($proj_schema, true);
Below are my database value and i want to retrieve answer_id using php.
a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}
Try below code, it will return answer id to you.
$str = "a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}";
$arry = unserialize($str);
echo $arry[0]->answer_id;
The Issues is that your serialized String contains back-slashes which would mess with the serialized object. Solution: Remove the backslashes and unserialzed the string and you'd get your object back:
<?php
$strSerializedWithSlashes = 'a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}';
$strSerializedWithoutSlashes = str_replace("\\", "", $strSerializedWithSlashes);
$objUnSerialized = unserialize($strSerializedWithoutSlashes);
var_dump($objUnSerialized);
// DUMPS::
array (size=1)
0 =>
object(stdClass)[1]
public 'question_id' => string '1' (length=1)
public 'question_text' => string 'This is question 1' (length=18)
public 'answer_id' => string '2' (length=1)
public 'answer_text' => string 'asss' (length=4)
public 'points_base' => string '2' (length=1)
public 'points' => string '2' (length=1)
public 'custom_response' => string '' (length=0)
You can test it here: https://eval.in/571535
And now; to get you answer_id You can simply do this:
<?php
$objData = $objUnSerialized[0];
$answerID = $objData->answer_id;
var_dump($answerID); // DUMPS: '2'
I got following invalid code: (e.g. $column->Field == 'email')
echo $row[$column->Field];
With the Error:
Fatal error: Cannot use object of type stdClass as array
Thats the var_dump of $row:
object(stdClass)[17]
public 'id' => string '1' (length=1)
public 'email' => string 'master' (length=9)
public 'Name' => string 'THE MASTER' (length=28)
public 'reply' => string '1' (length=1)
I now what the error means i just can'T figure out how to work around it (i Might be too tired)
Im looking for something like that: What is the correct/working way to do so?
echo $row->$column->Field;
IDK how i didnt got to that earlier but i just defined a variable before hand
$field = $column->Field
echo $row->$field;
So 2 Solutios to this one:
1) Define a Variable:
$field = $column->Field;
echo $row->$field;
2) Credit to Abdo Adel:
If you want to do it in one line, try
$row->{$column->Field}
I have an object with a property:
protected $recipients = array();
In one of my methods I set the contents of the $recipients property with an associative array of data using this method:
public function setRecipientData($recipientData){
foreach($recipientData as $key => $value){
$this->recipients[$key] = $value;
}
}
When I dump the contents of $this->recipients, the array ends up looking like this:
array (size=2)
0 =>
array (size=6)
'email' => string 'info#mycompany.com' (length=29)
'userEmail' => string 'xxx#yyy.com' (length=11)
'first_name' => string 'Test' (length=4)
'last_name' => string 'User' (length=4)
'jobTitle' => string 'Customer User' (length=13)
'phoneNumber' => string '123.456.7890' (length=12)
I also have a property that will extract only the email addresses from the property like this:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
$emailPair = array('email' => $recipient['userEmail']);
$emails[] = $emailPair;
}
return $emails;
}
The problem that I'm running into is that when I run the method getRecipientsEmails() I get the error:
Undefined index: userEmail
I don't understand why it's not seeing the index 'userEmail'. If I dump the nested array it shows a value:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
dd($emailPair = array('email' => $recipient['userEmail']));
$emails[] = $emailPair;
}
return $emails;
}
Result:
array (size=1)
'email' => string 'xxx#yyy.com' (length=20)
I've tried dumping the autoload with composer but it makes no difference.
Can anyone help point me in the right direction??
EDIT: Sorry, I realized that I wrote the wrong method into my original post. I've added the original getRecipientEmails() method and the dump that shows the data.
I am returning data from two tables in CodeIgniter with the function below
public function test()
{
$this->db->select('*');
$this->db->from('WHOUSE1.DLY_BWR_DLY_PERFORMANCE');
$this->db->join('WHOUSE1.DATE_DIM', 'WHOUSE1.DATE_DIM.DATE_KEY = WHOUSE1.DLY_BWR_DLY_PERFORMANCE.BDP_DATE');
$query = $this->db->get();
return $query->result_array();
}
Using var_dump I am getting the result below
array (size=3226)
0 =>
array (size=121)
'BDP_ID' => string '945149' (length=6)
'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
'BDP_DATE' => string '20040807' (length=8)
'BDP_DAY_CODE' => string '6' (length=1)
'BDP_TAKE' => string '4923.78' (length=7)
'BDP_PAYOUT' => string '3779.22' (length=7)
'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)
1 =>
array (size=121)
'BDP_ID' => string '945150' (length=6)
'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
'BDP_DATE' => string '20040809' (length=8)
'BDP_DAY_CODE' => string '1' (length=1)
'BDP_TAKE' => string '2848.3' (length=6)
'BDP_PAYOUT' => string '4190.34' (length=7)
'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)
But what I will like to get is this
array (size=3226)
0 =>
object(stdClass)[27]
'BDP_ID' => string '945149' (length=6)
'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
'BDP_DATE' => string '20040807' (length=8)
'BDP_DAY_CODE' => string '6' (length=1)
'BDP_TAKE' => string '4923.78' (length=7)
'BDP_PAYOUT' => string '3779.22' (length=7)
'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)
1 =>
object(stdClass)[29]
'BDP_ID' => string '945150' (length=6)
'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
'BDP_DATE' => string '20040809' (length=8)
'BDP_DAY_CODE' => string '1' (length=1)
'BDP_TAKE' => string '2848.3' (length=6)
'BDP_PAYOUT' => string '4190.34' (length=7)
'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)
I can't seem to get a way of converting the array into object(stdClass) Any help will be appreciated as am new to CodeIgniter.
Array to stdClass can be done in php this way.
stdClass:: __set_state(array());
Or a nicer way.
$a = (object) array();
Use this code in Your Model ( change your table name and select, distinct fileds )
$this->db->select('DISTINCT(subcategory)');
$this->db->from('tbl_property');
$this->db->where('status','1');
$data = $this->db->get()->result();
$sub_id = array();
foreach ($data as $row)
{
array_push($sub_id,$row->subcategory);
}
$this->db->from('tbl_subcategory');
$this->db->where_in('id',$sub_id);
$data1 = $this->db->get()->result();
return $data1;
use
return $query->result();
This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
Edit:
If you just want to print you can use var_dump() or print_r().
var_dump($obj);
print_r($obj);
If you want an array of all properties and their values use get_object_vars().
$properties = get_object_vars($obj);
print_r($properties);
According to the same documentation for Codeigniter I detail:
result_array()
This function returns the query result as a pure array, or an empty array when no
result `is produced. Typically you'll use this in a foreach loop, like this:`
and ..
result()
This function returns the query result as an array of objects, or an empty array
on failure.
You should return your data in this way
return $query->result();