Here i having one multidimensional using this array productId wise i am making one more array,from this array i having Galery array, from this i have to take first prodcutImage value, i don't how can achive this, if any one update my asnwer
I having array like this
print_r($productByCode)
Array
(
[0] => Array
(
[productId] => 5
[adminproductId] => 1Delivery00123
[categoryName] => Computer Accessories
[brandName] => Dell
[title] => Dell Inspiron 5000 Series 5547 5555 5557 5558 Internal Laptop Keyboard (Black)
[subTitle] => Dell Keyboard
[price] => 200
[discountType] => Percentage
[discountValue] => 20
[afterDiscount] => 160
[shipppingAmount] => 150
[taxPercentage] => 25
[Galery] => Array
(
[0] => Array
(
[gId] => 13
[productId] => 5
[prodcutImage] => 1.jpeg
)
[1] => Array
(
[gId] => 14
[productId] => 5
[prodcutImage] => 2.jpeg
)
[2] => Array
(
[gId] => 15
[productId] => 5
[prodcutImage] => 3.jpeg
)
)
)
)
I have tried like this
$itemArray = array
(
$productByCode[0]["productId"]=>array
(
'categoryName'=>$productByCode[0]["categoryName"],
'productId'=>$productByCode[0]["productId"],
'brandName'=>$productByCode[0]["brandName"],
'subTitle'=>$productByCode[0]["subTitle"],
'quantity'=>$Qty_total,
'price'=>$productByCode[0]["afterDiscount"],
'taxPercentage'=>$productByCode[0]["taxPercentage"].'%',
'shipppingAmount'=>$productByCode[0]["shipppingAmount"],
'qtyprice'=>round($productByCode[0]["afterDiscount"])
)
);
Current Output
Array
(
[5] => Array
(
[categoryName] => Computer Accessories
[productId] => 5
[brandName] => Dell
[subTitle] => Dell Keyboard
[quantity] => 1
[price] => 160
[taxPercentage] => 25%
[shipppingAmount] => 150
[qtyprice] => 160
)
)
Expected Output
Array
(
[5] => Array
(
[categoryName] => Computer Accessories
[productId] => 5
[brandName] => Dell
[subTitle] => Dell Keyboard
[quantity] => 1
[price] => 160
[taxPercentage] => 25%
[shipppingAmount] => 150
[qtyprice] => 160
[prodcutImage] => 1.jpeg
)
)
If I understood you requirement, You've already tried like this way to assign values to different array element, So just grab the Galery value like this using nested array element accessing way & add this element at the last of your array.
'prodcutImage' => $productByCode[0]['Galery'][0]['prodcutImage']
So it will like this,
$itemArray = array
(
$productByCode[0]["productId"]=>array
(
'categoryName'=>$productByCode[0]["categoryName"],
'productId'=>$productByCode[0]["productId"],
'brandName'=>$productByCode[0]["brandName"],
'subTitle'=>$productByCode[0]["subTitle"],
'quantity'=>$Qty_total,
'price'=>$productByCode[0]["afterDiscount"],
'taxPercentage'=>$productByCode[0]["taxPercentage"].'%',
'shipppingAmount'=>$productByCode[0]["shipppingAmount"],
'qtyprice'=>round($productByCode[0]["afterDiscount"]),
'prodcutImage' => $productByCode[0]['Galery'][0]['prodcutImage']
)
);
Related
My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}
I have array like this:
Array
(
[0] => stdClass Object
(
[model_id] => 1
[category_id] => 1
[model_name] => Xperia
[category_name] => Mobile
[category_image] => 81649e37620644bec47244fda5233363.jpg
[item_type] => 1
)
[1] => stdClass Object
(
[model_id] => 3
[category_id] => 1
[model_name] => HTC One
[category_name] => Mobile
[category_image] => 250a3e4655454feaec8e2537d149846a.jpg
[item_type] => 1
)
[2] => stdClass Object
(
[model_id] => 9
[category_id] => 3
[model_name] => Apple TV
[category_name] => Televisions
[category_image] => no_image.jpg
[item_type] => 1
)
[3] => stdClass Object
(
[model_id] => 4
[category_id] => 2
[model_name] => MacBook
[category_name] => Laptop
[category_image] => c9890535d26bd06189fc22940a5ee5da.jpg
[item_type] => 1
)
[4] => stdClass Object
(
[model_id] => 2
[category_id] => 1
[model_name] => iPhone 7
[category_name] => Mobile
[category_image] => cab0631b071d0562412ccab2279d391e.jpg
[item_type] => 1
)
[5] => stdClass Object
(
[model_id] => 5
[category_id] => 3
[model_name] => Samsung TV
[category_name] => Televisions
[category_image] => e60bd4822e516a93f1e99a8b456b70ce.jpg
[item_type] => 1
)
)
How do I remove elements where category_id as well as item_type are repeated ? With the code below, I could only remove elements where any single value is repeating.
foreach($all_device_categories as $arr){
if(!isset($result[$arr->category_id])){
$result[$arr->category_id] = $arr;
}
}
Update: category_id as well as item_type should not be repeated at the same time. Its fine if category_id repeats but not item_type or vice versa.
As you need to store only one value if both category_id and item_type repeats then you need to concat them using some other characters like: -. If you don't do it then some information will be missed out.
foreach($all_device_categories as $arr){
$result[$arr->category_id.'-'.$arr->item_type] = $arr;
}
I think this will help you, This will always takes the update array values if any match found. And if you don't want to take the updates value then simply you need to apply a condition before storing new array.
foreach($all_device_categories as $arr){
if(!isset($result[$arr->category_id.'-'.$arr->item_type])){
$result[$arr->category_id.'-'.$arr->item_type] = $arr;
}
}
The second example will store only the first match, if the repeats comes then it will avoid.
For multiple key match. You can combine those keys into array key. Try this:
foreach($all_device_categories as $arr){
if(!isset($result[$arr->category_id.$arr->item_type])){
$result[$arr->category_id.$arr->item_type] = $arr;
}
}
I have the below array containing arrays and arrays. This is just one entry; imagine now that I have 20 sources and each source contains at least 10 entries like the one below. This is a nightmare I want to solve.
Can anyone advice on how I can parse this efficiently in PHP and be able to get each of the values to be able to insert them into a DB for further processing!! Thank you.
Array (
[id] => id
[direction] => ltr
[updated] => 1407220315278
[title] => Syria | The Guardian
[alternate] => Array ( [0] => Array (
[href] => href
[type] => text/html ) )
[continuation] => 147a4ddf48e:28d98:bda086f
[items] => Array ( [0] => Array (
[keywords] => Array (
[0] => Global development
[1] => Refugees
[2] => Syria
[3] => Middle East and North Africa
[4] => World news
[5] => UK news
[6] => Scotland
[7] => Conflict and development
[8] => Immigration and asylum )
[fingerprint] => 47075b4f
[originId] => originId
[id] => Te6w3H2VbpKKFda+uYdqytnutphL/kktv0RL7gq9jjU=_147a4ddf48e:28d98:bda086f
[title] => A Syrian refugee in Scotland: 'I'm one of the lucky ones' video
[published] => 1407218400000
[crawled] => 1407220315278
[alternate] => Array (
[0] => Array (
[href] => href
[type] => text/html ) )
[author] => Produced by Claudine Spera and Ellie Violet Bramley
[origin] => Array (
[streamId] => streamId
[title] => Syria | The Guardian
[htmlUrl] => htmlUrl )
[summary] => Array (
[direction] => ltr
[content] => Ayman is one of about 3 million Syrian refugees living outside his homeland. After nine of his friends were killed in Damascus, Ayman used his student visa to flee to the UK, leaving his wife and twin boys behind. 'We didn't expect civil war in Syria, never, ever,' he says. But now, with the help of the Red Cross, the family are reunited and building a life in Edinburgh Continue reading... )
[visual] => Array (
[url] => imagUrl
[height] => 252
[width] => 600
[contentType] => image/jpeg )
[unread] => 1
[categories] => Array (
[0] => Array (
[id] => user/5312f2fe-1adc-476f-93da-8b1db5a63180/category/عربي
[label] => عربي ) )
[engagement] => 4 [engagementRate] => 0.33 ) ) )
If there are always the same keys in the array it should be relatively easy to iterate with something like this:
foreach($yourarray as $key1=>$item1){
//dosomething
if($key1=="alternate"){
foreach($item1 as $key2=>$item2){
//and so on
}
}
}
As to saving it in a database I admit its not easy and I am not that experienced with databases but if I were you I'd save them in multiple tables with "1 to n" references.
Q: How to Search Massive Multi-Dimensional Array for Single Value, and Return Parent Array?
I have this massive json that represents all of the achievements in WoW.
http://us.battle.net/api/wow/data/character/achievements
I converted it into an array using json_decode. This then leaves me with a very massive array that I need to search all of its levels until I find a specific value, I then need to return the parent array of that value.
ex:
This is one small part of the decoded array.
[0] => Array
(
[id] => 7385
[title] => Pub Crawl
[points] => 10
[description] => Complete the Brewmaster scenario achievements listed below.
[reward] => Reward: Honorary Brewmaster Keg
[rewardItems] => Array
(
[0] => Array
(
[id] => 87528
[name] => Honorary Brewmaster Keg
[icon] => inv_holiday_brewfestbuff_01
[quality] => 3
[itemLevel] => 90
[tooltipParams] => Array
(
)
[stats] => Array
(
)
[armor] => 0
)
)
[icon] => inv_misc_archaeology_vrykuldrinkinghorn
[criteria] => Array
(
[0] => Array
(
[id] => 20680
[description] => Spell No Evil
[orderIndex] => 0
[max] => 1
)
[1] => Array
(
[id] => 20681
[description] => Yaungolian Barbecue
[orderIndex] => 1
[max] => 1
)
[2] => Array
(
[id] => 20682
[description] => Binan Village All-Star
[orderIndex] => 2
[max] => 1
)
[3] => Array
(
[id] => 20683
[description] => The Keg Runner
[orderIndex] => 3
[max] => 1
)
[4] => Array
(
[id] => 20684
[description] => Monkey in the Middle
[orderIndex] => 4
[max] => 1
)
[5] => Array
(
[id] => 20685
[description] => Monkey See, Monkey Kill
[orderIndex] => 5
[max] => 1
)
[6] => Array
(
[id] => 20686
[description] => Don't Shake the Keg
[orderIndex] => 6
[max] => 1
)
[7] => Array
(
[id] => 20687
[description] => Party of Six
[orderIndex] => 7
[max] => 1
)
[8] => Array
(
[id] => 20688
[description] => The Perfect Pour
[orderIndex] => 8
[max] => 1
)
[9] => Array
( re
[id] => 20689
[description] => Save it for Later
[orderIndex] => 9
[max] => 1
)
[10] => Array
(
[id] => 20690
[description] => Perfect Delivery
[orderIndex] => 10
[max] => 1
)
)
[accountWide] =>
[factionId] => 2
)
I am attempting to create a function where I can just simply enter the achievement ID, which in this exmple is 7385, and have the parent array which would be [0] => Array (...); returned, so i can then grab the achievement details from that array.
I am not sure if this is really a proper question, as I am not sure as where to start.
So far I have just started breaking the original massive array down into its 10 equally as massive categories, and then searching them each individually, but I would like to just be able to search the main array once instead of searching each category array individually.
ex:
$allAchieves = file_get_contents('http://us.battle.net/api/wow/data/character/achievements');
$allAchieves = json_decode($allAchieves, true);
$generalAchieves = $allAchieves[achievements][0][achievements];
$quests = $allAchieves[achievements][1][categories];
$explorationAchieves = $allAchieves[achievements][2][categories];
$pvp = $allAchieves[achievements][3][categories];
$dungeonAndRaids = $allAchieves[achievements][4][categories];
$professions = $allAchieves[achievements][5][categories];
$reputation = $allAchieves[achievements][6][categories];
$scenarios = $allAchieves[achievements][7][categories];
$worldEvents = $allAchieves[achievements][8][categories];
$petbattle = $allAchieves[achievements][9][categories];
$featsOfStrength = $allAchieves[achievements][10][categories];
Hopefully someone can help, as the other threads I have seen sofar on array searching seem too simple to be of any help as the arrays they are dealing with are nothing to the size of the one I have here.
Thanks for the suggestions, but I solved the issue using a different approach found here:
http://us.battle.net/wow/en/forum/topic/8892160022?page=1#4
Here is an example of an array that is output:
Array ( [CART] => Array ( [ITEMS] => Array ( [0] => Array ( [product_id] => 269194 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => 15 Top Hits for Easy Piano [product_code] => HL102668 [product_price] => 14.9900 [original_price] => 14.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) [1] => Array ( [product_id] => 266421 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => Whistle [product_code] => HD245839 [product_price] => 3.9900 [original_price] => 3.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) ) [LAST_UPDATED] => 1349829499 [NUM_ITEMS] => 2 ) [JustAddedProduct] => [CHECKOUT] => Array ( ) )
There is an array for each unique product (in this example there are 2 unique products.) Sometimes there will be just one, sometimes there could be 20 or more unique products.
The value that is important to me is [product_code]. You can see that in the first array, there is [product_code] => HL102668. In the second there is [product_code] => HD245839.
How can I check to see if 'HD' exists in any of the [product_code] values? If it does, I need to return false.
Thank you for your help!
Access your sub array :
$products = $array['CART']['ITEMS'];
Loop through your sub array :
foreach ($products as $product)
Check if HD exists in your product_code, with either simple strstr, or with regex using preg_match (if you are comfortable with it).
if (strstr($product['product_code'], "HD")) {
// Do your actions
}