Sorry about such an easy question but I am new to PHP.
I am trying to add the isInStock keys and values from this array:
$stock array:
Array
(
[0] => Array
(
[name] => name of item 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://images1
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[isInStock] => 1
)
[1] => Array
(
[variantId] => 7986070
[isInStock] => 1
)
[2] => Array
(
[variantId] => 7985916
[isInStock] => 1
)
[3] => Array
(
[variantId] => 7985929
[isInStock] => 1
)
[4] => Array
(
[variantId] => 7985918
[isInStock] => 1
)
[5] => Array
(
[variantId] => 7985935
[isInStock] => 1
)
[6] => Array
(
[variantId] => 7985945
[isInStock] => 1
)
[7] => Array
(
[variantId] => 7985994
[isInStock] => 1
)
)
[productId] => 7985894
)
[1] => Array
(
[name] => name of item 2
[price] => 45.00
[colour] => Multi
[image] => http://images
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[isInStock] => 1
)
[1] => Array
(
[variantId] => 8041115
[isInStock] => 1
)
[2] => Array
(
[variantId] => 8040904
[isInStock] => 1
)
[3] => Array
(
[variantId] => 8041132
[isInStock] => 1
)
[4] => Array
(
[variantId] => 8041015
[isInStock] => 1
)
[5] => Array
(
[variantId] => 8040942
[isInStock] => 1
)
[6] => Array
(
[variantId] => 8040954
[isInStock] => 1
)
[7] => Array
(
[variantId] => 8040990
[isInStock] => 1
)
)
[productId] => 8040851
)
and put them in this array under each size according to the variantId value if they both match.
$data array:
Array
(
[0] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://url
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[size] => US 0
)
[1] => Array
(
[variantId] => 7986070
[size] => US 2
)
[2] => Array
(
[variantId] => 7985916
[size] => US 4
)
[3] => Array
(
[variantId] => 7985929
[size] => US 6
)
[4] => Array
(
[variantId] => 7985918
[size] => US 8
)
[5] => Array
(
[variantId] => 7985935
[size] => US 10
)
[6] => Array
(
[variantId] => 7985945
[size] => US 12
)
[7] => Array
(
[variantId] => 7985994
[size] => US 14
)
)
)
[1] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Multi
[image] => http://url
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[size] => US 0
)
[1] => Array
(
[variantId] => 8041115
[size] => US 2
)
[2] => Array
(
[variantId] => 8040904
[size] => US 4
)
[3] => Array
(
[variantId] => 8041132
[size] => US 6
)
[4] => Array
(
[variantId] => 8041015
[size] => US 8
)
[5] => Array
(
[variantId] => 8040942
[size] => US 10
)
[6] => Array
(
[variantId] => 8040954
[size] => US 12
)
[7] => Array
(
[variantId] => 8040990
[size] => US 14
)
)
)
I have been trying to solve this for a while now, but I keep getting stuck. I can assign individual values but can't get it to do the whole array. Any help would be appreciated.
I would like my resulting array to be like this:
Array
(
[0] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://url
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[size] => US 0
[isInStock] => 1
)
[1] => Array
(
[variantId] => 7986070
[size] => US 2
[isInStock] => 1
)
[2] => Array
(
[variantId] => 7985916
[size] => US 4
[isInStock] => 1
)
[3] => Array
(
[variantId] => 7985929
[size] => US 6
[isInStock] => 1
)
[4] => Array
(
[variantId] => 7985918
[size] => US 8
[isInStock] => 1
)
[5] => Array
(
[variantId] => 7985935
[size] => US 10
[isInStock] => 1
)
[6] => Array
(
[variantId] => 7985945
[size] => US 12
[isInStock] => 1
)
[7] => Array
(
[variantId] => 7985994
[size] => US 14
[isInStock] => 1
)
)
)
[1] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Multi
[image] => http://url
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[size] => US 0
[isInStock] => 1
)
[1] => Array
(
[variantId] => 8041115
[size] => US 2
[isInStock] => 1
)
[2] => Array
(
[variantId] => 8040904
[size] => US 4
[isInStock] => 1
)
[3] => Array
(
[variantId] => 8041132
[size] => US 6
[isInStock] => 1
)
[4] => Array
(
[variantId] => 8041015
[size] => US 8
[isInStock] => 1
)
[5] => Array
(
[variantId] => 8040942
[size] => US 10
[isInStock] => 1
)
[6] => Array
(
[variantId] => 8040954
[size] => US 12
[isInStock] => 1
)
[7] => Array
(
[variantId] => 8040990
[size] => US 14
[isInStock] => 1
)
)
)
So far I have tried array_merge which doesn't put the values in the right place.
I have tried this $data['isInstock'] = $stock[0]['variants'][0]['variantId']; which also won't work.
Sadly, it takes a fair amount of iterating/preparation to get your two arrays ready for use with array_merge_recursive(). I'll admit, I'm not proud of the convolution in my method. The major factor in all this is that array_merge_recursive() only "plays nicely" with non-numeric indexes, so I had to replace your numerically indexed keys with relative id values within the arrays. I'll do my best to explain my steps, but again, it's not pretty... (Demo)
Step #1: Prepare $stock array:
foreach($stock as $subarray){
$new_stock["#{$subarray['productid']}"]=$subarray; // replace outer key
$new_variants=[]; // declare a fresh array
foreach($subarray['variants'] as $varsub){
$new_variants["#{$varsub['variantId']}"]['isInStock']=$varsub['isInStock']; // one element only
// omitting variantId element this time as the next array will offer it.
}
$new_stock["#{$subarray['productid']}"]['variants']=$new_variants;
}
Step #2: Prepare $data array & merge:
foreach($data as $subarray){
$new_data["#{$subarray['productid']}"]=$subarray; // replace outer key
$new_variants=[]; // declare a fresh array
foreach($subarray['variants'] as $varsub){
$new_variants["#{$varsub['variantId']}"]=$varsub; // both elements from variants
}
$new_data["#{$subarray['productid']}"]['variants']=array_values(array_merge_recursive($new_variants,$new_stock["#{$subarray['productid']}"]['variants']));
// new variants subarray has been merged, re-indexed, and written to $new_data
}
Step #3: re-index outer array keys, and display:
$result=array_values($new_data);
var_export($result);
The bulk of the array preparations is to generate unique id's for the outer and inner arrays (in both $stock & $data). This permits the array_merge to isolate the related productids and recursively merge the variant elements.
If these two arrays are being generated from a database, then my high recommendation is to utilize available database functionality to merge this data instead of php.
For a simple example of how array_merge_recursive() works here's a small demo. Experiment with the keys in either of the arrays. If you so-much-as remove the # from the numeric string, array_merge_recursive() will assume that it's dealing with numeric indexes and mince things up. My technique to preserve your id's as strings was to prepend the #, but it could have been done by adding any of a range of non-digit characters to the key value.
Related
From start I have a list of orders in the following way:
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
[category_id] => 12
)
What I want is to sum the values for total_weight and qty_ordered based on category_id
I have managed to get the array in the below manner now using a foreach. But I need to aggregate the values somehow.
Array
(
[14] => Array
(
[0] => Array
(
[product_id] => 1099
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[1] => Array
(
[product_id] => 1095
[total_weight] => 0.0395
[qty_ordered] => 10.0000
)
)
[12] => Array
(
[0] => Array
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
)
[15] => Array
(
[0] => Array
(
[product_id] => 742
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
)
So ideally I would the output to be like with below with as little code as possible:
Array
(
[14] => Array
(
[total_weight] => 0.0785
[qty_ordered] => 20.0000
)
[12] => Array
(
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[15] => Array
(
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
I was able to sort it in the below way.
foreach($retval as $val) {
$return[$val['category_id']]['qty_ordered'] += $val['qty_ordered'];
$return[$val['category_id']]['row_total'] += $val['row_total'];
}
I have an array, which I have created by importing a csv file with the parsecsv library.
Array (
[0] => Array ( [cubcode] => EC023 [qty] => 2 )
[1] => Array ( [cubcode] => EC026A [qty] => 3 )
[2] => Array ( [cubcode] => EC025 [qty] => 7 )
[3] => Array ( [cubcode] => EC027 [qty] => 67 )
[4] => Array ( [cubcode] => EC031 [qty] => 567 )
[5] => Array ( [cubcode] => EC033 [qty] => 78 )
[6] => Array ( [cubcode] => EC034 [qty] => 234 )
[7] => Array ( [cubcode] => EC038 [qty] => 67 )
[8] => Array ( [cubcode] => EC038A [qty] => 67 )
[9] => Array ( [cubcode] => EC039 [qty] => 60 )
[10] => Array ( [cubcode] => EC039A [qty] => 100 )
)
I need to loop through the array to create two variables "cubcode" and "qty" and use them in a function that I have already created.
I am struggling to get to them because the seem to be nested in the array. I have not done much work with arrays, so please be gentle with me.
Thanks.
Sorry for the code only answer, but you literally just need to write it in PHP, and not in English. Since $csv->data (from comments) is the name of the array:
foreach ($csv->data as $item) {
function_you_already_created($item['cubcode'], $item['qty']);
}
I am Working with MWS for the first time and hoping to create a program that uses the ListMatchingProducts request to average out the prices of every product that matches a query.
It should be a very simple program, but I am having trouble retrieving data.
First I make the call and get amazon's xml sheet, Then I convert the xml to an array.
Print_R shows that the array looks something like this:
Array ( [ListMatchingProductsResult] => Array ( [Products] => Array ( [Product] => Array ( [0] => Array ( [Identifiers] => Array ( [MarketplaceASIN] => Array ( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => 0786866020 ) ) [AttributeSets] => Array ( [ItemAttributes] => Array ( [Author] => Array ( [0] => Stephen C. Lundin [1] => Harry Paul [2] => John Christensen ) [Binding] => Hardcover [Brand] => Hyperion [Color] => White [Creator] => Ken Blanchard [Edition] => 1 [Feature] => Great product! [ItemDimensions] => Array ( [Height] => 8.25 [Length] => 5.50 [Width] => 0.00 [Weight] => 0.54 ) [IsAdultProduct] => false [Label] => Hyperion [Languages] => Array ( [Language] => Array ( [0] => Array ( [Name] => english [Type] => Published ) [1] => Array ( [Name] => english [Type] => Original Language ) [2] => Array ( [Name] => english [Type] => Unknown ) ) ) [ListPrice] => Array ( **[Amount] => 21.00** [CurrencyCode] => USD ) [Manufacturer] => Hyperion [ManufacturerMaximumAge] => 1188.0 [ManufacturerMinimumAge] => 156.0 [NumberOfItems] => 1 [NumberOfPages] => 110 [PackageDimensions] => Array ( [Height] => 0.65 [Length] => 8.60 [Width] => 5.65 [Weight] => 0.58 ) [PackageQuantity] => 1 [PartNumber] => 9780786866021 [ProductGroup] => Book [ProductTypeName] => ABIS_BOOK [PublicationDate] => 2000-03-08 [Publisher] => Hyperion [ReleaseDate] => 2000-03-08 [SmallImage] => Array ( [URL] => http://ecx.images-amazon.com/images/I/51cHo55tbOL._SL75_.jpg [Height] => 75 [Width] => 47 ) [Studio] => Hyperion [Title] => Fish: A Proven Way to Boost Morale and Improve Results ) ) [Relationships] => Array ( ) [SalesRankings] => Array ( [SalesRank] => Array ( [0] => Array ( [ProductCategoryId] => book_display_on_website [Rank] => 4629 ) [1] => Array ( [ProductCategoryId] => 1043856 [Rank] => 2 ) [2] => Array ( [ProductCategoryId] => 2635 [Rank] => 7 ) [3] => Array ( [ProductCategoryId] => 2637 [Rank] => 18 ) ) ) ) [1] ...
I am trying to access the amount part of the array, as this is the price of the object. Eventually, I will need to access the amount of each product and so a loop will likely come into play, but right now i cannot even access one products sales amount.
Here is the code I have been trying
$value = $array->ListMatchingProductsResult->Products->Product[0]->ListPrice->Amount;
print_r($value);
And it is not working. Even calling print_r on $array->ListMatchingProductsResult is not printing an array.
Any help is greatly appreciated!
Thanks,
Matt
You are almost there. Whatever method you are using to convert the XML to a PHP array however, does just that: It creates an associative array of things, not an object. That is why you cannot access it through the ->element operator, but need to use array indexes ['element']
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]['ListPrice']['Amount'];
If you're ever wondering why you're not getting something back, you can successily shorten above expresion until you do. So if a print_r(...) on above expression returns nothing, just remove one square bracket at a time from that print_r until you do get something back. You then know that the last bracket you removed was the culprit.
Array (
[ListMatchingProductsResult] => Array (
[Products] => Array (
[Product] => Array (
[0] => Array (
[Identifiers] => Array (
[MarketplaceASIN] => Array (
[MarketplaceId] => ATVPDKIKX0DER
[ASIN] => 0786866020
)
)
[AttributeSets] => Array (
[ItemAttributes] => Array (
[Author] => Array (
[0] => Stephen C. Lundin
[1] => Harry Paul
[2] => John Christensen
)
[Binding] => Hardcover
[Brand] => Hyperion
[Color] => White
[Creator] => Ken Blanchard
[Edition] => 1
[Feature] => Great product!
[ItemDimensions] => Array (
[Height] => 8.25
[Length] => 5.50
[Width] => 0.00
[Weight] => 0.54
)
[IsAdultProduct] => false
[Label] => Hyperion
[Languages] => Array (
[Language] => Array (
[0] => Array (
[Name] => english
[Type] => Published
)
[1] => Array (
[Name] => english
[Type] => Original Language
)
[2] => Array (
[Name] => english
[Type] => Unknown
)
)
)
[ListPrice] => Array (
[Amount] => 21.00
[CurrencyCode] => USD
)
[Manufacturer] => Hyperion
...
BTW, using <pre>...</pre> is helpful when trying to make sense of print_r() or var_dump().
P.S. You owe me a new space key.
Here is an sdk library that uses the ListMatchingProducts of Amazon MWS. You will be able to get the info in accessing info required.
https://github.com/choomz/amazon-mws-sdk/blob/master/search/src/MarketplaceWebServiceProducts/Samples/ListMatchingProductsSample.php
To show php errors have this set in your php.in
display_errors = On
You can also give this on top of the php script
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
This question already has answers here:
How do you remove an array element in a foreach loop?
(6 answers)
Closed 5 years ago.
This is a sample of my array:
Array
(
[productId] => 7740792
[productCode] => 1019534
[productPrice] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 0
[text] => £0.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
[variants] => Array
(
[0] => Array
(
[variantId] => 7740915
[sku] => 5784194
[isInStock] => 1
[isLowInStock] => 1
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
[1] => Array
(
[variantId] => 7740906
[sku] => 5784195
[isInStock] => 1
[isLowInStock] =>
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
I want to delete/unset "productPrice", "sku" and "price" from the whole array.
I have used this so far:
unset($alldata[0]['variants'][0]['price']);
unset($alldata[0]['variants'][1]['price']);
But the array changes and has thousands of entries so coding the unset like this would not be easy. I am new to PHP and have searched all I can and looked up the array functions for something suitable with no luck.
unset ($alldata['productPrice']);
foreach ($alldata['variants'] as $key => $value) {
unset (
$alldata['variants'][$key]['price'],
$alldata['variants'][$key]['sku']
);
}
I just started using moltin and learn to developing it.
In the getting started there is code that to show product
<?php
$product = \Product::Find(['slug' => 'baju']);
?>
documentation link https://moltin.com/getting-started/php
and the result is when I print array
<?php
print_r($product);
?>
It shows like this
Array ( [status] => 1 [result] => Array ( [0] => Array ( [id] => 1207658536885027482 [order] => [created_at] => 2016-03-17 03:08:58 [updated_at] => 2016-03-17 03:08:58 [sku] => baju-1 [title] => baju [slug] => baju [sale_price] => 0 [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [category] => Array ( [value] => Uncategorized [data] => Array ( [1134518259857490806] => Array ( [id] => 1134518259857490806 [order] => [created_at] => 2015-12-07 05:12:15 [updated_at] => 2015-12-07 05:12:15 [parent] => [slug] => uncategorized [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [title] => Uncategorized [description] => Products that do not fit into another category ) ) ) [stock_level] => 10 [stock_status] => Array ( [value] => In Stock [data] => Array ( [key] => 1 [value] => In Stock ) ) [description] => baju [requires_shipping] => Array ( [value] => Yes [data] => Array ( [key] => 1 [value] => Yes ) ) [weight] => 0 [height] => 0 [width] => 0 [depth] => 0 [catalog_only] => Array ( [value] => No [data] => Array ( [key] => 0 [value] => No ) ) [tax_band] => Array ( [value] => Default [data] => Array ( [id] => 1134518260142703561 [title] => Default [description] => [rate] => 20 [created_at] => [updated_at] => ) ) [collection] => [brand] => [price] => Array ( [value] => £1.20 [data] => Array ( [formatted] => Array ( [with_tax] => £1.20 [without_tax] => £1.00 [tax] => £0.20 ) [rounded] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) [raw] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) ) ) [is_variation] => [modifiers] => Array ( ) [images] => Array ( ) ) ) [pagination] => Array ( [total] => 1 [current] => 1 [limit] => 10 [offset] => 0 [from] => 1 [to] => 1 [offsets] => Array ( [first] => [previous] => [next] => [last] => ) [links] => Array ( [first] => [previous] => [next] => [last] => ) ) ) status = 1
How to get specific array from this list?
I already try
<?php
echo $product[0]['id'];
?>
it didn't work, show error Notice: Undefined offset: 0
you have a multidimensional array under the result key
access it the following way:
$product['result'][0]['id']
Give this a try:
echo $product['result'][0]['id'];