I want to add key and value form array multidimensional to other array multidimensional. I have 2 array multidimensional, this is:
$data1 = array(
array('id' => 1, 'name' => 'Dani'),
array('id' => 2, 'name' => 'Maachan'),
);
$data2 = array(
array('id' => 2, 'class' => 'Informatics'),
);
I want to join that's 2 array multidimensional to a array multidimensional with "id" in $data1 has some value "id" in $data2 like this:
$dataResult = array(
array('id' => 1, 'name' => 'Dani', 'class' => ''),
array('id' => 2, 'name' => 'Maachan', 'class => 'Informatics'),
);
I hope you can help me, thank you.
This works
$data1 = array(
array('id' => 1, 'name' => 'Dani'),
array('id' => 2, 'name' => 'Maachan'),
);
$data2 = array(
array('id' => 2, 'class' => 'Informatics'),
);
//first lets sort out data2
$new_data2 = array();
foreach($data2 as $d){
$new_data2[$d['id']] = $d['class'];
}
$new_data1 = array();
//now lets create a final array
foreach($data1 as $d){
$d['class'] = array_key_exists($d['id'], $new_data2) ? $new_data2[$d['id']] : '' ;;
$new_data1[] = $d;
}
echo '<pre>';
print_r($new_data1);
echo '</pre>';
Related
I added some item inside cart session(Array) but i want to remove 1 rows for that i wrote following code but that is not working for me.
public function deletecart() {
$this->loadModel("Product");
if($this->Session->check('cart') AND count($this->Session->read('cart'))>0)
{
foreach ($this->Session->read('cart') as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
unset($this->Session->read('cart')[$key]);
}
}
}
}
Here is my session debug value
array(
'[0]' => array(
(int) 0 => array(
'Product' => array(
'id' => '8',
'category' => 'Pendant',
)
)
),
(int) 1 => array(
(int) 0 => array(
'Product' => array(
'id' => '12',
'category' => 'Pendant'
)
)
)
)
You can't unset session key value like this. You will have to store Session key value in a temporary variable.
$sessionArr = $this->Session->read('cart');
foreach ($sessionArr as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
// Unset key
unset($sessionArr[$key]);
}
}
// Assign $sessionArr value to cart key
$this->Session->write('cart',$sessionArr);
I am quite new to drupal 7. I need to add names which come from db table into checkboxes. How do i do that? I have written my callback form function below:
function page_second_callback_form($form){
$result = db_query('SELECT n.names FROM {my_test_tab} n');
$output ='';
foreach($result as $item) {
$output .= $item->names;
}
$opt = array($output => $output,);
$form['check'] = array(
'#type' => 'fieldset',
'#title' => 'some',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['check']['chk_box'] = array(
'#type' => 'checkboxes',
'#title' => 'check box title go here.',
'#required' => TRUE,
'#descrfiption' => 'some descriptions for checkboxes.',
'#options' => $opt,
);
$form['check']['submit'] = array(
'#type' => 'submit',
'#value' => 'Delete',
);
return $form;
}
You need to fill an array of the database options for the checkboxes, then pass this array to the #options property of the checkboxes.
Change your code to be:
$output =array(); // the $output should be an array to store the database values.
foreach($result as $item) {
$output[] = $item->names; // fill the $output array with the database values.
}
Then:
$form['check']['chk_box'] = array(
'#type' => 'checkboxes',
'#title' => 'check box title go here.',
'#required' => TRUE,
'#descrfiption' => 'some descriptions for checkboxes.',
'#options' => $output, // the changed line.
);
I want to set the multi selection to my multi list box.
I have two results in my view page.
$inr1 = 0;
$arr1 = array();
$str_arr = '';
foreach ($result as $rows){
$inr1 = $rows['Employees']['employee_id'];
$arr1[$inr1] = $rows['Employees']['first_name'].' '.$rows['Employees']['last_name'];
$str_arr = $str_arr.$inr1.',';
}
$str_arr = substr($str_arr,0,-1);
//print_r($arr1);
$inr = 0;
$arr = array();
foreach ($options as $option){
$inr = $option['Employee']['employee_id'];
$arr[$inr] = $option['Employee']['first_name'].' '.$option['Employee']['last_name'];
}
//print_r($arr);
echo $this->Form->input('emp_id_one', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'selected' => $arr1,
'style' => 'width:210px; height:125px;',
));
But the values in $arr1 not selected in the list box.
The $arr1 have the selected values.
The $arr have the options to the list.
The problem is that the selction is not working..There have no selection...
How can i do this?
If there is any problem in my code..?
Finally i solved my problem by chaing some code:
Add the one line of code after this $str_arr = substr($str_arr,0,-1);'.
That is....
$str_arr = substr($str_arr,0,-1);
$sel = explode(',',$str_arr);
Then change the name of variable as like this :
echo $this->Form->input('emp_id_one', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'selected' => $sel,
'style' => 'width:210px; height:125px;',
));
Now the values in $sel is selected in the multi list
I have a nested associated model based on
Project=>ProjectDocument=>ProjectDocumentSection=>ProjectDocumentSectionVariable=>Codeset=>Code
I am using containable behaviour to get a specific array with these elements (for a single project).
I have set up associations for these models in their respective models.
Using foreach I am able to interrogate all the data up to an including the ProjectDocumentSectionVariable level.
However, once I try to go to the Codeset level, I get errors.
The pertinent code is:
foreach ($project['ProjectDocument'] as $project_document):
if ($project_document['document_type'] == 1)
{
foreach ($project_document['ProjectDocumentSection'] as $project_document_section):
echo "<h4>" . $project_document_section['title']. "</h4><div><table><th>Variable</th><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th>" ;
foreach ($project_document_section['ProjectDocumentSectionVariable'] as $project_document_section_var):
echo "<tr><td>" . $project_document_section_var['variable_name']
. "</td><td>".$project_document_section_var['variable_description']
. "</td><td>".$project_document_section_var['variable_type']
."</td><td>".$project_document_section_var['variable_format']
."</td><td>".$project_document_section_var['codeset_id'] ;
foreach($project_document_section_var['Codeset'] as $codeset):
foreach($codeset['Code'] as $pcode):
echo $pcode['codevalue'].", ";
endforeach;
endforeach;
echo "</td></tr>";
endforeach;
echo "</table></div>";
endforeach;
}
endforeach;
But I get Illegal string offset 'Code', and Invalid argument supplied for foreach() at the line echo $pcode['codevalue'].", ";
I have looked at the array for $project_document_section_variable and I think there is a problem with the codeset array, but I cannot work out why it is doing this.
Here is an example of some output for the array $project_document_section_variable:
array(
'id' => '32',
'project_id' => '05a',
'project_document_id' => '3',
'project_document_section_id' => '2',
'variable_id' => '2',
'variable_name' => 'Respondent Education',
'variable_description' => 'B1. What is your highest level of education?',
'variable_type' => 'Categorical',
'variable_format' => 'Quantitive',
'codeset_id' => '43',
'Codeset' => array(
'id' => '43',
'title' => 'Educational level',
'Code' => array(
(int) 0 => array(
'id' => '96',
'codeset_id' => '43',
'codevalue' => '1',
'title' => 'No education'
),
(int) 1 => array(
'id' => '97',
'codeset_id' => '43',
'codevalue' => '2',
'title' => 'Primary'
),
(int) 2 => array(
'id' => '98',
'codeset_id' => '43',
'codevalue' => '3',
'title' => 'Secondary'
),
(int) 3 => array(
'id' => '99',
'codeset_id' => '43',
'codevalue' => '4',
'title' => 'Tertiary'
),
(int) 4 => array(
'id' => '100',
'codeset_id' => '43',
'codevalue' => '7',
'title' => 'Other (describe)'
),
(int) 5 => array(
'id' => '101',
'codeset_id' => '43',
'codevalue' => '8',
'title' => 'Refuses to answer'
),
(int) 6 => array(
'id' => '102',
'codeset_id' => '43',
'codevalue' => '9',
'title' => 'Don’t know'
)
)
)
)
You may notice that the codeset array does not have an (int) 0. perhaps this is because only one array is returned for codeset (which is correct, each project_document_section_variable has only one associated codeset.
Is there something blindingly obvious I am missing here?
This line -> foreach($project_document_section_var['Codeset'] as $codeset): is making php assign ['id'], ['title'], and ['Code'] to $codeset.
This line -> foreach($codeset['Code'] as $pcode): is making php looks for ['id']['Code'], ['title']['Code'], and ['Code']['Code'] which don't exists and raise the error.
Try replacing this:
foreach($project_document_section_var['Codeset'] as $codeset):
foreach($codeset['Code'] as $pcode):
echo $pcode['codevalue'].", ";
endforeach;
endforeach;
For this:
foreach($project_document_section_var['Codeset']['Code'] as $pcode):
echo $pcode['codevalue'].", ";
endforeach;
I used to have a function to create a custom menu by loading all terms from a specific vocab at drupal 6:
function _taxonomy_top_links($vid = NULL) {
$terms = taxonomy_get_tree($vid);
$taxos = array();
foreach ($terms as $term) {
$taxos[] = array('title' => $term->name, 'taxonomy/term/' . $term->tid, 'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)));
}
return theme('links', $taxos, array('id' => 'menu-'. $vid, 'class' => 'menu clearfix'));
}
This doesn't work at drupal 7 which I guess related to the new field api. How do you grab all terms from a specific vocab to preprocess at page level?
Thanks for any help.
Most of your code should actually work fine, it's the theme part that is incorrect.
$terms = taxonomy_get_tree($vid, 0, NULL, TRUE);
$links = array();
foreach ($terms as $term) {
$uri = entity_uri('taxonomy_term', $term);
$link = array(
'title' => $term->name,
'href' => $uri['path'],
'attributes' => array('rel' => 'tag'),
);
$link += $uri['options'];
if (!empty($term->description)) {
$link['title'] = strip_tags($term->description);
}
$links['tid-' . $term->tid] = $link;
}
$variables = array(
'links' => $links,
'attributes' => array(
'id' => 'menu-' . $vid,
'class' => array('menu', 'clearfix'),
),
);
return theme('links', $variables);