Solved

PHP - format posted array for MySQL statement

Posted on 2011-09-07
5
188 Views
Last Modified: 2012-05-12
Hi,
is there a way to get just numerical data from a posted array and put into a format of ('n','n','n') so that I can use in a MySQL query

Examples of posted variable subcat below.

Array ( [0] => select_products [1] => 156 )  
Array ( [0] => select_products [1] => 118 [2] => 133 [3] => 134 )
Array ( [0] => 153 [1] => 151 [2] => 152 [3] => 158 [4] => 137 [5] => 138 [6] => 135 ) --->  
Array ( [0] => 174 ) --->  



I want to create something like this from the above
('156')
('118','133','134')
('153','151','158','137','138','135')
('174')


Thanks in advance for your feedback.
0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 36500894
Array ( [0] => select_products [1] => 156 )

if(is_array($array)){
      foreach($array as $key=>$value)
      {
            is_numeric($value)
            {
             $data[] = $value
            }
      }
}

echo '<pre>';print_r($data);
0
 
LVL 7

Expert Comment

by:boon86
ID: 36500918
try this:

<?php
$a=array("select_products ","156");
$b=array("select_products ","118", "133", "134");
$c=array("153","151","152", "158", "137","138","135");
$d=array("174");


echo "('$a[1]')<br />";
echo "('$b[1]','$b[2]','$b[3]')<br />";
echo "('$a[1]')<br />";
echo "('$c[0]','$c[1]','$c[2]','$c[3]','$c[4]','$c[5]','$c[6]')<br />";
echo "('$d[0]')<br />";
?>

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36501150
if you want only integers:

<?php

$a = Array ( 0 => 'select_products', 1 => 118, 2 => 133, 3 => 134 ); 
$b = Array ( 0 => 153, 1 => 151, 2 => 152, 3 => 158, 4 => 137, 5 => 138, 6 => 135 );

$a_str = "(";

foreach($a as $val)
    if(is_int($val)) $a_str .= "'".$val."',";
$a_str = substr($a_str,0,-1);
$a_str .= ")";

echo $a_str;

echo"<br /><br />";

$b_str = "(";

foreach($b as $val)
    if(is_int($val)) $b_str .= "'".$val."',";
$b_str = substr($b_str,0,-1);
$b_str .= ")";

echo $b_str;
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36502101
You can use array_values() to remove the keys from the array.  You can use implode() to create a string from the array, using a comma as the "glue" string.  Then you can use the concatenation operator to wrap the string of numbers in parentheses.  This is a fairly common construct in PHP programming.  But I am curious... What do you mean by a "posted" array?  Where does this data originate?  Can you please show us the CREATE TABLE statement and show us the relationship between the external data and the columns you want to keep in the data base?  The data samples above do not give us much of a clue about how to create the query string that you might need.  Thanks, ~Ray
0
 

Author Closing Comment

by:sabecs
ID: 36502150
Thanks Roads_Roads, that's perfect.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question