[wpseo_breadcrumb]

How to use multiple condition in one foreach loop?

Solutons:


Use two loops:

foreach ($products as $product) {
    foreach ($categories as $category) {
      //  using $product and $category
    }
}

I had the same question today as the OP. I figured I can split up the problem by first merging the two arrays and then performing the actual foreach loop on the combined array.

This is the idea:

$combined_array = $products + $categories

foreach ($combined_array as $items) {. . .}

Practically speaking, I split the problem into two foreach loops, the first for the merging of the arrays and the second for the actual algorithm.
And indeed it does save me alot of space! Since it is much shorter to append one array to another array than to repeat a complicated foreach algorithm on two separate arrays consecutively.

If your two arrays have the same length, just use a for loop instead and access the elements using the current index.
You just have to make sure, that the indezies are matching in the two arrays.
Its not possible to achieve what you are trying using a foreach loop.

for ($i = 0; $i < count($arr1); $i++) {
    // do whatever you want with $arr1[$i] and $arr2[$i]
}

If your array have the same length, but the indizies may differ and you still want them to be on par, use array_values() before the loop to reindex both arrays.

This will only work for index based arrays!

Related Solutions

What does __all__ mean in Python?

Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py...

Is the linux kernel ported to JavaScript yet?

Javascript is not a systems programming language, it is not appropriate for a kernel. Additionally, the kernel is a very large body of code, and "porting" it to another language is not something that can be done easily, and would likely take years. If the...

How to insert (file) data into a PostgreSQL bytea column?

as superuser: create or replace function bytea_import(p_path text, p_result out bytea) language plpgsql as $$ declare l_oid oid; begin select lo_import(p_path) into l_oid; select lo_get(l_oid) INTO p_result; perform lo_unlink(l_oid); end;$$; lo_get was...

What is the best color combination for on screen reading?

Legibility depends on high contrast between foreground and background, so black-and-white is the safest bet. See for example: Hall RH & Hanna H 2003. The Impact of Web Page Text-Background Color Combinations on Readability, Retention, Aesthetics, and...

MATCH FULL vs MATCH SIMPLE in foreign key constraints

Check the CREATE TABLE page of the manual: There are three match types: MATCH FULL, MATCH PARTIAL, and MATCH SIMPLE (which is the default). MATCH FULL will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null; if...

JavaScript set object key by variable

You need to make the object first, then use [] to set it. var key = "happyCount"; var obj = {}; obj[key] = someValueArray; myArray.push(obj); UPDATE 2021: Computed property names feature was introduced in ECMAScript 2015 (ES6) that allows you to dynamically...

What is the difference between const and readonly in C#?

Apart from the apparent difference of having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the constructor exits. After that it is frozen. const's are implicitly...

How should I index a UUID in Postgres?

Use PostgreSQL's built-in uuid data type, and create a regular b-tree index on it. There is no need to do anything special. This will result in an optimal index, and will also store the uuid field in as compact a form as is currently practical. (Hash indexes in...

Top level domain/domain suffix for private network?

Since the previous answers to this question were written, there have been a couple of RFCs that alter the guidance somewhat. RFC 6761 discusses special-use domain names without providing specific guidance for private networks. RFC 6762 still recommends not...

Getting last modification date of a PostgreSQL database table

There is no reliable, authorative record of the last modified time of a table. Using the relfilenode is wrong for a lot of reasons: Writes are initially recorded to the write-head log (WAL), then lazily to the heap (the table files). Once the record is in WAL,...

How do I make this sed script a “one liner”?

An ANSI C string -- with $'' -- can contain backslash escapes, like \n -- so you can have a newline in sed's arguments while still having the shell command invoking sed be only one line. sed -i $'/INTERPRETER_PYTHON_DISTRO_MAP/,/version_added/ {\n /default/a\\...

How to OCR a PDF file and get the text stored within the PDF?

ocrmypdf does a good job and can be used like this: ocrmypdf in.pdf out.pdf To install: pip install ocrmypdf or sudo apt install ocrmypdf # ubuntu sudo dnf -y install ocrmypdf # fedora After learning that Tesseract can now also produce searchable PDFs, I found...

If …Else If wont work [closed]

First of all, put the javascript code between a <script></script> tag because javascript code will not run in an html <div></div> tag. Then, instead of x == 0||9||2, use x == 0 || x == 9 || x == 2. Kindly indent your code for easier...

How to convert the object of character to string

Your object of characters is already almost an array. It has numeric indices, but is missing the .length property. If you add that it, it will be an "array like" object, which can then be passed to Array.from to get a proper array. Once you have a real array,...

How can I write the approximate value of PI?

Because your { and } is wrong. I think brackets will be as given below If the formula is PI = 4/1 - 4/3 + 4/5 - 4/7 + ... ( Leibniz's Series ) then you can formalate as given below #include <iostream> using namespace std; int main() { double n, i; //...