get user_id from variable using php - php

I have this variable $products that I'm getting from the db. Now when I print what's in it it shows
Array ( [0] => Array ( [quantity] => 1 [product_id] => 5363 [category_id] => 209 [price] => 1 [tax] => 18.00 [tax_id] => 1 [description] => [product_name] => test2 [thumb_image] => [ean] => [attributes] => a:0:{} [attributes_value] => Array ( ) [weight] => 0.0000 [vendor_id] => 0 [files] => a:0:{} [freeattributes] => a:0:{} [dependent_attr_serrialize] => a:0:{} [href] => /kategorii/product/view/209/5363.html [free_attributes_value] => Array ( ) ) )
So my question is how can i get what is the product_id number?
I print this using print_r($products); but is gives me this error
Warning: current() [function.current]: Passed variable is not an array or object in ...
This is the code that is causing this problem:
$cart = JModel::getInstance('cart', 'jshop');
$cart->load();
$products=$cart->products;
//$productos[0]['product_id'];
print_r($products);

Assuming the variable you did a print_r of above is assigned to $products:
$products[0]['product_id']

Related

PHP JSON array accessing

I have a PHP variable, let's say named $products
when I try to print the first element in that variable by printing $products[0], the array print the word (array) on the screen.
how I can access each element in this array? thank you
Array(
[0] => Array
(
[id] => 2356
[title] => REAR BRAKE
[price] => $0
[image] => 2356_BRS-10228-1.jpg
[quantity] => 1
)
[1] => Array
(
[id] => 2358
[title] => REAR BRAKE MITSUBITSHI
[price] => $0
[image] => 2358_BRS-10230-1.jpg
[quantity] => 1
)
[2] => Array
(
[id] => 2360
[title] => Axela CX-4
[price] => $0
[image] => 2360_BRP-10639-1.jpg
[quantity] => 1
)
)
you can use foreach loop :
foreach($data as $key =>$value){
echo $value['id'].'<br>';
}
result :
2356
2358
2360

Amazon MWS: Accessing Array of Matching Products

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');

How to parse elasticsearch response in php

I try to parse my elasticsearch response in php.
When i print my json resultat with a var_dump, i've got that:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
But, when i try to list or to find a title by a foreach:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
that doesn't work, because i obtain this error:
Message: Invalid argument supplied for foreach()
I don't see why.
Thanks in advnce.
In foreach you are trying to use "$myobj".
But decoded json is in "$myData".

How to set value in array of array index

Array
(
[a31df20a1e75a9200fc0d8b136438c91] => Array
(
[addons] => Array
(
[0] => Array
(
[name] => Add Message here, First 5 Words Free
[value] => tyyty tytyt ty
[price] => 10
)
)
[product_id] => 1356
[variation_id] =>
[variation] =>
[quantity] => 1
[line_total] => 110
[line_tax] => 0
[line_subtotal] => 110
[line_subtotal_tax] => 0
)
)
how can i set value of "[line_total] => 110"?
$thearray['a31df20a1e75a9200fc0d8b136438c91']['line_total'] = 110;
You know the path to the value, so you can just set it..
$array['a31df20a1e75a9200fc0d8b136438c91']['line_total'] = 12345;

How to pull out data from session in PHP?

I have the following data from print_r($_SESSION):
Array (
[totalprice] => 954
[cart] => Array (
[115] => Array (
[name] => MÃ…NESKINN
[price] => 268.00
[count] => 1 )
[80] => Array (
[name] => DELFINLEK
[price] => 268.00
[count] => 1 )
[68] => Array (
[name] => OPPDAGELSEN
[price] => 418.00
[count] => 1 )
)
[shipping] => 65 )
Now I want to pull out all the price 268.00 and 418.00 from this session.
How can I do it?
I tried $_SESSION['cart']['price'], but it does not work.
$_SESSION['cart'] is an array. If you need only one row - it would be
$_SESSION['cart'][115]['price']
But you'd better walk through array:
foreach($_SESSION['cart'] as $item){
echo $item['price'];
}

Categories