I'm not sure if there is some kind of php function that will help me do this fairly simply or not. I figured I'd ask.
Let's say I have 5 products [prod1, prod2, prod3, prod4, prod5]
All of these products are related to eachother, so I need to arrive at something like this:
prod1, prod2, prod3, prod4, prod5
prod2, prod3, prod4, prod5, prod1
prod3, prod4, prod5, prod1, prod2
prod4, prod5, prod1, prod2, prod3
prod5, prod1, prod2, prod3, prod4
echo, save as variables, it doesn't matter to me.
In my example I said 5, but in reality there could be any number of products. Is there a function that does this automatically up to n
products?? I don't even know what to really call this other then I'm matching them together.
iterate for-in loop over the array elements. Each iteration, matches first array elements to second array elements using indexOf() method, if they match then push elements into arr array. Sort arr array elements in ascending order and as well as return sorted array list.
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.
You can do this:
$arr = array($prod1, $prod2, $prod3, $prod4, $prod5);
for ($i = 0; $i < count($arr); $i++) {
array_push($arr, array_shift($arr));
print_r($arr);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With