Combine two arrays on php - php

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;
}
}

Related

Create Array With Key And Value Pair In Codeigniter 4

I am trying to create and array to fill for my form_dropdown() for my view:
<?php echo form_dropdown('vendor', $vendors, '', 'class="form-control"') ?>
However, I am only getting the last value based on the function that generates the array:
public function get_vendors() {
$vendors = $this->db->table('vendor')->get()->getResultArray();
// Return key => value pair array
$array = array(
0 => 'Not Assigned'
);
if (!empty($vendors)) {
foreach ($vendors as $vendor) {
$array = array(
$vendor['id'] => $vendor['vendor']
);
}
}
return $array;
}
This function is within my model. The only problem I have is that it's returning only the last row from the table. However when I do a var_dump() for the $vendors I get all the rows. Not sure what I am doing wrong.
array (size=2)
0 =>
array (size=4)
'id' => string '1' (length=1)
'vendor' => string 'Blue Chip' (length=9)
'datecreated' => string '2022-08-16' (length=10)
'datemodified' => string '2022-08-16' (length=10)
1 =>
array (size=4)
'id' => string '2' (length=1)
'vendor' => string 'ASP' (length=3)
'datecreated' => string '2022-08-30' (length=10)
'datemodified' => string '2022-08-31' (length=10)
Changing the query to from getResultArray() to getResultObject() did the trick. So I was able to use $array[$vendor->id] = $vendor->vendor;
public function get_vendors() {
$vendors = $this->db->table('vendor')->get()->getResultObject();
// Return key => value pair array
$array = array(
0 => 'Not Assigned'
);
if (!empty($vendors)) {
foreach ($vendors as $vendor) {
$array[$vendor->id] = $vendor->vendor;
}
}
return $array;
}
If you still want to use getResultArray():
public function get_vendors() {
$vendors = $this->db->table('vendor')->get()->getResultArray();
// Return key => value pair array
$array = array(
0 => 'Not Assigned'
);
if (!empty($vendors)) {
foreach ($vendors as $vendor) {
$array[$vendor['id']] => $vendor['vendor'];
}
}
return $array;
}

Join array elements, display only lowest value

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;

How to remove a specific index from a multidimensional array in php

I am trying to remove pieces of a multidimensional array if a certain condition is met. The array can be as shown below, call it $friends:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
I have a function called is_followed, that let's me see if the id of one of the people in the array is being followed by the "user". The code I am trying is:
//remove followed friends from the $friends array
$i=0;
foreach($friends as $friend) {
foreach($friend as $f) {
if(Fanfollow::is_followed($id,$f->id)) {
unset($friend[$i]);
}
}
$i++;
}
The $id is the id of the current user.
However, this is not working. I know that using unset on $friend and not $friends is probably the issue. But using unset on $friends also won't work, because it is the higher level array. Any ideas? Thank you.
If you're trying to take down the first parent keys, use the first foreach keys instead:
foreach($friends as $i => $friend) {
// ^ assign a key
foreach($friend as $f) {
if(Fanfollow::is_followed($id,$f->id)) {
unset($friends[$i]);
// unset this
}
}
}
Or if only for that single friend:
foreach($friends as $friend) {
foreach($friend as $i => $f) {
// ^ this key
if(Fanfollow::is_followed($id,$f->id)) {
unset($friend[$i]);
// unset this
}
}
}
array_filter comes to the rescue:
array_filter($friend,
function($f) use($id) { return Fanfollow::is_followed($id,$f->id)); }
);
Though the solution with foreach is legit, array_filteris much more clear and sematically correct.

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

How to print properties and key value in array

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;
}
}

Categories