For ecommerce, that expected name value pair I have the following approved code:
function create_example_purchase() {
set_credentials();
$purchase = array(
'name' => 'Digital Good Purchase Example',
'description' => 'Example Digital Good Purchase',
'amount' => '12.00', // sum of all item_amount
'items' => array(
array( // First item
'item_name' => 'First item name',
'item_description' => 'a description of the 1st item',
'item_amount' => '6.00',
'item_tax' => '0.00',
'item_quantity' => 1,
'item_number' => 'XF100',
),
array( // Second item
'item_name' => 'Second Item',
'item_description' => 'a description of the 2nd item',
'item_amount' => '3.00',
'item_tax' => '0.00',
'item_quantity' => 2,
'item_number' => 'XJ100',
),
)
);
return new Purchase( $purchase);
}
I would like to get $items Array inside associative $purchase array dynamically from shipping cart.
Is there a way to generate exactly the same output above?
My dirty solution, to write $purchase array as string inclusive the generated $items array in a file and include it
later in the called script.
Help appreciated.
$itemsArray = function_that_returns_your_2d_item_array();
$purchase['items'] = $itemsArray;
or if the function_that_returns_your_2d_item_array() returns a 2d array indexed by 'items' you could do:
$purchase = array_merge($purchase, $itemsArray);
Related
I'm trying to sort my AoH which looks like this:
$VAR1 = [
{
'Name' => 'John',
'Lastname' => 'Derp',
'Organization' => 'Finance',
'OfficeNR' => '23',
'ID' => '145'
},
{
'Name' => 'Kate',
'Lastname' => 'Herp',
'Organization' => 'HR',
'OfficeNR' => '78',
'ID' => '35'
},
{
'Name' => 'Jack',
'Lastname' => 'Serp',
'Organization' => 'Finance',
'OfficeNR' => '23',
'ID' => '98'
}
];
What I'm trying to do is to filter my output using keys from AoH, for example print out only those who have 'Organization' => 'Finance'.
I've tried to solve it using new array:
my #SortedAoH = sort { {Organization=>{'Finance'}} } #AoH;
But it doesn't work.
What you want is grep, not sort. You are getting the basic syntax of equivalence checking wrong as well.
Anyway, the filter is:
my #finance_orgs = grep { $_->{'Organization'} eq 'Finance' } #AoH;
The #finance_orgs variable will now only include the ones with Organization set to Finance.
Just an explanation of the pieces:
The $_ variable is the variable that gets assigned whenever the value is implied in a block, such as in grep or map or in a for loop without an explicitly named variable.
$_->{'Organization'} performs a hash lookup on the hash as it iterates through each entry in your array.
eq is the operator used to test for string equivalence (as opposed to == which tests for numeric equivalence).
$array = array(
'name' => 'john',
'age' => '25',
'birthday' => '02-03-1988',
'gender' => 'male',
'telephone' => '98676878',
'location' => 'Australia'
);
$array_slice = array_slice($array, 0, 3);
foreach($array_slice as $key => $val) {
if($key !== 'age') {
echo $key.' => '.$val.'<br>';
}
}
Output:
name => john
birthday => 02-03-1988
How to display 3 values of array?
So, I want output to be like this:
name => john
birthday => 02-03-1988
gender => male
I am very beginner in programmer, thanks for help.
Update: new solution after exchanging some comments below
You must filter the array and get the result in a new array. Then you display the content of the second array.
To filter your original array, use either array_intersect_key or array_filter functions.
I have a model called Category which hasMany Expense. I am trying to generate an accordion type interface where a user can expand a category and edit the expenses. I can generate the input fields but they don't seem to be in "edit" mode because they are not pre-populated.
I've searched online and found a few related articles such as this one CakePHP: How to update multiple records at the same time with the Form helper and also this one CakePHP: Form helper with saveMany() to edit multiple rows at once. I have tried to emulate their code but with no success.
My CategoriesController Index function looks like this ...
public function index($project_id) {
$data = $this->Category->find('all', array(
'conditions'=>array('Category.project_id' => $project_id),
'order'=>array('Category.category_name')
));
$this->set('categories', $data);
$this->request->data = $data;
}
I have read that cakephp2 requires $this->request->data to be set for FormHelper to work. But in the examples I have found online everyone seems to use the plural of the model name so I tried that as well.
My Categories\index.ctp looks like this. I'm not at the "accordion" stage yet. I'm just trying to get input boxes on the screen that are pre-populated.
$i=0;
foreach ($categories as $category)
{
echo $this->Form->input("Category.$i.category_id");
echo $this->Form->input("Category.$i.category_name");
$j=0;
foreach($category['Expense'] as $expense)
{
echo $this->Form->input('Expense.' . $j . '.expense_id');
echo $this->Form->input('Expense.' . $j . '.expense_name');
echo $this->Form->input('Expense.' . $j . '.dollar_amount');
echo $this->Form->input('Expense.' . $j . '.sqft_amount');
$j++;
}
$i++;
}
This code seems to be iterating properly because it spits out the correct input fields. The big problem right now is just getting the fields to pre-populate. It doesn't seem to be in "edit" mode and I'm worried that that will be a problem down the road when I try to save the data.
Also, I have tried it with and without $this->form->create('Category') at the top. It doesn't seem to make a difference.
The $categories array looks like this ...
array(
(int) 0 => array(
'Category' => array(
'category_id' => '1',
'category_name' => 'Category 1',
'category_index' => '1',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '2',
'expense_name' => 'Nails',
'category_id' => '1',
'dollar_amount' => '50',
'sqft_amount' => '1',
'expense_index' => '2'
),
(int) 1 => array(
'expense_id' => '1',
'expense_name' => 'Wood',
'category_id' => '1',
'dollar_amount' => '99',
'sqft_amount' => '1',
'expense_index' => '1'
)
)
),
(int) 1 => array(
'Category' => array(
'category_id' => '3',
'category_name' => 'Category 2',
'category_index' => '2',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '3',
'expense_name' => 'Bed',
'category_id' => '3',
'dollar_amount' => '99',
'sqft_amount' => '2',
'expense_index' => '1'
),
(int) 1 => array(
'expense_id' => '4',
'expense_name' => 'Chair',
'category_id' => '3',
'dollar_amount' => '70',
'sqft_amount' => '1',
'expense_index' => '2'
)
)
)
)
Any help would be greatly appreciated. Thanks!!
Coincidentally, I've been doing almost exactly the same thing today.
In order to automatically populate the form, you need to have your data in $this->data in the view. You can do this by assigning to $this->request->data in your controller.
Then you need to have your field names reflect the structure of that array exactly. So using your example, you might have:
echo $this->Form->input("$i.Category.category_name");
for the category fields, and
echo $this->Form->input("$i.Expense.$j.expense_id");
for your expense fields.
I am creating a Wordpress theme that catalogs albums, and I have created the custom post type, created the custom fields, and have them successfully pulling in. I have several custom fields including; Artist, Album, Size, Label etc. I currently have the posts sorting alphabetically by the Artist name with this array:
$args=array(
'post_type' => 'albums',
'order' => 'ASC',
'meta_key' => 'custom_meta_artist',
'orderby' => 'meta_value',
'posts_per_page' => -1,
);
But I would also like the Albums, 'custom_meta_album', to sort alphabetically if it is the same Artist. Currently if a user enters in 10 albums by the same artist, the post will be alphabetized correctly by the Artist name, but the Albums have no order.
Is there a way to do some sort of second level sorting or primary and secondary sorting in Wordpress? I don't know if it's a IF statement that says "if artists value is equal then also sort albums ascending" or something along those lines. I figure there needs to be some way to tell Wordpress which field it should sort by first and then continue to the second level.
You may try this, hope this will work
// keep this function in your functions.php
function myCustomOrderby($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
This is your args array
$args=array(
'post_type' => 'albums',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'custom_meta_artist',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'custom_meta_album',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','myCustomOrderby'); // Add filter before you call the WP_Query
$albums = new WP_Query($args);
remove_filter('posts_orderby','myCustomOrderby'); // Remove filter after you call the WP_Query
// Start your loop
while ( $albums->have_posts() ) : $albums->the_post();
//...
endwhile;
Ok, I have an array like so:
$my_array = array(
'hi' => array('description' => 'how are you?', 'title' => 'Hello', 'link' => 'http://mylink.com'),
'bye' => array('description' => array('Goodbye!', 'see ya'), 'title' => 'See Ya Later', 'link' => 'http://mybyelink.com'),
'not_now' => array('description' => array('I am away now!', 'gone'), 'title' => 'Away', 'link' => 'http://myawaylink.com'),
'back' => array('description' => array('I am back now!', 'back', 'present'), 'title' => 'Here', 'link' => 'http://mybacklink.com'),
);
So I would like the array to be ordered, within php code, somehow so that it orders it by the TITLE of each array bound: $my_array['hi']['title'], $my_array['bye']['title'], $my_array['not_now']['title'], and $my_array['back']['title'], but it MUST keep the array intact so that all of the values within the hi, bye, not_now, and back are the exact same and all of the arrays (if any are in it, are also the same).
So this array will need to be ordered in ABC order depending on the title, so should return in this order:
$my_array = array(
'not_now' => array('description' => array('I am away now!', 'gone'), 'title' => 'Away', 'link' => 'http://myawaylink.com'),
'hi' => array('description' => 'how are you?', 'title' => 'Hello', 'link' => 'http://mylink.com'),
'back' => array('description' => array('I am back now!', 'back', 'present'), 'title' => 'Here', 'link' => 'http://mybacklink.com'),
'bye' => array('description' => array('Goodbye!', 'see ya'), 'title' => 'See Ya Later', 'link' => 'http://mybyelink.com'),
);
Titles are in ABC order here: Away, Hello, Here, and See Ya Later.
How can I do this and still keep the array intact with all of the sub-arrays in it also?
Thanks guys :)
You want to use the uasort function with a user-defined sort function using strnatcmp in the function. Here is an example for your code:
function titleSort($a, $b) {
return strnatcmp($a['title'], $b['title']);
}
uasort($my_array, 'titleSort');
If desired you could add some sanity checking on the input variables to make sure they are arrays by using is_array.