Solved

PHP - format posted array for MySQL statement

Posted on 2011-09-07
5
186 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 109

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

820 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