cakephp combining associated array fields - php

I've an associated array with 20 elements whose child array looks like
(int) 2 => array(
'Comment' => array(
'id' => '5',
'user_id' => '13',
'time' => '2012-05-18 14:47:36'
),
'User' => array(
'name' => 'User name'
)
)
Now I want to extract the field name from its child array User with cakephp's set utility and append it to the child array Comment. Is there a one way step to do this other than using a for or foreach loop?
(int) 2 => array(
'Comment' => array(
'id' => '5',
'user_id' => '13',
'time' => '2012-05-18 14:47:36',
'name =>'User name'
)
)

It doesn't answer your question, but doing this with a foreach is a one way step too:
foreach ($arrays as $array)
$array['Comment']['name'] = $array['User']['name'];
I believe there's no need to use some utility, just for the sake of using an utility.
Especially that it'll do a foreach itself.

Related

Trying to update the value of a multidimensional associative array when I only know the location of a child element

I am running a recursive function for a multidimensional associative array. I know what I want to do but I can't figure out the right implementation. Let's say I have an array like this:
$my_array = array(
'1' => array(
'id' => '1',
'parent' => false,
'children' => array(
'2' => array(
'id' => '2',
'parent' => '1',
'children' => array(
'3' => array(
'id' => '3',
'parent' => '2',
'children' => array(
//how do I insert an array here if I only have only have the location '3' to work from?
)
)
)
)
)
)
);
and I want to insert a new item array inside the child of item 3, but I only have 3 as the location. How do I get to a situation where I can do something like:
$my_array['1']['children']['2']['children']['3']['children'] = $my_array;
if I only have '3' as the location to work with and I don't know what the path will be before the '3'?
I thought about converting the array to json and then doing a search for 'id' => '3', and then trying to inject the array as json under the children section, but the problem with this is I couldn't figure out a way on how to find the closing bracket of the json because there could be other closing brackets inside existing children of item 3.

php insert key/value into associative array

I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}

CakePHP use Hash to prepare Model array for saving

I'm wondering how one might use CakePHP Hash functions to achieve the following:
This is the array before (Plugin stored in Session):
$Data = array(
'add' => array(
'Certificate' => array(
'name' => '',
'entity_id' => '1',
'state_id' => '18',
'domicile' => '',
'formation_date' => array(
'month' => '',
'day' => '',
'year' => ''
)
)
),
'add2' => array(
'Certificate' => array(
'filing_fees' => '82'
)
)
)
This is the array after. I am reformatting it to call Certificate->save(). I removed the top level and merged the Certificate model.
$Data = array(
'Certificate' => array(
'name' => '',
'entity_id' => '1',
'state_id' => '18',
'domicile' => '',
'formation_date' => array(
'month' => '',
'day' => '',
'year' => ''
),
'filing_fees' => '82'
)
)
This is my current code:
$processed = array();
foreach ($Data as $k => $array1) {
foreach ($array1 as $array1k => $array2) {
if (!isset($processed[$array1k])) {
$processed[$array1k] = array();
}
foreach ($array2 as $a2k => $a2v) {
$processed[$array1k][$a2k] = $a2v;
}
}
}
What I have works, but it's not very "Cake-ish" and will certainly cause problems when I need to prepare data for saving more complex model relationships (e.g. HABTM saveall).
I tried Hash::combine, Hash::merge, array_merge_recursive(), and combinations of these before posting. I understand the PHP logic, but I am lost to find the CakePHP functions with similar logic.
The first step is to merge the Model, then the second step is to remove the key ('add, 'add2'). The closest I got was Hash::combine, but there was no way to reference a $keyPath {s} of the Model, skipping over the first level ('add', 'add2') of the array.
If you could point me in the direction of which Hash or other CakePHP functions are recommended, I'd be happy to take the next step.

Ubercart uc_cart_get_contents get data from standard class

I'm writing a custom module for one of my projects and I'm having a bit of an issue. What I'm trying to do is get a piece of data from uc_cart_get_contents() into a variable so that I can use it in a calculation:
Here's my var_export:
array ( )array ( 0 => stdClass::__set_state(array( 'cart_item_id' => '5', 'cart_id' => '1', 'nid' => '9', 'qty' => '10', 'changed' => '1287074120', 'data' => array ( 'attributes' => array ( 3 => '4', ), 'shippable' => '1', 'restrict_qty' => '0', 'module' => 'uc_product', ), 'title' => 'Chicago Canyon', 'vid' => '9', 'cost' => 1.16, 'price' => 11.16, 'weight' => 0, 'module' => 'uc_product', 'model' => 'chicago-canyon-p-card-p-env', )), )
But what if I want to put the model of each item in the cart into a variable, and based upon that variable adjust the item's price.
If it was an array I would do this:
$items= uc_cart_get_contents();
$model = $items[model];
but that doesn't work, it's a std class. I'm so so lost. Please help!
If your asking how to access object propertiers you do so like this:
$model = $items->model;
for starters, uc_cart_get_contents() returns an array of objects. so, if you did:
$items=uc_cart_get_contents();
you would have to iterate over $items, and then get the specific data elements you want from each item.
for example:
$items = uc_cart_get_contents();
foreach ($items as $item)
{
$model = $item->model;
// whatever ...
}
hope that helps

Drupal 6 forms and optgroup arrays

The following array is produced by converting xml to a array (using xml2array). However its not the exactly the right format I need for a optgroup in a Drupal 6 form.
Array (
[root] => Array ([attr] => Array ([id] => 1) [label] => Array ([value] => My Root)
[node] => Array (
[0] => Array ([attr] => Array([id] => 2) [label] => Array([value] => Category 1)
[node] => Array(
[0] => Array ([attr] => Array ([id] => 14) [label] => Array ([value] => Sub-Category 1))
[1] => Array([attr] => Array ([id] => 15) [label] => Array([value] => Sub-Category2))
I think the array has too be reduced to this format with id values intact for the sub-categories. However I can't confirm this with the drupal docs as they don't mention anything about assigning values to a option.
Array (
[Category 1] => Array(
[14] => Sub-Category 1
[15] => Sub-Category 2
)
)
So my questions are 1) is what is the correct array format for Drupal optgroups with my specified values and 2) how do I reduce my array to match?
1)
The correct format for an option group array is (I will use a list of states as an example):
$state_list = array(
'AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
...
);
This would display a list of the state names, accessible by the state initials as keys.
In order to use subgroups, you would modify the above like:
$state_list = array(
'A'=> array(
'AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
...
),
'C'=> array(
'CA'=>'California',
'CO'=>'Colorado',
...
),
...
);
So, you would need the following format for your translated data as given:
$options = array(
'My Root' => array(
'Category 1' => array(
'14' => 'Sub-Category 1',
'15' => 'Sub-Category 2'
)
)
)
However, if 'My Root' is the name of the select element, use this instead (I'll show you a form definition for each one below):
$options = array(
'Category 1' => array(
'14' => 'Sub-Category 1',
'15' => 'Sub-Category 2'
)
)
Your forms API definition would look like this:
function mymodule_form() {
$options = array(
'My Root' => array(
'Category 1' => array(
'14' => 'Sub-Category 1',
'15' => 'Sub-Category 2'
)
)
);
...
$form['Select Box Name'] = array(
'#type' => 'select',
'#title' => 'Select a Category',
'#options'=> $options,
);
...
return $form;
}
Or like this, if 'My Root' is the name of the select element and not one of it's categories:
function mymodule_form() {
$options = array(
'Category 1' => array(
'14' => 'Sub-Category 1',
'15' => 'Sub-Category 2'
)
);
...
$form['My Root'] = array(
'#type' => 'select',
'#title' => 'Select a Category',
'#options'=> $options,
);
...
return $form;
}
In this definition, your subcategories are keyed using integers - I am not sure this is what you want. Many times Drupal developers use arrays with identical keys and values (e.g. '[CA}=>[CA]') to make things easier.
A side-effect of this format is that 'Category 1' is not itself selectable (since your XML source has keys for both 'Root' and "Category 1' it would seem to me that they were selectable in their previous incarnation); I am not sure if HTML option group sub-category labels even are, so I am not sure if this is a problem or not.
I don't believe that CCK can out of the box do hierarchical options. You could probably reduce to to groups with fields and options but I don't know if that would be generic enough.
As for asssigning values to options that is easier.
From the docs:
Values: An array in the form of array(t('Display value 1'), t('Display value 2')) or array('return_value1' => t('Display Value 1'), 'return_value2' => t('Display Value 2')) if specific return values are required.
So your snippet above would be something like:
$form['Category 1'] = array(
'#type' => 'radios',
'#title' => t('Category 1'),
'#options' => array(
'14' => t('Sub-Category 1'),
'15' => t('Sub-Category 2')
),
);

Categories