I've two array. $arr1 is like this:-
Array
(
[Size] => L
[Color] => Purple
[Brand] => Lee
[Fabric] => Cotton
)
and another array $arr2 is like this:-
Array
(
[0] => Array
(
[Color] => Purple
[Size] => L
[Brand] => Lee
[Fabric] => Cotton
[Price] => 1000
[Quantity] => 5
)
[1] => Array
(
[Color] => Pink
[Size] => L
[Brand] => Lee
[Fabric] => Cotton
[Price] => 1100
[Quantity] => 5
)
[2] => Array
(
[Color] => White
[Size] => L
[Brand] => Lee
[Fabric] => Cotton
[Price] => 1200
[Quantity] => 5
)
[3] => Array
(
[Color] => Black
[Size] => L
[Brand] => Lee
[Fabric] => Cotton
[Price] => 900
[Quantity] => 5
)
[4] => Array
(
[Color] => Purple
[Size] => M
[Brand] => Lee
[Fabric] => Cotton
[Price] => 900
[Quantity] => 5
)
[5] => Array
(
[Color] => Purple
[Size] => S
[Brand] => Lee
[Fabric] => Cotton
[Price] => 900
[Quantity] => 5
)
[6] => Array
(
[Color] => Pink
[Size] => M
[Brand] => Lee
[Fabric] => Cotton
[Price] => 900
[Quantity] => 5
)
[7] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Lee
[Fabric] => Cotton
[Price] => 900
[Quantity] => 5
)
)
I want to get the price and quantity of $arr2 if both array matches. For example in this case $arr1 is having 4 arguments same as $arr2's 0th index values. So the resulted array should return 1000 and 5. I've tried array_diff and some other function but it doesn't helped me. Any help will be appreciated. Thanks in advance.
foreach($arr2 as $arrayIndex=>$element){
$match = true;
foreach($arr1 as $key=>$elementToMatch){
if($element[$key] != $elementToMatch ){
$match = false;
}
if($match == false) {
break;
}
}
if($match) {
return $arr2[$arrayIndex];
}
}
I would suggest something like this. It's a bit ugly becouse there is loop in a loop, but it would be easiest way to achive this.
Assuming the price and quantity is always the last, a simple approach would be this
foreach($arr2 as $arr) {
if(count(array_diff(array_slice($arr,0,4),$arr1))==0) {
return $arr;
}
}
Your
Array 1:-
$Arr1=array(array
(
"Color" => "Pink",
"Size" => "L",
"Brand" => "Lee",
"Fabric" => "Cotton",
"Price" => "1100",
"Quantity" => "5"
),array(
"Color" => "White",
"Size" => "L",
"Brand" => "Lee",
"Fabric" => "Cotton",
"Price" => "1200",
"Quantity" => "5"
),
array(
"Color" => "Black",
"Size" => "L",
"Brand" => "Lee",
"Fabric" => "Cotton",
"Price" => "1300",
"Quantity" => "5"
),
array(
"Color" => "RED",
"Size" => "L",
"Brand" => "Lee",
"Fabric" => "Cotton",
"Price" => "1100",
"Quantity" => "5"
)
);
Array 2:-
$Arr2=array
(
"Size" => "L",
"Color" => "Purple",
"Brand" => "Lee",
"Fabric" => "Cotton"
);
for($x=0;$x<sizeof($Arr1);$x++){
for($j=$x+1;$j<sizeof($Arr1);$j++){
if($Arr1[$x]["Price"]==$Arr1[$j]["Price"] && $Arr1[$x]["Quantity"]==$Arr1[$j]["Quantity"]){
$Arr2["Price"]=$Arr1[$j]["Price"];
$Arr2["Quantity"]=$Arr1[$j]["Quantity"];
}
}
}
var_dump($Arr2);
Your Output will be
'Size' => string 'L' (length=1)
'Color' => string 'Purple' (length=6)
'Brand' => string 'Lee' (length=3)
'Fabric' => string 'Cotton' (length=6)
'Price' => string '1100' (length=4)
'Quantity' => string '5' (length=1)
Assuming $arr1 is one dimensional and $arr2 is 2 dimensional, you can use following where result contains array of price and quantity for matched arrays.`
function match_array($arr1, $arr2, $result){
foreach($arr2 as $k=>$v){
// calculate diff between array1 and array2[key]
// if size of diff is 0 array matches
if(sizeof(array_diff($arr1, $v)) == 0){
// calculate the diff of array2[key] and array1
// this will give us the price and quantity
// push the diff into the result array
array_push($result, array_diff($v, $arr1));
}
}
print_r($result);
}
$result = array();
$result = match_array($array1, $array2, $result);
print_r($result);
Related
I want to remove the first.. how to say that outside of the array and left inside array of a multidimension array. I couldn't found out the solution.. someone knows how to do it??
This is my code. I assign a array and i try to put into another array like this.
$final_sku = array();
foreach($skus as $sku){
foreach($sku as $key => $s){
$final_sku[] = array('Sku' => $s);
}
}
$newArray = array(
"Product" => array(
"PrimaryCategory" => "1",
"AssociatedSku" => "12",
"Attributes" => array(
),
"Skus" => $final_sku
)
);
And this is the $final_sku output be like.
[0] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
)
[1] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
)
I want the output be like this.
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
UPDATE: I want to pass the array and convert to xml. So the array key would be duplicated.
It does not make sense to have the same array of 2 members under the same key.
If you want to access one of them, what will be its uniqueness over the other?
The logical solution is
[sku] => [
0 => [first sku data....],
1 => [sku data....],
]
I need to create a set of arrays that look like this:
Array ([ID] => 55 [status] => u [resvdate] => 07/16/2018 [price] => 119.00 [source] => C)
Array ([ID] => 56 [status] => u [resvdate] => 07/17/2018 [price] => 119.00 [source] => C)
Array ([ID] => 57 [status] => u [resvdate] => 07/18/2018 [price] => 119.00 [source] => C)
from five arrays that look like this:
Array ( [resvdate1] => 07/16/2018 [resvdate2] => 07/17/2018 [resvdate3] => 07/18/2018 )
Array ( [resvdateid1] => 55 [resvdateid2] => 56 [resvdateid3] => 57 )
Array ( [resvprice1] => 119.00 [resvprice2] => 119.00 [resvprice3] => 119.00 )
Array ( [pricesource1] => C [pricesource2] => C [pricesource3] => C )
Array ( [rowstatus1] => u [rowstatus2] => u [rowstatus3] => u )
Do I just need to loop through each array and pick off the values or is there a more elegant way to do this?
Here's a go at it:
$data = [
[
"resvdate1" => "07/16/2018",
"resvdate2" => "07/17/2018",
"resvdate3" => "07/18/2018"
],
[
"resvdateid1" => 55,
"resvdateid2" => 56,
"resvdateid3" => 57
],
[
"resvprice1" => "119.00",
"resvprice2" => "119.00",
"resvprice3" => "119.00"
],
[
"pricesource1" => "C",
"pricesource2" => "C",
"pricesource3" => "C"
],
[
"rowstatus1" => "u",
"rowstatus2" => "u",
"rowstatus3" => "u"
]
];
$keys = [
"resvdate" => "resvdate",
"resvdateid" => "id",
"resvprice" => "price",
"pricesource" => "source",
"rowstatus" => "status"
];
$result = [];
foreach ($data as $a) {
$i = 0;
foreach ($a as $k => $v) {
$result[$i++][$keys[preg_replace("/\d/", "", $k)]] = $v;
}
}
print_r($result);
Output
Array
(
[0] => Array
(
[resvdate] => 07/16/2018
[id] => 55
[price] => 119.00
[source] => C
[status] => u
)
[1] => Array
(
[resvdate] => 07/17/2018
[id] => 56
[price] => 119.00
[source] => C
[status] => u
)
[2] => Array
(
[resvdate] => 07/18/2018
[id] => 57
[price] => 119.00
[source] => C
[status] => u
)
)
Explanation
This is basically a column to row mapping involving a little key adjustment along the way.
This is my array,
array
(
[0] => Array
(
[Invoice_id] => 1
[Customer_Name] => Abcd Ltd
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 212.5
[SGST] => 212.5
[IGST] => 0
[Total_Amount] => 8925
)
[1] => Array
(
[Invoice_id] => 2
[Customer_Name] => Johnson and Sons
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 2975
[SGST] => 2975
[IGST] => 0
[Total_Amount] => 124950
)
)
How to convert this array like below,
array
(
array("invoice_id" => "1", "customer_name" => "Abcd Ltd", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>212.5, "sgst" =>212.5, "igst" =>0, "total_amount" =>8925),
array("invoice_id" => "2", "customer_name" => "Johnson and Sons", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>2975, "sgst" =>2975, "igst" =>512.5, "total_amount" =>124950)
);
You already have it in the format you want, they are just in different ways of displaying arrays, except that your arrays are indexed by keys, if you really want to unindex it use this $newArray = array_values($array)
You could try this to change keys of your array :
foreach ($array as $k => $item) {
foreach ($item as $key => $value) {
unset($array[$k][$key]) ; // remove old key
$array[$k][strtolower($key)] = $value ; // add new one
}
}
Then "Invoice_id" keys will be "invoice_id", and so on.
I want to merge two array's using key(product_id) and adding that values(usage).
Array 1
Array
(
[0] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
[1] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 1
[product_id] => 14
)
)
Array 2
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 2
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
)
I need an out put like this merging and adding usage values.
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 2
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 3
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
[5] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
)
This is my code for creating arrays
foreach($query->rows as $product){
$top_products[]=array(
'name'=>$product['name'],
'usage'=>$product['pusage'],
'product_id'=>$product['product_id']
);
}
foreach($query_2->rows as $product){
$top_point_products[]=array(
'name'=>$product['name'],
'usage'=>$product['p_usage'],
'product_id'=>$product['product_id']
);
}
$first =array(
array(
"name" => "Reschedule A Service",
"usage" => 1,
"product_id" => 8
),
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Each Calendar Event",
"usage" => 1,
"product_id" => 14
)
);
$second =array(
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Schedule A Service",
"usage" => 3,
"product_id" => 11
),
array(
"name" => "Each Calendar Event",
"usage" => 2,
"product_id" => 14
),
array(
"name" => "Sales Performance Dashboard",
"usage" => 2,
"product_id" => 30
),
array(
"name" => "Quote",
"usage" => 1,
"product_id" => 32
)
);
$result = array_unique(array_merge($first,$second), SORT_REGULAR);
Use array_unique & array_merge
Use the array_merge function, like this:
$C = array_merge($A, $B);
print_r($C);
Read manual Array merge
try this code
<?php
$array1=array
(
0 => array(
'name' => "Reschedule A Service",
'usage' => 1,
'product_id' => 8
),
1 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 2,
'product_id' => 14
)
);
$array2=array
(
0 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
1 => Array
(
'name' => "Schedule A Service",
'usage' => 3,
'product_id' => 11
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 5,
'product_id' => 14
),
3 => Array
(
'name' => "Sales Performance Dashboard",
'usage' => 2,
'product_id' => 30
),
4 => Array
(
'name' => "Quote",
'usage' => 1,
'product_id' => 32
)
);
$product_id1=array_column($array1, 'product_id');
$product_id2=array_column($array2, 'product_id');
$new=array_intersect($product_id1,$product_id2);
foreach ($new as $key => $value) {
if(in_array($new[$key],$product_id2)){
$array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
}
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
$array2[]=$array1[$key];
}
foreach ($array2 as $key => $value) {
echo "[".$key."]=><br>";
foreach ($value as $key1 => $value1) {
echo "      ";
echo "[".$key1."]=>".$value1."<br>";
}
echo "<br>";
}
?>
output
[0]=>
[name]=>Adding An Image
[usage]=>2
[product_id]=>5
[1]=>
[name]=>Schedule A Service
[usage]=>3
[product_id]=>11
[2]=>
[name]=>Each Calendar Event
[usage]=>7
[product_id]=>14
[3]=>
[name]=>Sales Performance Dashboard
[usage]=>2
[product_id]=>30
[4]=>
[name]=>Quote
[usage]=>1
[product_id]=>32
[5]=>
[name]=>Reschedule A Service
[usage]=>1
[product_id]=>8
Use array_merge and a simple foreach loop to check your condition and update the usagevalues.
See below
$result = array_merge($arrArray1, $arrArray2);
$result2 = array();
foreach($result as $key => $value){
if(array_key_exists($value['product_id'], $result2)){
$result2[$value['product_id']]['usage'] += $value['usage'];
} else{
$result2[$value['product_id']] = $value;
}
}
print_r($result2);
If you want to reset your resultant array indexes use array_merge again like this
$result2 = array_merge($result2);
Hope this will help
I am trying to get the example below to have key names for each dimension so it would look more like this:
Array
(
[Mobile] => Array
(
[0] => Array
(
[product_id] => 007
[product_name] => Blackberry R-900 Mobile
[product_price] => £450
[product_status] => 1
[product_category] => Mobile
)
Instead of the example below,
I have included the class that I am using to create the array into a categorized array and the usage of it.
The function appendNode in array2arraytree is where the algorithm actually creates the new array for each category.
Thanks for any help.
Example:
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 007
[product_name] => Blackberry R-900 Mobile
[product_price] => £450
[product_status] => 1
[product_category] => Mobile
)
[1] => Array
(
[product_id] => 001
[product_name] => Wespro Multi-SIM & Touch-screen Mobile
[product_price] => £150
[product_status] => 1
[product_category] => Mobile
)
[2] => Array
(
[product_id] => 004
[product_name] => Sigmatel MP4/MP3 + Camera Mobile
[product_price] => £150
[product_status] => 1
[product_category] => Mobile
)
)
[1] => Array
(
[0] => Array
(
[product_id] => 033
[product_name] => 8 GB Pendrive
[product_price] => £14.99
[product_status] => 0
[product_category] => Computers
)
[1] => Array
(
[product_id] => 334
[product_name] => 250 GB Portable Hard Drive
[product_price] => £79.99
[product_status] => 1
[product_category] => Computers
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 033
[product_name] => The White Tiger – Aravind Adiga
[product_price] => £29.99
[product_status] => 1
[product_category] => Books
)
[1] => Array
(
[product_id] => 4501
[product_name] => The Final Reckoning - Sam Bourne
[product_price] => £19.99
[product_status] => 0
[product_category] => Books
)
[2] => Array
(
[product_id] => 034
[product_name] => The Final Reckoning - Sam Bourne
[product_price] => £15.79
[product_status] => 0
[product_category] => Books
)
)
)
Usage:
<?php
require_once("array2arraytree.php");
$arrProducts=array(
array(
"product_id" => "007",
"product_name" => "Blackberry R-900 Mobile",
"product_price" => "£450",
"product_status" =>"1",
"product_category" =>"Mobile"
),
array(
"product_id" => "033",
"product_name" => "8 GB Pendrive",
"product_price" => "£14.99",
"product_status" => "0",
"product_category" => "Computers"
),
array(
"product_id" => "033",
"product_name" => "The White Tiger – Aravind Adiga",
"product_price" => "£29.99",
"product_status" => "1",
"product_category" => "Books"
),
array(
"product_id" => "4501",
"product_name" => "The Final Reckoning - Sam Bourne",
"product_price" => "£19.99",
"product_status" => "0",
"product_category" => "Books"
),
array(
"product_id" => "001",
"product_name" => "Wespro Multi-SIM & Touch-screen Mobile",
"product_price" => "£150",
"product_status" => "1",
"product_category" => "Mobile"
),
array(
"product_id" => "004",
"product_name" => "Sigmatel MP4/MP3 + Camera Mobile",
"product_price" => "£150",
"product_status" => "1",
"product_category" => "Mobile"
),
array(
"product_id" => "034",
"product_name" => "The Final Reckoning - Sam Bourne",
"product_price" => "£15.79",
"product_status" => "0",
"product_category" => "Books"
),
array(
"product_id" => "334",
"product_name" => "250 GB Portable Hard Drive",
"product_price" => "£79.99",
"product_status" => "1",
"product_category" => "Computers"
)
);
$objTree=new Array2ArrayTree($arrProducts,"product_category");
$arrTree=$objTree->makeTree();
print("<pre>");
print_r($arrTree);
print("</pre>");
?>
Class Array2ArrayTree:
class Array2ArrayTree
{
public $arrOriginal = array();
public $arrDummy = array();
public $strKey = "";
public function __construct($arrData, $arrKey)
{
$this->arrOriginal = $arrData;
$this->strKey = $arrKey;
$this->arrDummy = array();
}
public function makeTree()
{
for ($i = 0; $i <= sizeof($this->arrOriginal) - 1; $i++) {
$keyPosition = $this->searchKey($this->arrOriginal[$i][$this->strKey]);
if ($keyPosition == -1) {
$this->addNode($this->arrOriginal[$i]);
} else {
$this->appendNode($this->arrOriginal[$i], $keyPosition);
}
}
return $this->arrDummy;
}
function searchKey($strCurrentValue)
{
for ($i = 0; $i <= sizeof($this->arrDummy) - 1; $i++) {
if ($this->arrDummy[$i][0][$this->strKey] == $strCurrentValue) {
return $i;
}
}
return - 1;
}
function addNode($arrNode)
{
$this->arrDummy[sizeof($this->arrDummy)][0] = $arrNode;
}
function appendNode($arrNode, $keyPosition)
{
array_push($this->arrDummy[$keyPosition], $arrNode);
}
}
?>
Should be even simpler:
public function makeTree()
{
foreach($this->arrOriginal as $value) {
$this->arrDummy[$value[$this->strKey]][] = $value;
}
return $this->arrDummy;
}
I think this would work - map the array to get the keys you want, then use array_filter to get only the items in that category:
//get the keys you want:
$keys = array_unique(array_map(function($item) { return $item['product_category']; }, $array));
$return = [];
foreach($keys as $key) {
//filter the array to the one's in the current category, and reindex them to 0:
$return[$key] = array_values(array_filter($array, function($item) use ($key) {
return $item['product_category'] == $key;
}));
}