PHP implode: converting one-dimensional and multi-dimensional arrays to strings, sorting using numeric indices.

The popularity of the php implode function speaks of its extreme usefulness and many advantages even over the usual string concatenation. It provides sufficient flexibility for coding and multi-use.

Programming considerations when using string concatenation

If the text is combined using concatenation, then we are forced to adapt when writing code to the strict sequence with which this text should be collected. Let's look at a simple example.

The program will help managers to consider the availability of goods in the warehouse of the store. If the product is finished or almost finished, the "Order" button will appear.

We are connected to the database.

$hostname="localhost"; $username="root"; $password=""; $dbname="my_database"; $usertable="my_table"; $yourfield = "product"; $con = mysqli_connect($hostname,$username, $password); mysqli_select_db($con, $dbname); mysqli_set_charset($con,"utf8"); $query = "SELECT * FROM $usertable"; $result = mysqli_query($con, $query); mysqli_close($con); 

Database Content:

MySql database

To get started, let's assemble the form using the usual string concatenation:

 $form = ''; $form .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; if($result){ while($row = mysqli_fetch_assoc($result)){ $form .= "<div>"; foreach($row as $key_product =>$value_product){ if($key_product >= 38 ){ if( $value_product == 1 ){ $form .= "<input type='number' name='sale_".$row['id'].'_'.$key_product."' min = '1' max = '".$value_product."' id='sale_".$row['id'].$key_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' >".$key_product.": ".$value_product." </label><br><br>"; $form .= "<input type='number' name='zakaz_".$row['id']."_".$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #ccc;' ><label for='zakaz_".$row['id'].$key_product."'>".$key_product.": ".$value_product." </label><br><br>"; }else if( $value_product == 0 ){ $form .= "<input type='number' name='zakaz_".$row['id']."_".$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #bbb;' ><label for='zakaz_".$row['id'].$key_product."'>".$key_product.": ".$value_product." </label><br><br>"; }else{ $form .= "<input type='number' name='sale_".$row['id'].'_'.$key_product."' id='sale_".$row['id'].$key_product."' min = '1' max = '".$value_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' >".$key_product.": ".$value_product." </label><br><br>"; } }else{ $form .= $key_product.": ".$value_product."<br>"; } } $form .= "</div><br>"; } } echo $form .= '<input type="submit" value="submit"></form>'; 

This method requires a strict recording sequence; if you need to change one line for another, you need to rewrite the code.

In the table we see product information obtained from the database:

Build html form using implode php

In this form, it is difficult to figure out which product to order and which to sell, so concatenation is not suitable for large amounts of information.

Flexibility and multiple use of the implode () function

By writing strings to an array and then concatenating them with php implode, we get a lot more options for writing code.

We rewrite our code: delete the concatenation, instead the lines will be entered into the array and collected by the implode () function:

 $form = ''; $form .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; if($result){ while($row = mysqli_fetch_assoc($result)){ foreach($row as $key_product =>$value_product){ $form_product[$key_product] = ''; if($key_product >= 38 ){ if( $value_product == 1 ){ $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' min = '1' max = '".$value_product."' id='sale_".$row['id'].$key_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #ccc;' ><label for = 'zakaz_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else if( $value_product == 0 ){ $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #bbb;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else{ $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' id='sale_".$row['id'].$key_product."' min = '1' max = '".$value_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; } }else{ $form_product[$key_product]= $key_product.": ".$value_product."<br>"; } $form_row[$row['id']] = implode('', $form_product); } } } echo $form .= implode('',$form_row).'</form>'; 

Suppose we need another separate table for managers, where the cells "Order" and "Sell" must be separate. Thanks to php array implode we can manipulate strings. Add additional arrays and create another shape:

 $form_sklad=' '; $sale = array(); $zakaz = array(); $form_sklad .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; $sale[$key_product] = $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' min = '1' max = '".$value_product."' id='sale_".$row['id'].$key_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; $zakaz[$key_product] = $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #ccc;' ><label for = 'zakaz_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; $form_row_zakaz[$row['id']] = implode('', $zakaz); $form_row_sale[$row['id']] = implode('', $sale); echo $form_sklad .= implode('', $form_row_zakaz).implode('', $form_row_sale).'</form>'; 

We collect the same lines in the second form ($ form_sklad). We place the order in the $ zakaz array, and sell in the $ sale array. Consequently, each unit of goods will appear either in the $ sale array or in $ zakaz. A unit of goods is one pair of shoes of a certain size (from 38 to 42). One row ($ row ['id']) in the database contains the goods of the same model, but of different sizes, so we further combine with php implode all the units of goods of the same model into $ form_row_zakaz and $ form_row_sale arrays. If one pair of shoes of the same size remains, then such a product will appear both in the $ zakaz array (the goods end) and in the $ sale array (sell the last pair of this size).

Thus, there is no need to create another loop and rewrite the lines again and again.

All together looks like this:

 $form = ''; $form_sklad=' '; $sale = array(); $zakaz = array(); $form .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; $form_sklad .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; if($result){ while($row = mysqli_fetch_assoc($result)){ foreach($row as $key_product =>$value_product){ $sale[$key_product] = $zakaz[$key_product] = $form_product[$key_product] = ''; if($key_product >= 38 ){ if( $value_product == 1 ){ $sale[$key_product] = $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' min = '1' max = '".$value_product."' id='sale_".$row['id'].$key_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; $zakaz[$key_product] = $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #ccc;' ><label for = 'zakaz_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else if( $value_product == 0 ){ $zakaz[$key_product] = $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #bbb;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else{ $sale[$key_product] = $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' id='sale_".$row['id'].$key_product."' min = '1' max = '".$value_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; } }else{ $form_product[$key_product]= $key_product.": ".$value_product."<br>"; } $form_row[$row['id']] = implode('', $form_product); $form_row_zakaz[$row['id']] = implode('', $zakaz); $form_row_sale[$row['id']] = implode('', $sale); } } } echo $form .= implode('',$form_row).'</form>'; echo $form_sklad .= implode('', $form_row_zakaz).implode('', $form_row_sale).'</form>'; 

As a result, we get a new form, where the goods for the order are separate from the goods for sale.

Reusing strings with implode php

We can create lines in any order, and then using the numerical indices of the array to determine their sequence. The following example demonstrates this:

 if( $key_product === "model"){ $product[3] = '<span>'.$value_product.'</span>'; }else if( $key_product === "product"){ $product[2] = '<span>'.$value_product.'</span>'; } else if( $key_product === 'gender' ){ if( $value_product === '1' ){ $product[1] = '<span></span>'; }else{ $product[1] = '<span></span>'; } }else if( $key_product === 'color' ){ $product[4] = '<span>'.$value_product.'</span>'; } ksort($product); 

In this example, the lines will be displayed as follows: "men's - shoes - model - color". If you change the indexes, then the output sequence will change, using indexes it is very easy to change their order.

Combining the elements of the $ product array with php implode

 $form_row_product[$row['id']] = implode(' ', $product); 

and then display on the page where necessary:

 echo $form_row_product_string = implode('<br>', $form_row_product); 

Combining multidimensional array values ​​into strings using implode (), array_map (), and array_column ()

To get any lines anywhere in the code, create a multidimensional $ row_product array:

 $row_product[$row['id']] = $product; 

Now we can call the value of the array using array_map:

 echo implode(', ', array_map(function ($entry) { $shoes = array($entry[3], $entry[1]); $shoes_string = implode(' ', $shoes); return $shoes_string; }, $row_product)); 

The last example will display all models of shoes - male or female, you can also display the color and all other values ​​from the $ product array.

If you need to get only one value from $ row_product, you can use the array_column function:

 echo implode('', array_column($row_product, 3)); 

Thus, the php implode function provides access to many of the array features that are useful for getting data and manipulating output strings, which frees us from a strict string concatenation sequence.

All code:

 $hostname="localhost"; $username="root"; $password=""; $dbname="my_database"; $usertable="my_table"; $yourfield = "product"; $con = mysqli_connect($hostname,$username, $password); mysqli_select_db($con, $dbname); mysqli_set_charset($con,"utf8"); $query = "SELECT * FROM $usertable"; $result = mysqli_query($con, $query); mysqli_close($con); $form = ''; $form_sklad=' '; $sale = array(); $zakaz = array(); $product = array(); $row_product = array(); $form .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; $form_sklad .= '<form enctype = "multipart/form-data" action = "" method = "POST" >'; if($result){ while($row = mysqli_fetch_assoc($result)){ foreach($row as $key_product =>$value_product){ $sale[$key_product] = $zakaz[$key_product] = $form_product[$key_product] = ''; if($key_product >= 38 ){ if( $value_product == 1 ){ $sale[$key_product] = $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' min = '1' max = '".$value_product."' id='sale_".$row['id'].$key_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; $zakaz[$key_product] = $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #ccc;' ><label for = 'zakaz_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else if( $value_product == 0 ){ $zakaz[$key_product] = $form_product[$key_product] = "<input type='number' name='zakaz_".$row['id'].'_'.$key_product."' id='zakaz_".$row['id'].$key_product."' min = '1' style='width: 50px; background: #bbb;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; }else{ $sale[$key_product] = $form_product[$key_product] = "<input type='number' name='sale_".$row['id'].'_'.$key_product."' id='sale_".$row['id'].$key_product."' min = '1' max = '".$value_product."' style='width: 50px;' ><label for = 'sale_".$row['id'].$key_product."' > ".$row['product'].' - '.$row['model'].' - '.$row['color'].' - '.$key_product.': '.$value_product."</label><br><br>"; } }else{ if( $key_product === "model"){ $product[3] = '<span>'.$value_product.'</span>'; }else if( $key_product === "product"){ $product[2] = '<span>'.$value_product.'</span>'; } else if( $key_product === 'gender' ){ if( $value_product === '1' ){ $product[1] = '<span></span>'; }else{ $product[1] = '<span></span>'; } }else if( $key_product === 'color' ){ $product[4] = '<span>'.$value_product.'</span>'; } $row_product[$row['id']] = $product; $form_product[$key_product]= $key_product.": ".$value_product."<br>"; $form_row_product[$row['id']] = implode(' ', $product); } $form_row[$row['id']] = implode('', $form_product); $form_row_zakaz[$row['id']] = implode('', $zakaz); $form_row_sale[$row['id']] = implode('', $sale); } } } echo $form .= implode('',$form_row).'</form>'; echo $form_sklad .= implode('', $form_row_zakaz).implode('', $form_row_sale).'</form>'; echo $form_row_product_string = implode('<br>', $form_row_product); echo implode(', ', array_map(function ($entry) { $shoes = array($entry[3], $entry[1]); $shoes_string = implode('', $shoes); return $shoes_string; }, $row_product)); echo $array_column = implode('', array_column($row_product, 3)); 


All Articles