Remove part of an array if its not contain - php

I have an array in php called $json. It looks like this
Array (
[0] => Array (
[name] => Knife
[url] => http://steamcommunity.com/market/listings/753/297120...
[price] => 0.16 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 30
[game] => Counter-Strike: Global Offensive )
[1] => Array (
[name] => Strange Knife
[url] => http://steamcommunity.com/market/listings/440/Strange%2...
[price] => 0.55 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 177
[game] => Team Fortress 2 )
[2] => Array (
[name] => Festive Knife
[url] => http://steamcommunity.com/market/listings/440/Festive%20Knife" id="resultlink_2
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 66
[game] => Team Fortress 2 )
[3] => Array (
[name] => â… Flip Knife
[url] => http://steamcommunity.com/market/listings/7
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 24
[game] => Counter-Strike: Global Offensive )
...
How I can remove a part of an array where GAME != "Counter-Strike: Global Offensive" ?

$array; // Your array.
foreach($array as $subarray) {
if($subarray['game'] != "Counter-Strike: Global Offensive") {
unset($subarray);
}
}

Related

How can I combine 2 associative arrays with different keys and how to get the result array in the input to give order in PHP?

I have 2 arrays like:
$arr1 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
)
$arr2 = [220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
I need the result like :
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
For this I simply using array_merge
$arr3= $arr1+ $arr2;
But after that I got the result like:
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
My issue is after merging 2 associative arrays with different keys, I got the first array and second array mixed which means I need the result like the first three array elements of the first array, after that 4 array elements of the second array.
Can you anyone help me, please, It will be helpful for me
You could use:
foreach($arr2 as $key => $record) {
$arr1[$key] = $record;
}
This would result in the order you're looking for.
demo

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

Foreach to get data from array in wordpress php

Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}

Fetch values from an array using nested foreach loop

I have a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}

Sorting an array with stdClass Objects

I am attempting sort this array by each stdClass Object's [title] in an Ubercart order.tpl.php file. I have tried doing just a normal sort but since they all are stdClass Objects it doesn't do anything.
<pre>Array
(
[0] => stdClass Object
(
[order_product_id] => 1157
[order_id] => 142
[nid] => 38
[title] => Eatonville Forest Camping Permit
[manufacturer] =>
[model] => eatonville-camp-permit
[qty] => 1
[cost] => 9.00000
[price] => 10.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 0
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[1] => stdClass Object
(
[order_product_id] => 1158
[order_id] => 142
[nid] => 35
[title] => Eatonville Forest Motorized Recreation Access Permit and Release of Liability
[manufacturer] =>
[model] => eatonville-motor-rec-access
[qty] => 1
[cost] => 175.00000
[price] => 175.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[2] => stdClass Object
(
[order_product_id] => 1159
[order_id] => 142
[nid] => 31
[title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Families and Release of Liability
[manufacturer] =>
[model] => snoqualmie-family-non-motor-rec-access
[qty] => 1
[cost] => 150.00000
[price] => 150.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[3] => stdClass Object
(
[order_product_id] => 1160
[order_id] => 142
[nid] => 40
[title] => Snoqualmie Forest Camping Permit
[manufacturer] =>
[model] => snoqualmie-camp-permit
[qty] => 1
[cost] => 300.00000
[price] => 300.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[4] => stdClass Object
(
[order_product_id] => 1161
[order_id] => 142
[nid] => 39
[title] => White River Forest Camping Permit
[manufacturer] =>
[model] => whiteriver-camp-permit
[qty] => 1
[cost] => 300.00000
[price] => 300.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[5] => stdClass Object
(
[order_product_id] => 1162
[order_id] => 142
[nid] => 30
[title] => White River Forest Family Non-motorized Recreation Access Permit for Families and Release of Liability
[manufacturer] =>
[model] => whiteriver-family-non-motor-rec-access
[qty] => 1
[cost] => 150.00000
[price] => 150.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[6] => stdClass Object
(
[order_product_id] => 1163
[order_id] => 142
[nid] => 33
[title] => White River Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
[manufacturer] =>
[model] => whiteriver-non-motor-rec-access
[qty] => 1
[cost] => 75.00000
[price] => 75.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[7] => stdClass Object
(
[order_product_id] => 1164
[order_id] => 142
[nid] => 34
[title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
[manufacturer] =>
[model] => snoqualmie-non-motor-rec-access
[qty] => 1
[cost] => 75.00000
[price] => 75.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
)</pre>
This is the code controlling it in the order.tpl.php
<pre>php if (is_array($order->products)) {
$context = array(
'revision' => 'formatted',
'type' => 'order_product',
'subject' => array(
'order' => $order,
),
);
}
</pre>
I think usort() should work:
function sortByTitle($a, $b){
return strcmp($a->title, $b->title);
}
usort($theArray, 'sortByTitle');
You may want to throw in some checking to make sure the title member actually exists.

Categories