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
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 would like to check if the value of a standard class object has a value of 'None'. The object is a PDO and pulls data from the database.
Here is the data from a var_dump:
array (size=1)
0 =>
object(stdClass)[8]
public 'Subid' => string '1' (length=1)
public 'id' => string '27' (length=2)
public 'english' => string 'English Standard' (length=16)
public 'maths' => string 'Mathematics General 2' (length=21)
public 'hsie1' => string 'Modern History' (length=14)
public 'science1' => string 'Biology' (length=7)
public 'science2' => string 'None' (length=4)
public 'science3' => string 'None' (length=4)
public 'tech1' => string 'Agriculture' (length=11)
public 'tech2' => string 'None' (length=4)
public 'extension' => string 'None' (length=4)
public 'arts1' => string 'None' (length=4)
public 'arts2' => string 'Dance' (length=5)
public 'pdhpe1' => string 'None' (length=4)
public 'vet1' => string 'None' (length=4)
public 'start_time' => string '2016-11-15 19:54:08' (length=19)
Is it possible to loop through the keys and test the values?
You can foreach over an array and the public properties of an object
So
foreach ( $array as $obj ) {
foreach ( $obj as $prop => $val ) {
if ( $val == 'None' ) {
echo $prop . ' = ' . $val;
}
}
}
EDIT:
I dont see why you had to change anything here is a quick test I ran
$o = new stdClass;
$o->p1 = 1;
$o->p2 = 'two';
$o->p3 = 'None';
$array = array($o);
var_dump($array);
foreach ( $array as $obj ) {
foreach ( $obj as $prop => $val ) {
if ( $val == 'None' ) {
echo $prop . ' = ' . $val;
}
}
}
And the results were
array(1) {
[0] =>
class stdClass#1 (3) {
public $p1 => int(1)
public $p2 => string(3) "two"
public $p3 => string(4) "None"
}
}
p3 = None
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;
}
}
In my last question, was created a way to divide a array based in the string pattern Father_son, but now i need a way to split too a new pattern: Father_N_son
Original (generated by http://ideone.com/pmHHR code) :
array
'Pagamento' =>
array
'data' => string '' (length=0)
'0_pessoaId' => string '85' (length=2)
'0_valorBruto' => string '890.00' (length=6)
'1_pessoaId' => string '83' (length=2)
'1_valorBruto' => string '20.00' (length=5)
Now i need:
array
'Pagamento' =>
array
'data' => string '' (length=0)
0 =>
array
'pessoaId' => string '85' (length=2)
'valorBruto' => string '890.00' (length=6)
1 =>
array
'pessoaId' => string '83' (length=2)
'valorBruto' => string '20.00' (length=5)
Thanks,
Celso
my solution with regex:
static function parserRequest($array = null) {
if (empty($array)) {
$array = $_REQUEST;
}
foreach ($array as $k => $v) {
$name = explode('_', $k);
$newkey = array_shift($name);
$newname = implode('_', $name);
//para o padrão Entidade.n.atributo
if(preg_match("/[0-9]_[a-z]/", strtolower($newname))){
$nameValued = explode('_',$newname);
$newkeyValued = array_shift($name);
$newnameValue = implode('_', $name);
$result[$newkey][$newkeyValued][$newnameValue] = $v;
}else{
//para o padrão Entidade.atributo
$result[$newkey][$newname] = $v;
}
}
return $result;
}