How separate php array with same value and not same - php

I have a shop basket with for example 6 items. Some items are for one shop and another items for one and maybe two shops.
I want create factors for each shops. How can I do? How can I known these items in basket are for one shop or has for 2 or 3 shop . How can separate this items to factor.
Array('customer' => Array('basket' => Array(
'9_2' => Array
(
"row" => "0",
'item' => 'cd',
'count' => '1',
'sale_start_date' => '1391-12-25 19:27:56',
'sale_end_date' => '1392-04-20 19:27:49',
'sale_price' => '40500',
'price' => '54564',
'id' => '999035',
'shopid' => '4'
),
'999_17' => Array
(
'row' => '1',
'item' => 'car',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '6'
),
'9_3' => Array
(
'row' => '2',
'item' => 'book',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '4'
),
'10_5' => Array
(
'row' => '2',
'item' => 'dvd',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '5'
)
)
)
);
This example is array from my SESSION . I want seperate factor with shopid value. In this basket for example we have 2 item from shopid=4 and have 1 item from shopid=6 and one item from shopid=5 how can create seprate factor for each shop.
First I use usort function for sort array with shopid but I can't continue for separate each same and not same item shop for create factor

Something like this should work
<?php
$shopItems = array();
foreach ($_SESSION['customer']['basket'] as $basket) {
$key = $basket['shopid'];
$shopItems[$key][] = $basket;
}
This will store groups of items from the same shop id in elements of the array $shopItems
For example (only showing shop id 4)
Array (
[4] => Array (
[0] => array(
"row" => "0",
'item' => 'cd',
'count' => '1',
'sale_start_date' => '1391-12-25 19:27:56',
'sale_end_date' => '1392-04-20 19:27:49',
'sale_price' => '40500',
'price' => '54564',
'id' => '999035',
'shopid' => '4'
),
[1] => array(
'row' => '2',
'item' => 'book',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '4'
)
)
)

$basket = array();
foreach ($_SESSION['customer']['basket'] as $k => $v)
$basket[$v['shopid']][$k] = $v;
print_r($basket);
Result:
Array
(
[4] => Array
(
[9_2] => Array
(
[row] => 0
[item] => cd
[count] => 1
[sale_start_date] => 1391-12-25 19:27:56
[sale_end_date] => 1392-04-20 19:27:49
[sale_price] => 40500
[price] => 54564
[id] => 999035
[shopid] => 4
)
[9_3] => Array
(
[row] => 2
[item] => book
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 4
)
)
[6] => Array
(
[999_17] => Array
(
[row] => 1
[item] => car
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 6
)
)
[5] => Array
(
[10_5] => Array
(
[row] => 2
[item] => dvd
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 5
)
)
)

This should do the trick.
function get_seperated_shops($all_items)
{
$seperated_shops = array();
foreach($all_items AS $item => $properties)
{
$seperated_shops[$properties['shopid']][$item] = $properties;
}
return $seperated_shops;
}
echo "<pre>";
print_r(get_seperated_shops($_SESSION['customer']['basket']));
echo "</pre>";

Related

Referencing field from stdClass Object which contains an array of drawable information

I'd like to return the value of the Birthday field from the following massive mixed array of data containing nested arrays and stdClass Objects. I tried the following code but it didn't work, I would be extremely grateful for any help.
$data->_field_data['nid']['entity']->field_player_birthday['und'][0]['value'];
PHP:
stdClass::__set_state(array(
'node_field_data_field_game_players_nid' => '2931',
'node_title' => '1539634465',
'nid' => '8114',
'node_language' => 'tr',
'field_data_field_player_birthday_node_entity_type' => 'node',
'_field_data' => array (
'node_field_data_field_game_players_nid' => array (
'entity_type' => 'node',
'entity' => stdClass::__set_state(
array( 'vid' => '2931',
'uid' => '1',
'title' => 'Fernando Muslera',
'log' => '',
'status' => '1',
'comment' => '1',
'promote' => '0',
'sticky' => '0',
'ds_switch' => '',
'nid' => '2931',
'type' => 'player',
'language' => 'und',
'created' => '1520727843',
'changed' => '1539137801',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1539137801',
'revision_uid' => '1',
'body' =>
array ( ), 'field_player_address' => array ( 'und' => array ( 0 =>
array ( 'country' => 'AR', 'administrative_area' => 'B',
'sub_administrative_area' => NULL, 'locality' => 'Buenos Aires',
'dependent_locality' => '', 'postal_code' => '', 'thoroughfare' => '',
'premise' => '', 'sub_premise' => NULL, 'organisation_name' => NULL,
'name_line' => NULL, 'first_name' => NULL, 'last_name' => NULL, 'data'
=> NULL, ), ), ), 'field_geofield' => array ( 'und' => array ( 0 => array ( 'geom' => 'POINT (-58.3815591 -34.6036844)', 'geo_type' =>
'point', 'lat' => '-34.603684400000', 'lon' => '-58.381559100000',
'left' => '-58.381559100000', 'top' => '-34.603684400000', 'right' =>
'-58.381559100000', 'bottom' => '-34.603684400000', 'geohash' =>
'69y7pkxfgjdq4', ), ), ), 'field_player_search_news' => array ( ),
'field_player_map' => array ( ), 'field_player_birthday' => array (
'und' => array ( 0 => array ( 'value' => '1986-06-16 00:00:00',
'timezone' => 'America/New_York', 'timezone_db' => 'America/New_York',
'date_type' => 'datetime', ), ), ), 'field_player_team' => array (
'und' => array ( 0 => array ( 'tid' => '328', ), ), ), 'field_image'
=> array ( ), 'field_game_rating' => array ( 'und' => array ( 0 => array ( 'rating' => '90', 'target' => NULL, ), ), ), 'rdf_mapping' =>
array ( 'rdftype' => array ( 0 => 'sioc:Item', 1 => 'foaf:Document',
), 'title' => array ( 'predicates' => array ( 0 => 'dc:title', ), ),
'created' => array ( 'predicates' => array ( 0 => 'dc:date', 1 =>
'dc:created', ), 'datatype' => 'xsd:dateTime', 'callback' =>
'date_iso8601', ), 'changed' => array ( 'predicates' => array ( 0 =>
'dc:modified', ), 'datatype' => 'xsd:dateTime', 'callback' =>
'date_iso8601', ), 'body' => array ( 'predicates' => array ( 0 =>
'content:encoded', ), ), 'uid' => array ( 'predicates' => array ( 0 =>
'sioc:has_creator', ), 'type' => 'rel', ), 'name' => array (
'predicates' => array ( 0 => 'foaf:name', ), ), 'comment_count' =>
array ( 'predicates' => array ( 0 => 'sioc:num_replies', ), 'datatype'
=> 'xsd:integer', ), 'last_activity' => array ( 'predicates' => array ( 0 => 'sioc:last_activity_date', ), 'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601', ), ), 'path' => array ( 'pathauto' =>
'1', ), 'cid' => '0', 'last_comment_timestamp' => '1520727843',
'last_comment_name' => NULL, 'last_comment_uid' => '1',
'comment_count' => '0', 'name' => 'efet', 'picture' => '0', 'data' =>
'a:7:{s:7:"contact";i:1;s:5:"block";a:1:{s:6:"system";a:1:{s:4:"main";i:1;}}s:16:"ckeditor_default";s:1:"t";s:20:"ckeditor_show_toggle";s:1:"t";s:14:"ckeditor_width";s:4:"100%";s:13:"ckeditor_lang";s:2:"en";s:18:"ckeditor_auto_lang";s:1:"t";}',
)), ), ), 'field_field_player_birthday' => array ( 0 => array (
'rendered' => array ( '#markup' => '1986', '#access' => true, ), 'raw'
=> array ( 'value' => '1986-06-16 00:00:00', 'timezone' => America/New_York', 'timezone_db' => 'America/New_York', 'date_type'
> => 'datetime', ), ), ), ))
You are close (as I've had to change the objects slightly to test, I hope this works)...
echo $data->_field_data['node_field_data_field_game_players_nid']['entity']
->field_player_birthday['und'][0]['value'];
Basically instead of nid, it's node_field_data_field_game_players_nid.

Mongodb Search from Collection

I need to Search collection in mongodb having
'food_name' => 'fish'
and
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
I tried the following code. But the result is not-correct. I think multiple $eq is not allowed (same index in array).
array (
'$and' =>
array (
array (
'food' =>
array (
'$elemMatch' =>
array (
'food_name' =>
array (
'$eq' => 'fish',
),
),
),
),
array (
'room' =>
array (
'$elemMatch' =>
array (
'room_features' =>
array (
'$elemMatch' =>
array (
'$eq' => 'Shower'
'$eq' => 'Hairdryer'
),
),
'roomrate' =>
array (
'$eq' => new MongoInt32(2500),
),
),
),
),
),
)
Here is the document I need to search.
array (
'_id' => new MongoId("59670aca7fafd8342e3c9869"),
'subcat_name' => 'Test',
'place' => '',
'description' => '',
'created_date' => '1499970060',
'created_by' => 'Admin',
'openingtime' => '',
'closingtime' => '',
'hotel_class_id' => '594245f67fafd87e243c986a',
'hotel_type_id' => '594244177fafd884563c9869',
'latitude' => '0',
'longitude' => '0',
'dist_id' => '5911966a7fafd8c83c3c986a',
'cat_id' => '58fb230e7fafd883183c986d',
'featured' => '0',
'visited' => new MongoInt64(5),
'subcat_slug' => 'test-trivandrum-1',
'image' => NULL,
'food' =>
array (
0 =>
array (
'food_id' => '149992634012642164',
'region_id' => '5944ba947fafd883333c9869',
'food_name' => 'fish',
'type' => 'veg',
'rate' => '100',
),
1 =>
array (
'food_id' => '14999366891994980639',
'region_id' => '595c75c17fafd835173c986c',
'food_name' => 'curry',
'type' => 'veg',
'rate' => '1000',
),
),
'room' =>
array (
0 =>
array (
'room_id' => '14999346791721342880',
'roomtype' => 'DELUXE KING ROOM1',
'roomrate' => new MongoInt64(2500),
'image' => 'beach_icon33.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
),
1 =>
array (
'room_id' => '14999346901389554873',
'roomtype' => 'DELUXE KING ROOM new',
'roomrate' => new MongoInt64(4000),
'image' => 'beach_icon34.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Bathrobe',
),
),
),
)
Please Give me an alternate way to search multiple item from array.
Thanks in advance.
I think if you want to query list you can add the list in query,
try this
{
"food" : {
"$elemMatch": {
"food_name" : "fish"
}
},
"room" : {
"$elemMatch": {
"room_features" : ["Shower", "Hairdryer"]
}
},
}
Hope this help.

Group objects in php with similar course_id

I have a variable $items that has following data inside it.
Array
(
[0] => stdClass Object
(
[course_id] => 8
[doc_id] => 85
[doc_title] => PDF Notes
[doc_price] => 243
)
[1] => stdClass Object
(
[course_id] => 6
[doc_id] => 73
[doc_title] => PDF Notes
[doc_price] => 12
)
[2] => stdClass Object
(
[course_id] => 6
[doc_id] => 75
[doc_title] => Audio Topics
[doc_price] => 21
)
[3] => stdClass Object
(
[course_id] => 6
[doc_id] => 77
[doc_title] => Video Past Papers
[doc_price] => 32
)
)
I want to categorize the data. The course_id should become the key and the courses that have similar course_id should be categorized and become the value of that key. Like in the about array, [1], [2] and [3] have same course_id so the for key => 6, an array should be made with sub array that have docs 73, 75 and 77.
array => (
'6' => 'array of docs with course_id 6',
'8' => 'array of docs with course_id 8',
)
VAR_EXPORT
array (
0 =>
stdClass::__set_state(array(
'course_id' => '8',
'doc_id' => '85',
'doc_title' => 'PDF Notes',
'doc_price' => '243',
)),
1 =>
stdClass::__set_state(array(
'course_id' => '6',
'doc_id' => '73',
'doc_title' => 'PDF Notes',
'doc_price' => '12',
)),
2 =>
stdClass::__set_state(array(
'course_id' => '6',
'doc_id' => '75',
'doc_title' => 'Audio Topics',
'doc_price' => '21',
)),
3 =>
stdClass::__set_state(array(
'course_id' => '6',
'doc_id' => '77',
'doc_title' => 'Video Past Papers',
'doc_price' => '32',
)),
)
Try this:
use get_object_vars to convert obj to array.
$newarray = array();
foreach ($items as $item)
{
$newarray[$item->course_id][] = get_object_vars($item);
}

Shopify orders array issue

I have these two arrays
$order2 = array('line_items' => array(
array('title' => 'Shipping',
'price' => '10',
'quantity' => '2',
)
));
$order3 =array('title' => 'Handling',
'price' => '5',
'quantity' => '1',
);
I've used array_merge and array_push in this instance but both don't give me my desired output. I'm trying to get it to show like this.
array('line_items' => array(
array('title' => 'Shipping',
'price' => '10',
'quantity' => '2',
),
array('title' => 'Handling',
'price' => '5',
'quantity' => '1',
)
));
The output i get is
Array ( [line_items] => Array ( [0] => Array ( [title] => Shipping [price] => 10 [quantity] => 2 ) ) [title] => Handling [price] => 5 [quantity] => 1 )
How can i achieve my desired output so i can use this as a shopify order?
You don't need any functions to do that, just a simple array assignment on top of the existing one:
$order2['line_items'][] = $order3;

Incomplete Data from json_decode

Okay some basic info...
PHP 5.3.5
JSON 1.2.1
This is all running on a local XAMPP server. Basically, my code below is returning an incomplete associative array. The json_last_error() returns 0. I have searched the net but could not find this type of error without haveing a specific json error returned.
<?php
$ShippingObj = json_decode($_POST['formData'], true);
echo json_last_error();
echo $ShippingObj;
I have validated the string with JSONLint..and here is what its output was....
array (
'name' => 'newShipment',
'comments' => '',
'count' => 1,
'itemID' => 0,
'itemWeight' => 7.548,
'itemLength' => 0,
'itemWidth' => 0,
'itemHeight' => 0,
'limits' => '',
'collection' =>
array (
0 =>
array (
'name' => '8x6x4 Standard',
'comments' => '',
'count' => 5,
'itemID' => '3',
'itemWeight' => 7.5475,
'itemLength' => '8',
'itemWidth' => '6',
'itemHeight' => '4',
'limits' => 'MFR',
'collection' =>
array (
0 =>
array (
'ID' => '1',
'Name' => ' TA-200 Demarcation Enclosure',
'Qty' => 1,
'Weight' => '0.34',
),
1 =>
array (
'ID' => '2',
'Name' => ' CG-1000 Demarcation Enclosure',
'Qty' => 4,
'Weight' => '1',
),
2 =>
array (
'ID' => '19',
'Name' => ' FM-02 Stereo FM Transmitter',
'Qty' => 1,
'Weight' => '0.1625',
),
3 =>
array (
'ID' => '20',
'Name' => ' Renard 64 Rev XC Parts Kit w/PCB',
'Qty' => 1,
'Weight' => '1.125',
),
4 =>
array (
'ID' => '21',
'Name' => ' SSRez Parts Kit w/PCB - Set of 8',
'Qty' => 2,
'Weight' => '0.86',
),
),
),
),
)
Which is a valid JSON construct.....here is the input that I copied into http://www.functions-online.com/json_decode.html to verify my string.
$returnValue = json_decode('{"name":"newShipment","comments":"","count":1,"itemID":0,"itemWeight":7.548,"itemLength":0,"itemWidth":0,"itemHeight":0,"limits":"","collection":{"0":{"name":"8x6x4 Standard","comments":"","count":5,"itemID":"3","itemWeight":7.5475,"itemLength":"8","itemWidth":"6","itemHeight":"4","limits":"MFR","collection":{"0":{"ID":"1","Name":" TA-200 Demarcation Enclosure","Qty":1,"Weight":"0.34"},"1":{"ID":"2","Name":" CG-1000 Demarcation Enclosure","Qty":4,"Weight":"1"},"2":{"ID":"19","Name":" FM-02 Stereo FM Transmitter","Qty":1,"Weight":"0.1625"},"3":{"ID":"20","Name":" Renard 64 Rev XC Parts Kit w/PCB","Qty":1,"Weight":"1.125"},"4":{"ID":"21","Name":" SSRez Parts Kit w/PCB - Set of 8","Qty":2,"Weight":"0.86"}}}}}', true);
The first section of code returns the below for $ShippingObj. Note the "[collection] => array(5) " that does not expand out...its empty. I have checked the string from the previous JS and it is fine.
array(10) (
[name] => (string) newShipment
[comments] => (string)
[count] => (int) 1
[itemID] => (int) 0
[itemWeight] => (float) 16.2955
[itemLength] => (int) 0
[itemWidth] => (int) 0
[itemHeight] => (int) 0
[limits] => (string)
[collection] => array(1) (
[0] => array(10) (
[name] => (string) CG2000 OEM Box
[comments] => (string)
[count] => (int) 5
[itemID] => (string) 2
[itemWeight] => (float) 8.1475
[itemLength] => (string) 21
[itemWidth] => (string) 16.5
[itemHeight] => (string) 12.5
[limits] => (string) 2ND
[collection] => array(5) (
)
)
)
)

Categories