json decode and property accessing fails [duplicate] - php

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?

<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}

Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

Related

Access Special Character Key from STD array in PHP [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

how to retrieve constituency area from uk ons open data geography portal boundaries data attribute st_area(shape) [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

fetching data from JSON string using php * syntax [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

Codeigniter : Parse array passed to view

i've to parse an array and print its reslut in a table ,
this is how i am passing array to view in
controller
public function history(){
$history_result= array();
$history_result = $this->user_model->user_history($this->user_id,0);
$this->result_set['data'] = $history_result;
$this->load->view('test', $this->result_set);
}
in view if i var_dump $data i get
array (size=1)
0 =>
object(stdClass)[19]
public 'id' => string '1' (length=1)
public 'user_id' => string '1' (length=1)
public 'sender_name' => string 'test' (length=14)
public 'sender_mobile' => string '12323' (length=10)
public 'receiver_name' => string 'sfsf' (length=4)
public 'sender_location' => string 'sfsf' (length=4)
public 'receiver_location' => string 'sfsfs' (length=5)
public 'receiver_mobile' => string '0' (length=1)
public 'is_urget' => string '0' (length=1)
public 'is_assigned' => string '0' (length=1)
public 'request_type' => string '0' (length=1)
public 'attachment_id' => string '' (length=0)
public 'status' => string '0' (length=1)
my question is how do i access each item out of this result so that may print in a html table like this
<tr>id : 1</tr>
<tr>sender name : name..</tr>
If you have this in your model
return = $this->db->get()->row();//one result
In your view you can display like
$data->id . '<br>' . $data->user_id . '<br' . $data->sender_name ...
if in your model get a multiple rows like
return = $this->db->get()->result();//as many as you have
In your view
foreach($data as $item){
echo '<tr>';
echo '<td>'.$item->id.'</td>';
echo '<td>'.$item->user_id.'</td>';
echo '<td>'.$item->sender_name.'</td>';
...
echo '</tr>';
}

How to convert stdClass object in to refrence array? (PHP)

I have variable $related witch is stdCalss object and i want to convert it with one foreach loop in to reffrence array.
Var_dump of Object $related:
array (size=19)
0 =>
object(stdClass)[20]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1567' (length=4)
1 =>
object(stdClass)[21]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1562' (length=4)
2 =>
object(stdClass)[22]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1564' (length=4)
3 =>
object(stdClass)[23]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1410' (length=4)
4 =>
object(stdClass)[24]
public 'id_product' => string '111' (length=3)
public 'related' => string '77' (length=2)
5 =>
object(stdClass)[25]
public 'id_product' => string '111' (length=3)
public 'related' => string '1610' (length=4)
Php code:
foreach ($related AS $r){
????
}
var_dump($rels);
Wished Output:
$rels = array(
'1568'=>'1567, 1562, 1564, 1410',
'111'=>'77,1610'
);
Build a temporary array and implode it:
foreach($related AS $r) {
$temp[$r->id_product][] = $r->related;
$rels[$r->id_product] = implode(', ', $temp[$r->id_product]);
}
print_r($rels);
$rels = array ();
foreach ($related as $r){
$rels[$r->id_product] .= $r->related . ","; // put all of them with respective key together
}
$rels = array_map(
function ($a) {
$a = preg_replace('~,(?!.*,)~', '', $a);
return $a;
}
,$rels); // remove last commas
var_dump($rels);
Try,
$rels = array();
foreach($related as $r){
$rels[$r->id_product] .= $r->related.', ';
}
array_walk_recursive($related, function(&$item){
$item = rtrim($item,', ');
});
From the PHP docs: http://php.net/manual/en/language.types.type-juggling.php

Categories