Solved

PHP - format posted array for MySQL statement

Posted on 2011-09-07
5
182 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
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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now