I need to access the keys and properties of the arrays. I am bit confuse, I don't know how to handle this easily.
This is the code I run,
foreach ($posts as $key=> $value){
if($value->total_skill!='na'&& $value->total_skill!='0'){
$selcted = $wpdb->get_results("SELECT `$selection` FROM wp_skilllist WHERE First_name = '$value->First_Name' ");
var_dump($selcted);
}
I get following result. I notice that there are lot of arrays inside the arrays. I need to access
propeties and print their results.
As an example
FMS_Web_tec_HTML 4
FMS_Web_tec_CSS 3
FMS_Web_tec_XML 4
FMS_Web_tec_JavaScript 2
array (size=1)
0 =>
object(stdClass)[257]
public 'FMS_Web_tec_HTML' => string '4' (length=1)
public 'FMS_Web_tec_CSS' => string '3' (length=1)
public 'FMS_Web_tec_XML' => string '4' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
array (size=1)
0 =>
object(stdClass)[258]
public 'FMS_Web_tec_HTML' => string '3' (length=1)
public 'FMS_Web_tec_CSS' => string '3' (length=1)
public 'FMS_Web_tec_XML' => string '2' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
array (size=1)
0 =>
object(stdClass)[257]
public 'FMS_Web_tec_HTML' => string '3' (length=1)
public 'FMS_Web_tec_CSS' => string '2' (length=1)
public 'FMS_Web_tec_XML' => string '3' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
i think your trying to access the data set in $selcted
foreach ($selcted as $sel) {
$vars = get_object_vars($sel);
foreach ($vars as $key => $var) {
echo $sel->$key;
}
}
Related
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);
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>';
}
My array:
array (size=3)
0 =>
object(stdClass)[20]
public 'PkID' => string '488' (length=3)
public 'Price' => string '666' (length=3)
public 'discount_id' => string '1' (length=1)
1 =>
object(stdClass)[38]
public 'PkID' => string '490' (length=3)
public 'Price' => string '999' (length=3)
public 'discount_id' => string '2' (length=1)
2 =>
object(stdClass)[41]
public 'PkID' => string '489' (length=3)
public 'Price' => string '111' (length=3)
public 'discount_id' => string '1' (length=1)
Question is how can I group elements together that share the same discount_id number. But when I group, I wish that only the lowest Price integer is displayed.
EDIT: I tried
foreach ($array as $value)
{
$new_array[$value->discount_id] = $value;
}
Which returns grouped arrays like so:
array (size=2)
1 =>
object(stdClass)[41]
public 'PkID' => string '489' (length=3)
public 'Price' => string '111' (length=3)
public 'discount_id' => string '1' (length=1)
2 =>
object(stdClass)[38]
public 'PkID' => string '490' (length=3)
public 'Price' => string '999' (length=3)
public 'discount_id' => string '2' (length=1)
But I don't know how to display the smallest price from those two grouped elements (in the example above it is the smallest but this is only coincidence)
$new_array = array();
foreach ($array as $value) {
if (array_key_exists($value->discount_id, $new_array)) { // element with given discount_id already exists
if ($new_array[$value->discount_id]->Price > $value->Price) { // existing element has higher price - replace it
$new_array[$value->discount_id] = $value;
}
} else { // add new element
$new_array[$value->discount_id] = $value;
}
}
simplified:
$new_array = array();
foreach ($array as $value)
if (!array_key_exists($value->discount_id, $new_array) || $new_array[$value->discount_id]->Price > $value->Price) // no element or existing element has higher price
$new_array[$value->discount_id] = $value;
Hello i am trying to combine two php array.
First one
array (size=13)
0 =>
object(stdClass)[30]
public 'ID' => string '1' (length=1)
public 'name' => string 'html5' (length=5)
public 'img' => string 'HTML5.png' (length=9)
1 =>
object(stdClass)[31]
public 'ID' => string '2' (length=1)
public 'name' => string 'css3' (length=4)
public 'img' => string 'css.png' (length=7)
2 =>
object(stdClass)[32]
public 'ID' => string '3' (length=1)
public 'name' => string 'php' (length=3)
public 'img' => string 'php1.png' (length=8)
3 =>
object(stdClass)[33]
public 'ID' => string '4' (length=1)
public 'name' => string 'java script' (length=11)
public 'img' => string 'javascript.png' (length=14)
Second one
array (size=3)
0 =>
object(stdClass)[26]
public 'ID' => string '1' (length=1)
public 'IDuser' => string '1' (length=1)
public 'IDskill' => string '1' (length=1)
1 =>
object(stdClass)[27]
public 'ID' => string '2' (length=1)
public 'IDuser' => string '1' (length=1)
public 'IDskill' => string '3' (length=1)
2 =>
object(stdClass)[28]
public 'ID' => string '3' (length=1)
public 'IDuser' => string '1' (length=1)
public 'IDskill' => string '4' (length=1)
ID from first array is equal to IDskill from second array. I am trying to combine to create new array if IDskill and ID are same, with something like this in new array
public 'ID' => string '1' (length=1)
public 'name' => string 'html5' (length=5)
public 'img' => string 'HTML5.png' (length=9)
===>New field public 'MATCH' => string '1' (length=9)
use array_merge() function
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
or
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
Try this ( where a1 is first array and a2 is second one and $result is final, merged array ):
$result = array();
$n=0;
foreach($a1 as $k=>$v) {
if (isset($v->ID) && isset($a2[$n]->ID) && $v->ID==$a2[$n]->ID) {
$result[$n]=$v;
$result[$n]->MATCH=1;
}
$n++;
}
print_r($result);
You could use array_map and anonymous functions:
<?php
$first_array = array(...);
$second_array = array(...);
// Anonymous function to extract the IDskills from the second array:
$get_IDs = function($element) {
return($element->IDskill);
};
// Array of (just) IDskills of the second array:
$IDs = array_map($get_IDs, array_filter($second_array, function($element) {
// Anonymous function to filter $second_array elements without IDskill:
return(isset($element->IDskill));
}));
// Another anonymous function that returns a new array with elements from
// $first_array with the new MATCH property.
// use(&$IDs) makes $IDs available inside the function scope:
$check_match = function($element) use(&$IDs) {
// We use clone because we don't want to modify the original array values:
$match_element = clone $element;
if(isset($match_element->ID))
$match_element->MATCH = in_array($match_element->ID, $IDs);
else
$match_element->MATCH = false;
return($match_element);
};
$match = array_map($check_match, $first_array);
?>
If you don't want to use the two first anonymous functions or don't want to create the $IDs array you can replace use(&$IDs) for use(&$second_array) and the line:
$match_element->MATCH = in_array($match_element->ID, $IDs);
for a loop that iterates through $second_array elements to check whether any of them has an IDskill equal to $match_element->ID.
Moreover, you may not need to check if IDskill and ID exist if all the elements in the arrays have the same properties.
let me add my two cents, how to find element matching ID from the 1st array in the second one
$tmp = array_filter($array2, function($ar2) use ($ID) { return $ar2->IDskill === $ID; });
if ($tmp) // Found
else // Not found
all code may be
result = array()
foreach ($array1 as $item) {
$ID = $item->ID;
$tmp = array_filter($array2, function($ar2) use ($ID) { return $ar2->IDskill === $ID; });
if ($tmp) { // Found
$tmp = $item;
$tmp->MATCH = 1;
result[] = $tmp;
}
}
Consider:
array (size=1)
0 =>
array (size=5)
'name' => string '15268459735 Farming and Projects XXXXX' (length=38)
'region' => string '2' (length=1)
'entitynumber' => string '2012/002086/24' (length=14)
'ownership' => string '6' (length=1)
'id' => string '26249' (length=5)
Why does the following still return the same array without the owner element?
foreach ($result as $row)
{
$row['owner'] = 1;
}
Try passing the array by reference:
foreach ($result as &$row) {
$row['owner'] = 1;
}
See also: What's the & for, anyway?