I want to print all results of the taxonomy_vocabulary_11 array (this is a Drupal 7 site).
If I use <?php print render($content['taxonomy_vocabulary_11'][0]['#title']); ?> I get only one result.
I´ve usuccessfully tried
<?php foreach ($content->taxonomy_vocabulary_11 as $key => $value): $terms = $value['#title']; ?>
<?php print $terms; ?>
<?php endforeach?>
I get this error: Notice: Trying to get property of non-object
Now, this is the output I get using devel module (dpm($node);)
(object) array(
'vid' => '5178',
'uid' => '1',
'title' => 'PROYECTO',
'log' => '',
'status' => '1',
'comment' => '2',
'promote' => '0',
'sticky' => '0',
'nid' => '155',
'type' => 'jornadas',
'language' => 'und',
'created' => '1095048000',
'changed' => '1360589684',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1360589684',
'revision_uid' => '1',
'taxonomy_vocabulary_4' => array(),
'taxonomy_vocabulary_6' => array(
'und' => array(
array(
'tid' => '33',
'taxonomy_term' => (object) array(
'tid' => '33',
'vid' => '6',
'name' => 'Jornadas Gratuitas',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_6',
),
),
),
),
'taxonomy_vocabulary_7' => array(
'und' => array(
array(
'tid' => '40',
'taxonomy_term' => (object) array(
'tid' => '40',
'vid' => '7',
'name' => 'Para PH',
'description' => '',
'format' => NULL,
'weight' => '-10',
'vocabulary_machine_name' => 'vocabulary_7',
),
),
),
),
'taxonomy_vocabulary_11' => array(
'und' => array(
array(
'tid' => '262',
'taxonomy_term' => (object) array(
'tid' => '262',
'vid' => '11',
'name' => 'colegios',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_11',
),
),
array(
'tid' => '543',
'taxonomy_term' => (object) array(
'tid' => '543',
'vid' => '11',
'name' => 'derecho',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_11',
),
),
),
),
'body' => array(
'und' => array(
array(
'value' => "
I´ve also tried <?php print render($content['taxonomy_vocabulary_11']); ?> to treat the taxonomy as any other field, but it won´t print anything.
Note: If I go and do print $content['taxonomy_vocabulary_11']; it just print the word array.
What´s wrong in my approach?
Try:
foreach ($content['taxonomy_vocabulary_11'] as $tv11) {
print render($tv11['#title']);
}
I dont use drupal but this should work if i understood how the $content['taxonomy_vocabulary_11'] array is structured.
Just in case anyone needs it:
<?php
$vid = 11; //vocabulary id
$nid = $node->nid; //it looks for the current loaded node
$query = "SELECT tid, name
FROM (
SELECT td.tid AS tid, name
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
AND n.nid = ".$nid."
GROUP BY td.tid
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
echo l($term->name, "taxonomy/term/$term->tid") . ', ';
}
?>
Related
array (
0 =>
WC_Memberships_User_Membership::__set_state(array(
'id' => 52330,
'plan_id' => 18952,
'plan' =>
WC_Memberships_Membership_Plan::__set_state(array(
'id' => 18952,
'name' => 'Level 2 Access Until June',
'slug' => 'level-2-access-until-june',
'post' =>
WP_Post::__set_state(array(
'ID' => 18952,
'post_author' => '8',
'post_date' => '2017-09-11 11:05:13',
'post_date_gmt' => '2017-09-11 15:05:13',
)),
'access_method_meta' => '_access_method',
'rules' =>
array (
),
)),
'user_id' => '5213',
'status' => 'wcm-active',
'post' =>
WP_Post::__set_state(array(
'ID' => 52330,
'post_title' => 'Auto Draft',
)),
'product' => NULL,
'renewal_login_token_meta' => '_renewal_login_token',
'locked_meta' => '_locked',
)),
1 =>
WC_Memberships_User_Membership::__set_state(array(
'id' => 28639,
'plan_id' => 27786,
'plan' =>
WC_Memberships_Membership_Plan::__set_state(array(
'id' => 27786,
'name' => 'Level 1 – Ethical and Professional Standards',
'slug' => 'level-1-ethical-and-professional-standards',
'post' =>
WP_Post::__set_state(array(
'ID' => 27786,
'post_author' => '8',
'post_mime_type' => '',
'comment_count' => '0',
'filter' => 'raw',
)),
'access_method_meta' => '_access_method',
'email_content_meta' => '_email_content',
'rules' =>
array (
),
)),
'user_id' => '5213',
'status' => 'wcm-active',
'post' =>
WP_Post::__set_state(array(
'ID' => 28639,
'post_author' => '5213',
'post_date' => '2017-11-27 21:41:06',
'filter' => 'raw',
)),
'product' => NULL,
'type' => 'manually-assigned',
'locked_meta' => '_locked',
)),
);
I have this array given by woocommerce method: wc_memberships_get_user_active_memberships($user_id);
I need to extract out the name of the memberships the user has, it must be in a loop as the user can have multiple memberships. I am looking to get back name = Level 2 Access Until June, Level 1 – Ethical and Professional Standards
From what you've posted, I would try this:
$memberships_info = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships_info as $membership) {
echo $membership['plan']['name'];
}
From the woocommerce docs, it looks like you can specify what status of active memberships. So you might want to do that to include all membership types:
$args = array(
'status' => array( 'active', 'complimentary', 'pending' ),
);
$memberships_info = wc_memberships_get_user_active_memberships($user_id, $args); // adding the args bit to get all memberships
foreach ($memberships_info as $membership) {
echo $membership['plan']['name'];
}
Woocommerce docs: https://docs.woocommerce.com/document/woocommerce-memberships-function-reference/
This is what finally worked for me.
$user_id = get_current_user_id();
$memberships = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships as $membership => $val) {
echo $val->plan->name . '<br>';
};
Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays
$arrayToView is the info of every single user that i want.
$tagsArray are only tags that use every user but i need to merge all the info something like the last array...
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
)
And i have another one like this
$tagsArray= array(
'IVOFACUNDO' = array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' = array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
so the question is how i can merge both arrays obviously respectively with the same admin something like this
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23,
'tags' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44,
'tags' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
)
The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?
You can try this snippet.
foreach($arrayToView as $key => $arr){
if(array_key_exists($key, $tagsArray)){
$arrayToView[$key]['tags'] = $tagsArray[$key];
}
}
echo '<pre>';print_r($arrayToView);echo '</pre>';
Use php inbuilt function
$result_Arr = array_merge_recursive($arrayToView,$tagsArray);
<?php
$arrayToView = array(
'IVOFACUNDO' => array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' => array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
);
$tagsArray= array(
'IVOFACUNDO' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
);
foreach($arrayToView as $key => $value){
if(isset($tagsArray[$key])){
$arrayToView[$key]['tags'] = array();
foreach($tagsArray[$key] as $key2 => $value2){
$arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
}
}
}
echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>
I have Story related to a Chapter with a many to many relation
I have a StoryChapter Model .
I have this find all stories result :
array(
(int) 0 => array(
'Story' => array(
'id' => '111',
'title' => 'First Story',
'question' => 'What do you want ?',
'description' => 'ezrsrfgq ergtqergq',
'date' => '2014-06-10',
'image' => '/uploads/stories/111.jpg',
'created' => '2014-06-10 07:51:35',
'modified' => '2014-06-13 12:45:43',
'created_by' => '1',
'original' => null,
'tags' => ''
),
'StoryChapter' => array(
(int) 0 => array(
'id' => '110',
'story_id' => '111',
'chapter_id' => '81',
'chapter_title' => 'Second Chapter',
'created' => '2014-06-11 00:00:00'
),
(int) 1 => array(
'id' => '109',
'story_id' => '111',
'chapter_id' => '80',
'chapter_title' => 'First Chapter',
'created' => '2014-06-13 00:00:00'
)
),
'StoryUser' => array(),
'StoryGroup' => array(),
'Favorite' => array(),
'Tag' => array()
),
(int) 1 => array(
'Story' => array(
'id' => '112',
'title' => 'Second Story',
'question' => 'What do you want ?',
'description' => 'edghs rthsghsx ghs rhsgrhsrtgh',
'date' => '2014-06-13',
'image' => '/uploads/stories/112.jpg',
'created' => '2014-06-13 07:43:18',
'modified' => '2014-06-13 07:43:18',
'created_by' => '1',
'original' => null,
'tags' => ''
),
'StoryChapter' => array(),
'StoryUser' => array(),
'StoryGroup' => array(),
'Favorite' => array(),
'Tag' => array()
)
)
I want the find function to order only the StoryChapter by created desc without affecting the order of the found stories .
I hope you understand what I mean .
Thank you
I solved the problem by adding the order in the hasMany array in the Story model
public $hasMany = array(
'StoryChapter' => array(
'className' => 'StoryChapter',
'foreignKey' => 'story_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => 'created ASC',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;
I have a relationship where a quote habtm applicants. I am trying to get a quote to save with multiple applicants at once. I already have an array of the applicants I need but I don't know how to format that array to get it to save when I insert it into the quote array.
The applicant array looks like this:
array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
)
I need one that looks like this:
array(
'Applicants' => array(
'id' => 435,
'id' => 436,
'id' => 66
)
)
How might I go about doing this?
Or is there a better way?
When I save a quote the array looks like this:
array(
'QuoteButton' => 'Submit',
'Quote' => array(
'date' => array(
'day' => '13',
'month' => '10',
'year' => '2013'
),
'description' => '',
'quote_accepted' => '0',
'research_accepted' => '0',
'cc_accepted' => '0',
'pesel_accepted' => '0',
'setfees_accepted' => '0',
'total' => '0'
),
'Applicant' => array(
'id' => '66'
),
How do I insert more than one applicant into the array?
An array can't have the same id, but can crate another array like this:
$datas = array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
);
$ids = array();
foreach($datas as $data => $applicants) {
$ids[] = $applicants['Applicant']['id'];
}
print_r($ids);
Output:
Array ( [0] => 436 [1] => 435 [2] => 66 )
How to use the ids? Like this:
foreach($ids as $key => $id) {
// do whatever you want with the applicant id
}
like tttony pointed out, array indexes need to be unique so i would:
$ids = array();
foreach($array as $applicant)
$ids[$applicant['Applicant']['id']] = null;
which will output:
Array
(
[436] =>
[435] =>
[66] =>
)
now id is key to $ids array...you could add something else as value rather than null