Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP - format posted array for MySQL statement

Posted on 2011-09-07
5
Medium Priority
?
190 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 2000 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 111

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 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