Comparing two object arrays with array_udiff - php

Array 1: $tags_result
array (size=4)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
2 =>
object(stdClass)[10]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)
Array 2: $post_tags_result
array (size=2)
0 =>
object(stdClass)[5]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[6]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
I'm trying to extract the values of the array 1 that do not appear in the array 2:
function foo($tags_result, $post_tags_result){
return $tags_result->id_tag != $post_tags_result->id_tag;
}
$difference_tags = array_udiff($tags_result, $post_tags_result, 'foo');
But the result return a common value: tag 1. I expect just tag 4 and tag 9.
array (size=3)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)

As an alternative, you could gather all the tags that needed to be excluded first. Then after that you could now filter it thru array_filter and get the desired result. Rough example:
$tags = array();
foreach($post_tags_result as $t) {
$tags[] = $t->tag; // gather all tags
}
// filter array using gathered tags
$result = array_filter($tags_result, function($v) use($tags){
return !in_array($v->tag, $tags);
});
Sample Output

From the manual:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
So, instead of a Boolean comparison you should do a diff to delete equal items. Since you are comparing numeric values, you should do something like:
$ php -a
php > $a = [['id'=>1],['id'=>2],['id'=>3],['id'=>4]];
php > $b = [['id'=>1],['id'=>4]];
php > $c = array_udiff($a, $b, function($a, $b){
return $a['id'] - $b['id'];
});
php > print_r($c);
Array
(
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
)
In your case:
$difference_tags = array_udiff($tags_result, $post_tags_result, function(($a1, $a2){
return $a1->id_tag - $q2->id_tag;
}));

Related

Dynamic Array, taking data from select

I have an array that has this structure:
array (size=6)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
I have values from a selection that is Dorado->32GB->Madera and I need find this value in the array; if its true that it exists, then take the other values like a vlr, pcost, etc.
What is the best method to find that, remembering that the values with number are dynamic, sometimes with 1 or 5 example:
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
0 => string 'Azul' (length=6)
1 => string '32GB' (length=4)
Can somebody explain how I could do this?
I suppose, this is a code to start with:
$searchValue1 = 'Dorado';
$searchValue2 = '32GB';
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if ($item[0] === $searchValue1 && $item[1] === $searchValue2 && $item[2] === $searchValue3) {
print_r($item);
}
}
A bit sophisticated version with checking whether $searchValue_ is not empty is:
$searchValue1 = 'Dorado';
$searchValue2 = null;
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if (
(!empty($searchValue1) && $item[0] === $searchValue1)
&&
(!empty($searchValue2) && $item[1] === $searchValue2)
&&
(!empty($searchValue3) && $item[2] === $searchValue3)
) {
print_r($item);
}
}
update: with unknown number of search parameters you can:
$searchValues = ['Dorado', 'Plastico'];
foreach ($yourArray as $item) {
// find intersection of two arrays
$intersect = array_intersect($item, $searchValues);
// if size of intersection is same as size of `$searchValues`
// then you can be sure that all elements of `$searchValues` are in `$item`
if (count($intersect) === count($searchValues)) {
print_r($item);
}
}

Sorting 'key=>value' pair multi-dimensional arrays in PHP

I am creating a site and I am trying to do a few complicated things with arrays to get it to work.
It is for an e-commerce site and each product on my site can have a number of attributes. I am trying to get each combination of attributes to do the pricing for them seperately.
First I get an array of the attribute ids ($attribute_id_array) and the query the database for the options for that array.
So if one attribute was colors the options here would be red,green,blue,etc,. or size would be small,medium,large,etc,. These are then stored in a new array ($attribute_arrays).
I then go through these to get every combination of attributes the product can have and sort these into a new array again ($new_attributes_array).
I then loop through this and create a price form for each combination.
$attribute_arrays = [];
foreach($attribute_id_array as $attribute_id){
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
array_push($attribute_arrays,$row);
}
}
var_dump($attribute_arrays);
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
$tmp = combinations($arrays, $i + 1);
$result = array();
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
$new_attributes_array = combinations($attribute_arrays);
var_dump($new_attributes_array);
This is all working fine except I want to be able to get the keys for all of the key value pairs so I can reference it back to my database.
The way it comes out at the moment is like this:
$attribute_id_array:
array (size=2)
1 => string '5' (length=1)
2 => string '7' (length=1)
$attribute_arrays:
0 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
1 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
$new_attributes_array:
0 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '3 metres' (length=8)
1 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '6 metres' (length=8)
2 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '3 metres' (length=8)
3 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '6 metres' (length=8)
Is there a way to get it so that the key will be similar in format to:
0 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute1 => string '3 metres' (length=8)
1 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute2 => string '6 metres' (length=8)
Edit
So I changed the line array_push($attribute_arrays,$row); to $attribute_arrays[$attribute_id] = $row;.
This now means that $attribute_arrays now has the$attribute_id variable as the key like so:
array (size=2)
5 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
7 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
This now means my other function for getting the combinations won't work as it is using the $i variable as the index for the array starting at '0'.
Found another function online to sort it here How to generate in PHP all combinations of items in multiple arrays:
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $property => $property_values) {
$tmp = array();
foreach ($result as $result_item) {
foreach ($property_values as $property_key => $property_value) {
$tmp[] = $result_item + array($property_key => $property_value);
}
}
$result = $tmp;
}
return $result;
}
However, this doesn't do exactly as I want and I end up with this:
array (size=4)
0 =>
array (size=1)
'attribute1' => string 'Step Through Bars' (length=17)
1 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string '6 metres' (length=8)
2 =>
array (size=2)
'attribute2' => string 'Gated' (length=5)
'attribute1' => string '3 metres' (length=8)
3 =>
array (size=1)
'attribute2' => string 'Gated' (length=5)
try this as your combinations function
modified code taken from here
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $key => $values) {
$tmp = array();
foreach ($result as $item) {
foreach ($values as $k=>$value) {
$tmp[] = array_merge($item, array($key.'-'.$k => $value));
}
}
$result = $tmp;
}
return $result;
}
Instead of using while on following line
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
Use foreach loop to get key paired value. e.g
foreach($databaseFetchedRecord as $mykey=>$myvalue)

Swap two values in string(s) and sort by second value in php?

I have string like this:
EUR-USD,USD-EUR,SEk-CAD
I want to make this to an array and sort by second currency and I want to the result to be
SEk-CAD,USD-EUR,EUR-USD
(Sorted by CAD, EUR and USD)
This is my attempt and it works, but I'm wondering if I'm not "overdoing" this? Anyone has an easier/better solution to achieve this?
Second currency
Create an array of currency pairs:
array (size=3)
0 =>
array (size=2)
0 => string 'EUR' (length=3)
1 => string 'USD' (length=3)
1 =>
array (size=2)
0 => string 'USD' (length=3)
1 => string 'EUR' (length=3)
2 =>
array (size=2)
0 => string 'SEk' (length=3)
1 => string 'CAD' (length=3)
Reverse order of currencies pairs in above array and put them into a non mutlidimensional array:
array (size=3)
0 => string 'USD-EUR' (length=7)
1 => string 'EUR-USD' (length=7)
2 => string 'CAD-SEk' (length=7)
Sort the array (with sort() ) and glue that array into a new string
string 'CAD-SEk,EUR-USD,USD-EUR' (length=23)
Make an array of currency pairs from the newly created string:
array (size=3)
0 =>
array (size=2)
0 => string 'CAD' (length=3)
1 => string 'SEk' (length=3)
1 =>
array (size=2)
0 => string 'EUR' (length=3)
1 => string 'USD' (length=3)
2 =>
array (size=2)
0 => string 'USD' (length=3)
1 => string 'EUR' (length=3)
Reverse order of currencies pairs in above array and put them into a non mutlidimensional array:
array (size=3)
0 => string 'SEk-CAD' (length=7)
1 => string 'USD-EUR' (length=7)
2 => string 'EUR-USD' (length=7)
Glue the array together into a final string:
string 'SEk-CAD,USD-EUR,EUR-USD' (length=23)
Try this:
$currency_string = "EUR-USD,USD-EUR,SEk-CAD";
$currency_array = explode(",", $currency_string);
function compare($a, $b) {
$a = explode("-", $a);
$b = explode("-", $b);
if ($a[1] === $b[1]){
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
usort($currency_array, "compare");
$final_currency_string = implode(",", $currency_array);
echo $final_currency_string; // Prints SEk-CAD,USD-EUR,EUR-USD
Hope this helps.

Split Array at Repeating Object in Array

I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)

php read object of array in array

$values = (object)$arr;
var_dump($values);
var_dump produces these:
object(stdClass)[1]
public 'time' => float 0.002
public 'distance' => float 0.156
public 'code' => string '1' (length=1)
public 'result' =>
array (size=17)
0 => string '2.94053, 101.787, A' (length=24)
1 => string '2.94043, 101.787, A' (length=24)
2 => string '2.9404, 101.787, A' (length=23)
3 => string '2.94029, 101.787, A' (length=24)
4 => string '2.94025, 101.787, A' (length=24)
5 => string '2.9402, 101.787, A' (length=23)
6 => string '2.94016, 101.787, A' (length=24)
7 => string '2.94007, 101.787, A' (length=24)
public 'arrayPosition' =>
array (size=1)
0 =>
array (size=1720)
0 => string '2.93955, 101.788, B' (length=22)
1 => string '2.93951, 101.788, B' (length=22)
2 => string '2.93926, 101.788, B' (length=22)
3 => string '2.93921, 101.788, B' (length=22)
4 => string '2.9392, 101.788, B' (length=21)
5 => string '2.93911, 101.788, B' (length=22)
6 => string '2.93906, 101.789, B' (length=22)
7 => string '2.93896, 101.789, B' (length=22)
How can I read each of the values of result and arrayPosition ?
By using this
echo "<br>".$values->time."<br>";
echo $values->distance."<br>";
echo $values->code."<br>";
echo $values->result."<br>";
echo $values->arrayPosition."<br>";
will fail on result and arrayPosition
Notice: Array to string conversion
to access arrayPosition there are couple of ways if you know the sub index of the element you want you can access it by
$values->arrayPosition[0][index_number]; //you may require the first dimension index if you have more than one element there as well
The other method is using a loop
foreach ($values->arrayPosition as $levelone) {
foreach ($levelone as $key => $leveltwo) {
echo $leveltwo;
}
}
foreach($values as $value)
{
foreach($value->result as $result1)
{
echo($result1);
echo("<br>");
}
foreach($value->arrayPosition as $arrayposition2)
{
foreach($arrayposition2 as $arrayposition_child)
{
echo($arrayposition_child);
echo("<br>");
}
}
}
for accessing those 2 variables, you can:
1)
foreach( $values->result as $row ) {
echo $row;
}
2)
foreach( $values->arrayPosition as $mulRow ) {
foreach( $mulRow as $row ) {
echo $row;
}
}

Categories