how can i get acces in developers array? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
$employees = array( 'Internee' =>
array(
'name' => 'Nabeel Salman',
'salary' => 'Non-Paid',
'join_date' => '12-12-2014',
'skills' => array('php',
'mysql'),
'phone' => array('0332-8700207',
"052-42900092"),
'address' => array('addr' => 'Karigar Web Solutions',
'perm_addr' => 'Sadar Cantt',
'city' => 'Sialkot',
'province' => 'Punjab',
'country' => 'Pakistan',
'zipcode' => "51310"),
'work hours' => "7.5"
),
'CEO' =>
array(
'name' => 'Muhammad Faisal',
'salary' => "100000",
'join_date' => '12-12-2009',
'skills' => array('php',
'mysql',
'JavaScript',
'jQuery',
'Laravel',
'CodeIgnitor'),
'phone' => array('0321-9999999',
"052-4582565"),
'address' => array('addr' => 'Karigar Web Solutions',
'perm_addr' => '35 Holdenhurst Road, Bournemouth, Dorset, BH8 8EJ',
'city' => 'Sialkot',
'province' => 'Punjab',
'country' => 'Pakistan',
'zipcode' => "51310"),
'work hours' => "7.5"
),
'Developers' =>
array( 'Developer1' =>
array(
'name' => 'Wasif Iqbal',
'salary' => "35000",
'join_date' => '24-3-2014',
'skills' => array('php',
'mysql',
'JavaScript',
'jQuery',
'Laravel'),
'phone' => array('0333-8694862',
"052-4111111"),
'address' => array('addr' => 'Karigar Web Solutions',
'perm_addr' => 'Defense Road',
'city' => 'Sialkot',
'province' => 'Punjab',
'country' => 'Pakistan',
'zipcode' => "51310"),
'work hours' => "7.5"
),
'Developer2'=>
array(
'name' => 'Bilal',
'salary' => "35000",
'join_date' => '24-3-2014',
'skills' => array('php',
'mysql', 'Networking',
'jQuery',
'JavaScript',
'Laravel'),
'phone' => array('0332-9900997',
"052-42897678"),
'address' => array('addr' => 'Karigar Web Solutions',
'perm_addr' => 'Somewhere in Sialkot',
'city' => 'Sialkot',
'province' => 'Punjab',
'country' => 'Pakistan',
'zipcode' => "51310"),
'work hours' => "7.5"
),
),
'Manager'=>
array(
'name' => 'Fahad Maqsood',
'salary' => '80000',
'join_date' => '12-12-2009',
'skills' => array('Management', 'Accounting'),
'phone' => array('+923217435809', "+13236866002"),
'address' => array('addr' => 'Karigar Web Solutions',
'perm_addr' => 'Shifting to a new place, will update in a few days.',
'city' => 'Sialkot',
'province' => 'Punjab',
'country' => 'Pakistan',
'zipcode' => "51310"),
'work hours' => "7.5"
),
);
how can i get acces in developers array ???

You can access the developers array like this:
$employees['Developers']
And then use the related indexes:
$employees['Developers']['Developer2']['name']
This is (like almost everything in PHP) well documented in the language reference

Related

Filter and re-format a given multi-dimensional array (php)

I have been given a dataset by my tutor and asked to carry out the following:
// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,
'sku' => 'TEST_3',
'alternates' => [
'asin' => [
'code' => '50',
'value' => 'JL1235',
],
'barcode' => [
'code' => '10',
'value' => 'JS160694',
],
],
'commodityCode' => '9989898889',
'price' => [
'value' => 145.99,
],
'instructions' => [
'picking' => [
'code' => 'PICK',
'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
],
],
'quantity' => [
'inner' => 100,
'masterCarton' => 200,
'pallet' => 300,
],
'averageMonthlyUnitSales' => 100,
'created_at' => '2022-03-13 04:14:12'
],
Example dataset is here:
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:
$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});
But I am not too sure how I can go about getting a new array in the required format?
The only way I can see to simply do both is as follows.
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
$new = [];
foreach ($data as $line){
if ( $line['averageMonthlyUnitSales'] > 350 ){
// reformat the array
$new = [
'id' => $line['id'],
'sku' => $line['sku'],
'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
],
'commodityCode' => $line['commodityCode'],
'price' => [ 'value' => $line['price'] ],
'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
],
'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
'created_at'=>$line['averageMonthlyUnitSales']
];
}
}
print_r($new);
RESULT
PHP 8.1.3
Array
(
[id] => 9947
[sku] => test_1
[alternates] => Array
(
[asin] => Array
(
[code] => 50
[value] =>
)
[barcode] => Array
(
[code] => 10
[value] => js160694
)
)
[commodityCode] => 9989898889
[price] => Array
(
[value] => 120.50
)
[instructions] => Array
(
[picking] => Array
(
[code] => PICK
[value] =>
)
)
[quantity] => Array
(
[inner] => 120
[masterCarton] => 200
[pallet] => 1200
)
[averageMonthlyUnitSales] => 750
[created_at] => 750
)

PHP/JSON array problems when connecting to API

The below code is not rendering the line_items as a JSON array and I have searched/asked around to no avail.
Can anyone give me a hand here?
I've also included the json_encode data of the $orderShippingInfo variable.
Below is the JSON output:
{
"token":"API_KEY_HERE",
"email":"a.pinochet#chilehelicoptertours.com",
"line_items":{
"sku":"12345",
"name":"Product Name",
"title":"Product Title",
"price":"$1.99",
"quantity":"1",
"total_tax":"$0.00"
},
"shipping_lines":{
"title":"UPS",
"price":"$0.00",
"method":"UPS 3 DAY",
"carrier":"UPS"
},
"order_id":"0001",
"profile":"default",
"shipping_address":{
"province":"AZ",
"city":"Testville",
"first_name":"Augusto",
"last_name":"Pinochet",
"zip":"12341",
"province_code":"NY",
"country":"US",
"company":"Company Name, Inc.",
"phone":"1112223333",
"country_code":"US",
"address1":"123 Testing Dr Street",
"address2":"Suite #1"
},
"subtotal_price":"$0.00",
"created_at":"2017-02-02",
"country_code":"US",
"total_discounts":"$0.00",
"total_price":"$0.00"
}
TIA!
<?php
$orderShippingInfo = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet#chilehelicoptertours.com',
'line_items' => array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
'quantity' => '1',
'total_tax' => '$0.00',
),
'shipping_lines' => array(
'title' => 'UPS',
'price' => '$0.00',
'method' => 'UPS 3 DAY',
'carrier' => 'UPS',
),
'order_id' => '0001',
'profile' => 'default',
'shipping_address' => array(
'province' => 'AZ',
'city' => 'Testville',
'first_name' => 'Augusto',
'last_name' => 'Pinochet',
'zip' => '12341',
'province_code' => 'NY',
'country' => 'US',
'company' => 'Company Name, Inc.',
'phone' => '1112223333',
'country_code' => 'US',
'address1' => '123 Testing Dr Street',
'address2' => 'Suite #1',
),
'subtotal_price' => '$0.00',
'created_at' => date('Y-m-d'),
'country_code' => 'US',
'total_discounts' => '$0.00',
'total_price' => '$0.00',
);
echo json_encode($orderShippingInfo);
?>
You need to make your array of line items an array of them.
For example:
$orderShippingInfo = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet#chilehelicoptertours.com',
'line_items' => array(
array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
'quantity' => '1',
'total_tax' => '$0.00',
)
),
'shipping_lines' => array(
'title' => 'UPS',
'price' => '$0.00',
'method' => 'UPS 3 DAY',
'carrier' => 'UPS',
),
'order_id' => '0001',
'profile' => 'default',
'shipping_address' => array(
'province' => 'AZ',
'city' => 'Testville',
'first_name' => 'Augusto',
'last_name' => 'Pinochet',
'zip' => '12341',
'province_code' => 'NY',
'country' => 'US',
'company' => 'Company Name, Inc.',
'phone' => '1112223333',
'country_code' => 'US',
'address1' => '123 Testing Dr Street',
'address2' => 'Suite #1',
),
'subtotal_price' => '$0.00',
'created_at' => date('Y-m-d'),
'country_code' => 'US',
'total_discounts' => '$0.00',
'total_price' => '$0.00',
);
Then you can add a second line item to the array if you require.
The reason for this is that javascript does not have an associative array concept, so to produce one out of a json_encode() would break javascript.
See this example of what json_encode will do
$xx = ['a','b'];
$yy = ['one'=> 1, 'two'=>2];
print_r($xx);
echo json_encode($xx).PHP_EOL;
print_r($yy);
echo json_encode($yy);
Array
(
[0] => a
[1] => b
)
["a","b"] // note this is a JSON array
Array
(
[one] => 1
[two] => 2
)
{"one":1,"two":2} // note this is a JSON object
If the PHP array is numerically indexed, you get a JSON array
If the PHP array is assoc array it will create an JSON Object
If for some reason you specifically want a JSON String representation to be an array you could just add a [] to the $orderShippingInfo[] = array( like this
$orderShippingInfo[] = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet#chilehelicoptertours.com',
'line_items' => array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
. . .
. . .

How to create a virtual field with containable behavior in cakephp 2.6

I have this code in model User:
$hasOne = array(
'Member' => array(
'className' => 'Member',
'foreignKey' => 'user_id',
'dependent' => true
)
);
function rest_findUserById($id = NULL) {
$row = $this->find(
'first',
array(
'contain' => array(
'UserSession'=>array(
'fields'=>array('user_id', 'session_token')
) ,
'Member' => array(
'fields' => array("*")
)
),
'fields' => array('username', 'email'),
'conditions' => array('User.id' => $id)
));
if (!$row) {
return FALSE;
}
debug($row);exit;
}
and in model Member:
$virtualFields = array(
'full_name' => 'CONCAT(Member.first_name, " ",Member.last_name)'
);
$belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
?>
When I call fuction rest_findUserById() I get this output:
/app/Model/User.php (line 378)
array(
'User' => array(
'username' => 'testuser',
'email' => 'test#gmail.com',
'id' => '71'
),
'Member' => array(
'id' => '11',
'user_id' => '71',
'first_name' => 'Whjc',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '11',
'image' => '92e20fd0260dcc5ea289611221723b6a.jpg',
'address_line_1' => 'Shhd',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '0000-00-00',
'referred_by' => 'asdf',
'created' => '2016-07-27 13:52:30',
'created_by' => '3',
'modified' => '2016-08-02 12:25:22',
'modified_by' => '3',
'status' => false
),
'UserSession' => array(
(int) 0 => array(
'user_id' => '71',
'session_token' => 'a685b3db5ab87a928716c7d8b3272572'
)
)
)
but when I call this code:
$this->Member->recursive = -1;
debug($this->Member->find('first'));
I get this output:
/app/Model/User.php (line 377)
array(
'Member' => array(
'id' => '4',
'user_id' => '64',
'first_name' => 'SDFS SDF',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '',
'image' => '',
'address_line_1' => 'sdf',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '1970-01-01',
'referred_by' => 'asdf',
'created' => '2016-07-22 15:25:30',
'created_by' => '0',
'modified' => '2016-07-22 15:25:30',
'modified_by' => '0',
'status' => false,
'full_name' => 'SDFS SDF test_user_lname'
)
)
as you can see the virtual field i.e.'full_name' is coming in the second output but not in first output.
How can I fix this?
From the manual
you cannot use virtualFields on associated models
the proposed solution is to copy the virtual field into your model, this way
/app/Model/User.php
$this->virtualFields['member_full_name'] = $this->Member->virtualFields['full_name'];
Of course this does not work for hasMany or HABTM relationships. Also note that in this way you'll find the virtual field in the User data array and not in the Member data array

How to descend several levels in find results array

Calling Cake's find method on my table like this:
$this->Client->find('all',['recursive' => -1])
returns
array(
(int) 0 => array(
'Client' => array(
'id' => '1',
'name' => 'Intel Corporation',
'website' => 'www.intel.com',
'address' => '2200 Mission College Blvd.',
'city' => 'Santa Clara',
'state' => 'CA',
'zip' => '95054'
)
),
(int) 1 => array(
'Client' => array(
'id' => '3',
'name' => 'Motorola Mobility LLC',
'website' => 'www.motorola.com',
'address' => '222 W. Merchandise Mart Plaza',
'city' => 'Chicago',
'state' => 'IL',
'zip' => '60654'
)
),
(int) 2 => array(
'Client' => array(
'id' => '4',
'name' => 'Nokia',
'website' => 'www.nokia.com',
'address' => '6000 Connection Drive',
'city' => 'Irving',
'state' => 'TX',
'zip' => '75039'
)
),)
What I want is to remove the redundant 'Client' array level:
array(
(int) 0 => array(
'id' => '1',
'name' => 'Intel Corporation',
'website' => 'www.intel.com',
'address' => '2200 Mission College Blvd.',
'city' => 'Santa Clara',
'state' => 'CA',
'zip' => '95054'
),
(int) 1 => array(
'id' => '3',
'name' => 'Motorola Mobility LLC',
'website' => 'www.motorola.com',
'address' => '222 W. Merchandise Mart Plaza',
'city' => 'Chicago',
'state' => 'IL',
'zip' => '60654'
),
(int) 2 => array(
'id' => '4',
'name' => 'Nokia',
'website' => 'www.nokia.com',
'address' => '6000 Connection Drive',
'city' => 'Irving',
'state' => 'TX',
'zip' => '75039'
),
);
I'd like to do this in native Cake, with a param call or something, but if I have to do it in a php array function, please explain. It's basically data for paginating.
That is just how Cake does it.
Are you sure moving the inner array is even necessary? You can just use it as is...
Anyway, moving the inner array is rather trivial:
foreach ($clients as & $client) {
$client = $client['Client'];
}

Magento: create bundle order programmatically

I have this code: http://pastebin.com/iFwyKM7G
Inside an event-observer class which executes after the customer registered. It creates an order automatically and it works for simple products. However I cannot figure out for the life of me how to make it work with Bundled product!
How can I make this work with a bundled product?
I DID IT!
I changed my code to the following:
$customer = Mage::getSingleton('customer/customer')->load($observer->getCustomer()->getId());
$session = Mage::getSingleton('adminhtml/session_quote');
$order_create_model = Mage::getSingleton('adminhtml/sales_order_create');
Mage::log($customer->debug(), null, 'toszodj_meg.log');
//$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
Mage::log($customer->getDefaultBillingAddress()->debug(), null, 'toszodj_meg.log');
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
$session->setCustomerId((int) $customer->getId());
$session->setStoreId((int) $storeId);
$orderData = array(
'session' => array(
'customer_id' => $customer->getId(),
'store_id' => $storeId,
),
'payment' => array(
'method' => 'banktransfer',
'po_number' => (string) '-',
),
// 123456 denotes the product's ID value
'add_products' =>array(
'2' => array(
'qty' => 1,
'bundle_option' => array(
2 => 2,
1 => 1,
),
'bundle_option_qty' => array(
2 => 1,
1 => 1,
),
),
),
'order' => array(
'currency' => 'EUR',
'account' => array(
'group_id' => $customer->getGroupId(),
'email' => (string) $customer->getEmail(),
),
'comment' => array('customer_note' => 'API ORDER'),
'send_confirmation' => 1,
'shipping_method' => 'flatrate_flatrate',
'billing_address' => array(
'customer_address_id' => $customer->getDefaultBillingAddress()->getEntityId(),
'prefix' => $customer->getDefaultBillingAddress()->getPrefix(),
'firstname' => $customer->getDefaultBillingAddress()->getFirstname(),
'middlename' => $customer->getDefaultBillingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultBillingAddress()->getLastname(),
'suffix' => $customer->getDefaultBillingAddress()->getSuffix(),
'company' => $customer->getDefaultBillingAddress()->getCompany(),
'street' => $customer->getDefaultBillingAddress()->getStreet(),
'city' => $customer->getDefaultBillingAddress()->getCity(),
'country_id' => $customer->getDefaultBillingAddress()->getCountryId(),
'region' => $customer->getDefaultBillingAddress()->getRegion(),
'region_id' => $customer->getDefaultBillingAddress()->getRegionId(),
'postcode' => $customer->getDefaultBillingAddress()->getPostcode(),
'telephone' => $customer->getDefaultBillingAddress()->getTelephone(),
'fax' => $customer->getDefaultBillingAddress()->getFax(),
),
'shipping_address' => array(
'customer_address_id' => $customer->getDefaultShippingAddress()->getEntityId(),
'prefix' => $customer->getDefaultShippingAddress()->getPrefix(),
'firstname' => $customer->getDefaultShippingAddress()->getFirstname(),
'middlename' => $customer->getDefaultShippingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultShippingAddress()->getLastname(),
'suffix' => $customer->getDefaultShippingAddress()->getSuffix(),
'company' => $customer->getDefaultShippingAddress()->getCompany(),
'street' => $customer->getDefaultShippingAddress()->getStreet(),
'city' => $customer->getDefaultShippingAddress()->getCity(),
'country_id' => $customer->getDefaultShippingAddress()->getCountryId(),
'region' => $customer->getDefaultShippingAddress()->getRegion(),
'region_id' => $customer->getDefaultShippingAddress()->getRegionId(),
'postcode' => $customer->getDefaultShippingAddress()->getPostcode(),
'telephone' => $customer->getDefaultShippingAddress()->getTelephone(),
'fax' => $customer->getDefaultShippingAddress()->getFax(),
),
),
);
$order_create_model->importPostData($orderData['order']);
$order_create_model->getBillingAddress();
$order_create_model->setShippingAsBilling(true);
$order_create_model->addProducts($orderData['add_products']);
$order_create_model->collectShippingRates();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model
->initRuleData()
->saveQuote();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model->setPaymentData($orderData['payment']);
$order_create_model
->importPostData($orderData['order'])
->createOrder();
$session->clear();
Mage::unregister('rule_data');
Mage::log('Order Successfull', null, 'siker_bammer.log');
And it works! thought the customer doesn't get notified. which iam trying to figure out now.

Categories