Questions tagged [array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
5
votes
2answers
32k views

Is there a way to check if a value exists in an array in twig?

Done some searching on this but can't quite find what I'm looking for. Most threads are regarding keys. I want to know if there is a way to test if a value exists in a twig array. It should be able ...
2
votes
2answers
200 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 ...
1
vote
2answers
75 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: ...
1
vote
1answer
17 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 == '...
2
votes
1answer
32 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 ...
1
vote
2answers
44 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 = ...
1
vote
2answers
47 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 ...
0
votes
0answers
13 views

ArrayHelper::stringToArray creates sometimes index array

It looks like the craftcms 2 ArrayHelper created indexed array on large strings. But I'll need a none indexed array. Any ideas what I'll do wrong? Running on php 7.1. $playerIds = ArrayHelper::...
1
vote
2answers
479 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 ...
12
votes
4answers
8k views

What is the best way to exclude an array of entries?

I have done a "Is featured" switch on articles which works great. I can list the latest 4 featured articles but after that I would like to list the others (Of course they may be featured too)...
5
votes
4answers
15k 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}...
1
vote
1answer
143 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 ...
1
vote
2answers
21 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]=...
1
vote
1answer
42 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 ...
1
vote
1answer
33 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',...
2
votes
2answers
111 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 (...
1
vote
1answer
49 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 %} ...
1
vote
1answer
52 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: {% ...
1
vote
2answers
223 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 ...
1
vote
1answer
56 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('...
1
vote
1answer
34 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' }, { '...
1
vote
2answers
227 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|...
2
votes
1answer
143 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 ...
1
vote
1answer
35 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()') ...
1
vote
1answer
34 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. ...
1
vote
1answer
379 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 ...
2
votes
2answers
298 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:...
0
votes
1answer
37 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 = [...
1
vote
0answers
38 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 ...
2
votes
1answer
41 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=...
1
vote
1answer
2k 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: {% ...
3
votes
1answer
678 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 ...
1
vote
1answer
58 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 ...
2
votes
1answer
167 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 ...
6
votes
3answers
2k views

How do I find live and pending entries when using an array?

I have this in my listings template, but the 'pending' parameter does not seem to work. {% set urlCat = craft.request.getSegment(3) %} {% set category = craft.categories.slug( urlCat ) %} {% set ...
1
vote
2answers
10k 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. ...
0
votes
1answer
4k 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 ...
1
vote
2answers
51 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 … ...
1
vote
1answer
362 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() %} ...
2
votes
2answers
963 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-...
2
votes
2answers
64 views

Can I exclude a commerce address by ID when retrieving a list of addresses?

I would like to retrieve a list of all commerce customer addresses except for one. When working with entries, this can be done with the {id: 'not ' ~ some.id } parameter. This doesn't seem to work ...
4
votes
1answer
95 views

Store categories in array by level in controller

I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group. So I used the following code to ...
1
vote
1answer
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 ...
2
votes
1answer
231 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 ...
2
votes
1answer
717 views

Eager-load and pagination

I'm currently working on a relation-heavy website with a colleague of mine and, for that reason, eager-loading stuff looks very appealing to us. We've succeeded in eager-loading pretty much everything ...
1
vote
2answers
192 views

Recursive array search

I've got a plugin where I've written a simple recursive array search to find an array within a nested/multidimensional array which contains a certain value. private function arraySearch($haystack,$...
1
vote
0answers
97 views

Create a baseball standings table

I am building a baseball standings table. I currently have a Single section setup in Craft to handle some basic info. It contains a table field with 3 rows: Team, Wins and Losses. I am able to output ...
2
votes
1answer
267 views

Remove duplicated entries from SuperTable related entries

Anyone able to assist me with removing duplicates in this example? So I have a related entries (cables) setup in a SuperTable. Some of these entries are duplicated across different rows. So a cable ...
2
votes
1answer
1k views

Iterate through all entries and count a specific value

I am having entries in a Craft channel-section. Each entry has the field "type" with one category filled. I need to iterate through the entries and count the amount of each category. Example: Entry ...
1
vote
1answer
551 views

How to replace a html entity within script tags using Craft replace

Apologies if this has been asked before, I can only find questions dealing with the reverse scenario (i.e. replacing special characters with html entities) I need to output special characters as part ...