Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EbrahemSamer's avatar

How to iterate though array of arrays using PHP ?

I've array of arrays say and I wanna combine each element with every element:

[ [ahmed, omar , ali],

[mohammed, sayed],

[mostafa, ali],

]

I want the result to be like that [

'ahmed mohammed mostafa',

'ahmed mohammed ali',

'ahmed sayed mostafa',

'ahmed sayed ali'

...... same thing with omar and ali

]

need way or algorithms to make it, thanks

0 likes
7 replies
Tray2's avatar

Something like this?

$arr = [ 
  ['ahmed', 'omar' , 'ali'],

  ['mohammed', 'sayed'],

  ['mostafa', 'ali'],
];


$arr2 = array_map('implode_arr', $arr);


function implode_arr($value) {
	return implode(', ', $value);
}

Gives this

Array
(
    [0] => ahmed, omar, ali
    [1] => mohammed, sayed
    [2] => mostafa, ali
)
1 like
EbrahemSamer's avatar

@tray2 I do not want that I want each element at every array with each element at others

latwelve's avatar

If you tidy up your question it may help with an answer

helgesverre's avatar

Not quite sure how I would do this, but if you want to do some googling by yourself, what you're looking for is called "finding all permutations", so i would try looking for keywords like "php all permutations of array" or similar phrases.

latwelve's avatar

Check your questions code formatting is all I'm saying, I didn't understand it until helgesverre replied.

The link I posted should solve your query

Please or to participate in this conversation.