Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implode Inside For Loop

Tags:

php

mysql

I want to implode the multi array inside the for loop.

$_POST['PprodName'];

In this $_POST['PprodName'] I have got a value as:

Array ( [0] => steel mj23 [1] => [2] => [3] => [4] => [5] => [6] => [7] => )

steel mj23 is my first product name.

Now I want to check if the element is empty according to their position. so I applied a for loop but don't know how to implode element's which are not empty.

I want to implode only those element which are not empty.

Here is my for loop.

for( $i=0; $i < count($_POST['PprodName']); $i++ ) {
                    if( !empty( $_POST['PprodName'][$i] ) ) {
                        print_r($_POST['PprodName'][$i]);
                    }
                }
like image 872
Someone Avatar asked Feb 10 '23 05:02

Someone


1 Answers

You don't need a for loop. Simply filter all empty values with array_filter() out and then you can simply use implode(), like this:

echo implode(", ", array_filter($_POST['PprodName']));
like image 176
Rizier123 Avatar answered Feb 13 '23 02:02

Rizier123