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

php implode . .

, , . .

. , "".

.

$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);
      
      



:

MySql database




:

	
$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>';
      
      



, , .





, :

Build html form using implode php




, , , .

implode()

, php implode, .

: , implode():

$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>';

      
      



, , "" "" . php array implode . :





$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>';



      
      



($form_sklad). "" $zakaz, "" - $sale. , $sale, $zakaz. - ( 38 42). ($row['id']) , , php implode $form_row_zakaz $form_row_sale. , $zakaz ( ), $sale ( ).

, .

:

$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>';
      
      



, .

Reusing strings with implode php




, . :

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);
      
      



: " - - - ". , , .

$product php implode

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

      
      



, :

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

      
      



implode(), array_map() array_column()

, $row_product:

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

      
      



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 lines, 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