Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
79 views

Update array inside array

So I've read all the posts and followed along at home. I'm not a CS major. I have an array: {% set testArray = { name: "John Doe", email: "[email protected]", players: [ { ...
stuartmcd69's user avatar
0 votes
2 answers
61 views

How do I only display a list of users that have entries and hide the rest

I want to list users that have created entries in a specific channel. I tried it the Ben Parisak way, but that didn't work out. here's the code that doesn't work (example 1): {% set entryIds = craft....
outline4's user avatar
  • 595
1 vote
1 answer
34 views

Prevent merge from removing data

I have a loop like this: {% set markers = [] %} {% for product in products %} {% for category in product.categories %} {% if category.level == 1 %} {% set data = ...
Tyssen's user avatar
  • 653
0 votes
1 answer
120 views

Add an Object key and value to an exsiting object

I am looping over an array of objects and trying to add a new Key and Value. I keep getting a host of errors so was hoping someone could help a little with formatting this loop. {{ set = transforms: [{...
Wally's user avatar
  • 343
1 vote
1 answer
140 views

How best to merge an array into an array for a relatedTo query parameter?

I have 3 Matrix fields which define optional parameters for building a query: A related Entries field A Categories field A Tags field Any combination of them could be used, including none or all. So ...
Jonathan Schofield's user avatar
0 votes
2 answers
85 views

How to trim each string in an array?

I am looking for how to trim each string in an array, in Craft 3. I would like to check if a string is in an array of strings. The array is from a form submission, a checkbox group. For example, there ...
Kota.M's user avatar
  • 423
1 vote
1 answer
176 views

Import a JSON file that uses arrays

I'm working with a live stock API and want to import the products on a daily basis (using a cron job probably). At this moment I've setup a Feed Me feed as entries. This works for all products where ...
noregt's user avatar
  • 1,062
1 vote
1 answer
58 views

Array manipulation

I have two arrays of entires. One for featured articles and another for the rest. {% set featuredArticles = entry.featuredArticles.article.all() %} {% set articles = craft.entries().section('...
Wally's user avatar
  • 343
1 vote
1 answer
1k views

Twig in array not working as expected

I am trying to get an entry type check on an array {% set landerArray = ['fundingLander','healthLander', 'projectLander', 'publicationLander'] %} {% set pageType = entry.type %} {% if ...
joomkit's user avatar
  • 2,034
1 vote
1 answer
219 views

Asset Custom Field Returning Null in Array

I am relatively new to Craft CMS and I have encountered an issue that I have attempted to debug for quite some time. When using the {% dd %} function to dump my array, a load of my custom fields ...
NodeCreative's user avatar
1 vote
0 answers
101 views

Automatically encode and decode JSON saved to Element

I have a custom element with an array property, which is to be saved to the DB as JSON, and a corresponding query class: use craft\base\Element; class Foo extends Element { public array $columns; ...
Matanya's user avatar
  • 472
1 vote
1 answer
131 views

Remove duplicate text from query results

I have written a query to select all categories for a certain entry. In this case, the category is called "Key Stage" and each one is written "Key Stage 1", "Key Stage 2" ...
Grace's user avatar
  • 55
1 vote
2 answers
800 views

Merging classes in Twig based on component and context

Whats the best way to approach this in twig? So I have a component that is used in multiple places, so within this component I set some default classes like so: {% set navSidebarClasses = { heading: ...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
56 views

in array not matching

I am probably doing something daft here, but tried multiple things to no resolution. I trying to find if a value is in an array but my test case doesn't match. {% if entry.applicationStatus == '...
mmc501's user avatar
  • 1,789
2 votes
1 answer
86 views

Problem with truncated Freeform checkbox group value when mapped to another field

NB: this is not the same as Freeform Checkbox Group Field only returns one value, which was resolved by field name markup invoking an array: name="fieldname[]". I'm already doing that. And ...
Jonathan Schofield's user avatar
1 vote
2 answers
693 views

Checking if a value is in an array of IDS - using a filter?

Is there an easier way to achieve this? Perhaps using a |filter function? {% set variantIds = [] %} {% for variant in product.variants %} {% set variantId = variant.id %} {% set variantIds = ...
Terry Upton's user avatar
  • 1,894
1 vote
2 answers
94 views

Count number of consecutive years in category array

I need to count the number of consecutive years a user has been a member of our organization. For example, if they've been a member from in 2018, 2019, 2020 and 2021, their loyalty will be 4 years. If ...
supazu's user avatar
  • 526
0 votes
1 answer
513 views

Grouping Super Table Results via Twig Multidimensional Array

I'm trying to loop through a Super Table array and group results based on a category ('contribution'). I've gotten pretty far but I'm struggling creating an array in Twig that gives me what I need (I ...
Document Services's user avatar
1 vote
2 answers
29 views

How do I access a specific element of a supersorted array?

Let's say we have this setup: {% set myArray = [9,3,5,7,2,1] %} {% set mySortedArray = myArray | supersort() %} It gives us: myArray = array(6) { [0]=int(9) [1]=int(3) [2]=int(5) [3]=...
Crafty Cat's user avatar
1 vote
1 answer
74 views

Help with an Entries field to an array I can use elsewhere

I have an Entries field so I can select a number of posts to feature in a "Hero" section on the home page, as well as a "most popular" section underneath it. However, the Hero ...
MarkD's user avatar
  • 85
3 votes
2 answers
928 views

How can I tell if a variable is an array or an entry/element query?

We have some components that are used to build pages, like normal, and some where we hydrate them with other data, so we are passing in arrays instead of entries. If something is essentially an ...
Greg Bowne's user avatar
1 vote
1 answer
110 views

Pushing additional key values without overwriting existing key values in an array

For an existing key in an associative array, I'm trying to push a new value to it without overwriting the existing values in the key. I've tried this: {% set myArray = { myKey: [ 'value1',...
user3092's user avatar
  • 117
2 votes
2 answers
214 views

Merge, shuffle, and paginate 😱

I'm trying to get a list of users from two different user groups. One user group (premium) must always show first. Whenever the page is loaded, however, the listing of users should show randomly (...
A Herrera's user avatar
  • 309
1 vote
1 answer
117 views

Error while accessing an associative array

Here is the code: {% if entry_name not in data and entry_total > 0 %} {% set data = data|merge({ (entry_name) : entry_total }) %} {% else %} {% set data.entry_name = 10 %} {% endif %} ...
hvs's user avatar
  • 283
1 vote
1 answer
60 views

Reuse items from array with forloop

I have a loop of a list of regions on a project, this could be an infinite list of areas as my client's customer-base grows. The project branding has four key colours, I have these in an array: {% ...
Jay's user avatar
  • 1,042
1 vote
2 answers
532 views

Impossible to invoke a method on array. How to merge multiple objects and keep as objects

{% set newsletterImages = newsletter.imagesCongratulations|merge(newsletter.imagesCis)|merge(newsletter.imagesSection3) %} {% set transformedImages = newsletterImages %} {% for image in ...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
82 views

Question About Eager Loading Syntax

I am trying to add eager loading to a query. The original query was as follows (‘sticky’ is a custom lightswitch field for marking sticky entries): {% set news = craft.entries.section('news').order('...
Willhaus's user avatar
  • 201
1 vote
1 answer
41 views

Help with correct syntax regarding multisort please

Hi hoping for a quick resolution on this as a lot of scripting relies upon it. I have an array: {% set furniture = [ { 'name': 'chair', 'price': '100' }, { '...
cannon303's user avatar
  • 309
2 votes
1 answer
253 views

Check for number less than x in array

I want to check if there is a number less than 10 in an array. {% if 10 in [0, 30, 50, 10, 9, 200] %} {# checks only if there is a 10 in it. not if there is anything below 10. #} yep. there is ...
KSPR's user avatar
  • 3,786
1 vote
1 answer
87 views

Get random entries from a section but keep the order of these when splitting the array

So I want to get 9 random entries from a section and I do this like so: {% set artists = craft.entries .section('artists') .artistImage(':notempty:') .with(['artistImage']) .orderBy('RAND()') ...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
191 views

Multisort flag doesn't seem to work

The multisort flag for arrays doesn't work the way I'm trying it. This is how I'm using it. {% for conv in conversations|multisort(['postDate'],['SORT_DESC']) %} This is the error I keep getting. ...
kinoli's user avatar
  • 51
1 vote
1 answer
545 views

Get all variants from multiple products

I need to get a list of variants from multiple products, but in a way that allows us to show the results in Craft's handy {% paginate %} tags. So far we have this less-than-appealing markup: {% set ...
Jay's user avatar
  • 1,042
0 votes
1 answer
78 views

Replace quotes in an array

I'm trying to use a charting library within a Craft template. The chart expects an array of numbers that I'm building up from a table but I'm getting an array of strings instead. {% set donutData = [...
Steven Grant's user avatar
  • 1,855
2 votes
2 answers
421 views

Updating Array Variable In Extending Template

I have a layout template: {% set bodyClass = [] %} <body class="{% for class in bodyClass %}{{ class }} {% endfor %}"> {{ block('pageBody') }} </body> And a template that extends it:...
kmgdev's user avatar
  • 1,189
1 vote
2 answers
353 views

Syntax for Multisort Filter

I am trying to use the new multisort filter to sort an array in reverse order using a date key, but can't seem to figure out the correct syntax. Here is what I have: {% for rides in allRides|...
Roi Agneta's user avatar
  • 1,463
1 vote
0 answers
47 views

Product variant association

On my Products details page I have 2 selects. The 1st select list out the Primary variants. Based on the selected Primary variant the 2nd select is auto-completed in with Secondary variants that have ...
Tad Ward's user avatar
  • 317
2 votes
3 answers
695 views

Element-API: Access the first url of an asset array

I have an asset field in my current project with the handle postImage, containing multiple image files. In my element-api endpoint, I wish to access the url of the first file only. I tried the ...
Anthronaut's user avatar
2 votes
1 answer
3k views

Impossible to invoke a method ("limit") on an array... but it's not an array?

I am receiving the error message "Impossible to invoke a method ("limit") on an array," but I don't think I am working with an array! Here is why I think that... First I set an albums variable: {% ...
Steven Thate's user avatar
2 votes
1 answer
53 views

Build array from nodes from RSS where keys contain colons

Probably pure twig: I am building an array from nodes taken from a RSS-feed where one of the keys contains a colon which of course breaks everything. Like so: Node in feed: <media:thumbnail url=...
Marcus Scheller's user avatar
4 votes
1 answer
1k views

Alphabetical index for a list of entries

I would like to build an alphabetical list of craft entries, but group them by the initials of one field of each entry. Thus, getting some kind of alphabetical index, like A Apples Ananas B Beer ...
Urs's user avatar
  • 639
1 vote
1 answer
82 views

Pagination and Entry Model on Homepage

On my site I have some entries with images. In the entry template I paginate the images like so: {% paginate entry.images.limit(1) as images %} My homepage is a single random entry and I want to ...
bstnhnsl's user avatar
  • 123
2 votes
1 answer
190 views

How to display entries filtered from more than one category

I want to try to learn how I can filter entries by multiple categories. I realise similar questions have been asked in the past, but all the answers are for specific use cases. All I want to do is ...
Andrew's user avatar
  • 738
1 vote
2 answers
16k views

How do I target the second item in an array with Twig?

I would like to select the second position in the array below. The problem is that this number is automatically changed. So I try to do this with .index function from Twig but I can not figure it out. ...
Edwin Cornelisse's user avatar
0 votes
1 answer
5k views

Order twig array by date and time

I have a section called "events" in which I have a table field that contains dates and times of these events. So I want to order my events by date and within each day by time. I thought of mergin ...
outline4's user avatar
  • 595
1 vote
2 answers
81 views

Using variable references to fields in a template

I have a situation where Matrix within Matrix would be ideal, but instead we've ended up with a Matrix row that has pairs of fields up to a maximum of 6: quote1 attribution1 … quoteN attributionN … ...
Jonathan Schofield's user avatar
1 vote
1 answer
410 views

Argument #2 is not an array

Here we go: {% set state = craft.categories.group('locations').level(1).title(entry.title).one() %} {% set solution = craft.categories.group('institutionSolutions').title(entry.parent.title).one() %} ...
Mark H.'s user avatar
  • 401
2 votes
2 answers
1k views

How can I iterate through an array of field handles for an entry?

Background: One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign field: {% if entry.courseDesign|length %} <section class="col-...
Katie Fritz's user avatar
5 votes
4 answers
20k views

Overwrite value in object in array

I'm strangely having trouble with this simple Twig merge problem and can't find anything online. Simplified, let's say you had: {% set users = [ {name: 'Bill', age: 25}, {name: 'Tim', age: 30}...
Ryan's user avatar
  • 1,952
1 vote
1 answer
1k views

Returning an array to Twig from a plugin

I'am writing a plugin that has to return an array of data, however Twig does not seem to accept it if I return a PHP array. I don't want to use an explode in Twig to make it into an array. I have ...
Bob van Velzen's user avatar
2 votes
1 answer
254 views

Load entries based on category field value while multiple categories are selected

I have an activity calendar (more like a weekly schedule) which filters per day and per category. Because there are so many activities I can't just load them all in at once so I use some sort of Ajax ...
Justin Dekkers's user avatar