How to encode every 3 elements in a PHP array?

I have the following PHP array:

$array = array( 'uname', '=', 'lizabd', 'pass', '=', '225555') 

now i want to loop every 3 elements like this way

  uname = lizabd pass = 225555 

How can i do this?

I know that I can slice it using this for the first element:

  $slice = array_slice($array, 3); 

But how to do it using Loop?

+5
source share
1 answer

You need to use array_chunk ()

 $array = array_chunk($array,3); foreach($array as $arr){ echo implode(' ',$arr); echo PHP_EOL; //for new line, you can also use echo '<br>'; } 

Exit: - https://eval.in/970842

Link: - implode ()

+6
source

Source: https://habr.com/ru/post/1275856/


All Articles