I am trying to get the value of various fields from taxonomy terms (and I'd like to do it the right way, i.e. not $term->field_foo['und'][0]['value'])
I'm able to do this reliably for nodes and adapted my method for taxonomy terms, but it doesn't seem to be working. Here is my code:
$field = field_get_items('taxonomy_term', $term, 'field_foo');
$value = field_view_value('taxonomy_term', $term, 'field_foo', $field[0]);
$rendered = render($value);
In troubleshooting this, I can see that field_get_items correctly returns an array... if I insert a var_dump (of $field) after the first line there, i get this:
array (size=1)
0 =>
array (size=1)
'value' => string '1' (length=1)
Yet field_view_value returns an empty string... again a var_dump (of $value) after the second line results in this:
array (size=2)
'#markup' => string '' (length=0)
'#access' => boolean true
Can anyone see where I'm going wrong?
You can use the Entity Metadata Wrapper:
$term = entity_metadata_wrapper('taxonomy_term', TERM_ID);
Simpler and reliable.
Related
I'm using a sphinx bundle (timewasted SphinxSearchBundle) in a symfony 2 app (https://github.com/timewasted/Search-SphinxsearchBundle) (based on the PHP API)
it works great as long as I don't try to use the SPH_MATCH_EXTENDED.
Here's a code sample
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED);
$searchResults = $sphinxSearch->search("#typemesure_id 2", $index);
But the result is always empty, and it seems that my field (#typemesure_id) is considered as a word :
'words' =>
array
'typemesure_id' =>
array
'docs' => string '0' (length=1)
'hits' => string '0' (length=1)
2 =>
array
'docs' => string '4169' (length=4)
'hits' => string '5714' (length=4)
Does anyone konws whats wrong in my request ?
Can anyone post an exemple of working extended query working with this bundle?
My goal is to get a condition like "#(user1,user2,user3) 15"
Thanks for your help
Notes:
the SPH_MATCH_ALL & SPH_MATCH_ANY are working fine.
The setFilter() method is also working
Looking at the function defition...
public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
It will automaticlly escape the query, so the # will be getting escaped. Need to pass false as fourth param
I have an item object in PHP, which has the following structure on var_dump :
$item->properties:
array (size=1)
1 =>
array (size=4)
'Key1' => string 'Value1' (length=6)
'Key2' => int 1
'Key3' => string 'true' (length=4)
'Key4' => string 'true' (length=4)
I want to access Key, value in a foreach loop and assign Key, value pair to some internal variables, however when i am using the foloowing code to loop pver array of array, i am getting error in accessing the values in the way i want. Here is what i am doing :
foreach($item->properties as $property) {
foreach($property as $value) {
echo $value;
}
}
Anyone have an idea what am i doing wrong, and how can i fix that ?
one of the things you provide to the foreach isn't a a valid argument, as the error says. Find out which of the 2 it is (linenumber) and var_dump that argument to see what type it is (probably "not an array" ).
In the end either $item->properties itself, or the array values of that array (if it is one), so $property is not an array.
It could be, for instance, that maybe the first key of the properties IS an array, but the second isn't? then you could use is_array to check.
When trying to access an array inside an array, only NULL is output.
My Code:
$aStats = array();
$aStats['hd'] = array();
$aStats['hd'][] = array
(
'dev' => $device,
'total' => $total,
'used' => $used,
'free' => $free,
'used_perc' => $used_perc,
'mount' => $folder
);
echo $aStats['hd']['free'];
When using json_encode, the values are displayed correctly:
die( json_encode( $aStats ) );
Where is my mistake?
Replace these lines:
$aStats['hd'] = array();
$aStats['hd'][] = array
With this:
$aStats['hd'] = array
You appear to be accessing your array ($aStats['hd']['free'];) as if the value of hd is an associated array, but using [] creates a new integer index in the array, and stores the value in that index. Joe Walker's answer shows what happens instead, that you have an associative array pointing to an indexed array pointing to another associative array, rather than the associative to associative array you suggest you're trying to use in your echo statement.
This is a practical tip that will let you find out where is the issue easly, all you need to do is:
var_dump($aStats);
This will output:
array (size=1)
'hd' =>
array (size=1)
0 =>
array (size=6)
'dev' => string 'SomeDevice' (length=10)
'total' => string '10000' (length=5)
'used' => boolean true
'free' => boolean false
'used_perc' => string 'none' (length=4)
'mount' => string '/some/directory/here/' (length=21)
Now you know you can access this element using
$aStats['hd'][0]['free'];
This will return null in your question because your variables are not yet initialized, but I guess you do have them initialized in your code, hope this helps.
I have an object that needs to be returned, however I need to perform some pre-return manipulation before returning it.
The object has the following format:
object(PaginationHelper)[3]
public 'current_page' => int 1
public 'items_per_page' => int 10
public 'dataset' =>
array (size=10)
0 =>
object(AdvertSet)[4]
public 'Keywords' => string '' (length=0)
protected 'Adverts' =>
array (size=3) // SIZE = 3 SO REMOVE THIS FROM 'dataset' ARRAY
...
public 'LiveStatus' => boolean false
1 =>
object(AdvertSet)[5]
public 'Keywords' => string '' (length=0)
protected 'Adverts' =>
array (size=1) // SIZE = 1 SO KEEP THIS IN 'dataset' ARRAY
...
public 'LiveStatus' => boolean false
etc etc ....
[End Object]
What I need to do:
Remove all parts of the 'dataset' array that doesn't have an 'Adverts' count of 1, thereby preserving only those datasets that have an 'Adverts' array size of 1.
Retain the fact that it is an object, to be returned.
I've tried multi-dimensional recursive functions to get through this, however the fact that it's an object and not an array is making progress hard, and I'm not sure I would be able to convert from an object to an array and back again without messing up the object's internals.
Can anyone help with this? Here's what I've gotten so far with a foreach...
foreach($results as $key => $value) {
if($key == 'dataset') {
// value is right array to check count
foreach($value as $k => $v) {
echo $v;
}
}
}
It doesn't work, but that's the method I'm currently working on.
I've also tried something like:
if(count($results->dataset->(Array)AdvertSet->Adverts == 1) { }
but I can't cast AdvertSet as Array.. Any help would be greatly appreciated!
Just a quick note: it doesn't have to be removed from the array, I just eventually need the same object without those that have an Adverts count of 3. So this could involve copying to a new array without those that have an Adverts count of <> 1.
My first thought was:
foreach($PaginationHelper->dataset as &$data) {
if(count($data) !== 1)
unset($data);
}
But after reading your question for the third time, I see you want to remove only those elements with a Adverts count not equal to 1.
Looking at your structure, the Adverts array is protected, and therefore there is now way to access it without subclassing Advertset object.
So, my final answer must be: It is not possible to remove them, with this structure!
Your data structure is not really recursive and you don't need recursive traversal.
You only need to iterate over the $object->dataset array and delete items where the count of adverts is not 1. Since you're trying to filter items over a protected property, one approach would be to implement a AdvertSet::count() method that would return number of contained adverts: $object->dataset[$i]->Adverts->count() != 1. I would advise against forcing your way to access the protected property just for the filtering's sake.
I'm total newbie to PHP and Drupal but I fix this simple(?) thingo on my template. I want to get title, date, text and link path from this array? I can't get it out of there. I think its because its in inside of another array and because im noob in PHP I cant get it out of there ? Also I would like to get it to a loop. If theres more content, I would get it as a list or something. I would use foreach for this? Like foreach $contents as $content and so on?
I get this output from this: var_dump($contents);
array
'total' => string '1' (length=1)
'data' =>
array
0 =>
array
'cid' => string '13231' (length=3)
'title' => string 'TITLEBLABLABLA' (length=3)
'body_text' => string 'TEXTBLABLABAL' (length=709)
'created' => string '313131' (length=10)
'created' => string '2010-07-13 14:12:11' (length=19)
'path' => string 'http://goog.fi' (length=72)
Think of accessing multidimensional arrays in the same way you'd access a file in a subdirectory: just reference each level/directory in sequence. Assuming that array is stored in $arr, you'd get the title as follows:
$arr['data']['0']['title']
Here is a basic PHP array tutorial
It comes down to this:
You retrieve an element of an array with []
$array = array( 'a' => 'b');
echo $array['a']; //Prints b;
To loop through a multi-dimensional array and echo date try this...
foreach ($contents as $key => $content) {
echo $content[$key]['created'];
}
If that doesn't work, post the output of print_r($content) in your question so I can't build you exact array. I'm kinda confused over the structure of your array in your question.