Build a PHP Array string from Database field value - php

So in my database I have a field that holds values in this format "MSFT,AAPL,SNAP,MCD"
How can I setup these values in this format to PHP such as this
$symbols = ["MSFT","AAPL","SNAP","MCD"];
I know Im going to need to explode (comma delimited) values but cant put my finger on it.
After I would explode the values and I would then use count with a while loop to build the string.
In the end I would think it would look something like this
$new_string = '"MSFT","AAPL","SNAP","MCD"';
Then I think could just do this?
$symbols = [$new_string];

you could try this.
<?php
$str = "MSFT,AAPL,SNAP,MCD";
$symbols = (explode(",",$str));
var_dump($symbols); #displays the array values;
var_dump(count($symbols)); #array count
?>

Related

how to replace space of a string with "," and convert into array

I have a text box.
I am enter the value in text box like 12 13 14.
and i am want to convert this into 12,13,14 and then convert it into array and show each separate value.
If your form field asks for the values without a comma, then you will need to explode the POST data by space. What you're doing now is imploding it by comma (you can't implode a string to begin with), and then trying to pass that into a foreach loop. However, a foreach loop will only accept an array.
$ar = explode(' ',$da);
That simple change should fix it for you. You will want to get rid of the peculiar die() after your foreach (invalid syntax, and unclear what you're trying to do there!), and validate your data before the loop instead. By default, if you explode a string and no matching delimiters are found, the result will be an array with a single key, which you can pass into a loop without a problem.
Are you sure you want to expect the user enters data in that particular format? I mean, what if the user uses more than one space character, or separate the numbers actually with commas? or with semicolons? or enters letters instead of numbers? Anyway.. at least you could transform all the spaces to a single space character and then do the explode() as suggested:
$da = trim(preg_replace('/\s+/', ' ', $_POST['imp']));
$ar = explode(' ', $da);
before your foreach().
use explode instead of implode as
The explode() function breaks a string into an array.
The implode() function returns a string from the elements of an array.
and you cannot do foreach operation for a string.
$da=$_POST['imp'];
$ar = explode(' ',$da);
foreach($ar as $k)
{
$q="insert into pb_100_fp (draw_3_fp) values ('".mysqli_real_escape_string($conn, $k)."')";
$rs=mysqli_query($conn, $q);
echo $k.",";
}
then you will get this output
o/p : 12,13,14,

how to convert letters into array in php

i am trying to convert letters into array.
All the letters are coming from mysql results randomly
for($column=0;$column<8;$column++){
echo '<div class="'.bluexyz.'">'.
$field1 = mysql_fetch_object($sql)->code
.'</div>';
}
each individual letter is a div named bluexyz.
now i want to convert these letters into array.
i have used explode inside the forloop which is not working.
$array = explode('\n',$field1);
it is placing all the letters in the array index of [0]. i want to place a one letter in the one index.
It'd be easier if you provide a clearer explanation and provide an example of what you're fetching and what exactly you're expecting the output to be.
From what I understand, you're trying to convert the $field1 string into array.
You could use str_split() function here.
Try this:
$array = str_split($field1);
echo "<pre>";
print_r($array);
echo "</pre>";
Hope this helps.

php split string by <tr>

I've some tabular data which is fetched from database. Like,
<tr><td>key1</td><td>pair1</td></tr>
<tr><td>key2</td><td>pair2</td></tr>
<tr><td>key3</td><td>pair3</td></tr>
<tr><td>key4</td><td>pair4</td></tr>
I want to split them with <tr>(s), and want rows in an array.
ex.
$arr = ("<td>key1</td><td>pair1</td>",
"<td>key2</td><td>pair2</td>",
"<td>key3</td><td>pair3</td>",
"<td>key4</td><td>pair4</td>");
I heard about explode(',', $myString);but it works with comma saperated string.
Note : Please don't suggest DOM Parser
$out = array_filter(explode('</tr>', str_replace('<tr>','',$input)));
try:
explode('<tr>', $myString);
You can explode every String with any pattern.
http://php.net/manual/en/function.explode.php
But if you split with
<tr>
you still have the
</tr>
in your Array as String. You could also remove them by removing the last 5 Characters in every Array String.
you can try this.
<?php
$arr ='<tr><td>key1</td><td>pair1</td></tr>
<tr><td>key2</td><td>pair2</td></tr>
<tr><td>key3</td><td>pair3</td></tr>
<tr><td>key4</td><td>pair4</td></tr>';
str_replace('<tr>','',$arr);
$rows = explode('</tr>',$arr);
print_r(array_filter($rows));// arry_filter is used to remove the last null node.
?>

Using php's explode function on fractions

I'm using a simple explode operation to use array values for inserting records to mysql database.
The code i use is:
// for loop above
$fieldsArr = explode(',', $field_names);
where $field_names is a string like :
'1_gps_update_coordinates','1' // prints out just fine
'1_meter_conf_holiday1_end','2099-01-01' // also fine
but
'1_electricity_unit_price','0,100' gives problem.
How can I overcome this? Any tip is appricated.
Ps $field_names values comes directly from database so I cannot really write an if statement.
if you can't change the input data, you can do this:
change the delimiter from comma to semicolon with str_replace function
$field_name = str_replace("','", "';'", $field_name)
then you can explode by semicolon
$fieldsArr = explode(';', $field_names);
You need to insert float using english number formatting
0.1
Instead of european 0,1
Use number_formatting() or simply str_replace(',', '.', $val);

getting only certain data from table

I have a field that is in this format
5551112391^HUMAN^HUMAN-800-800^6-main^^
How would I only grab the numbers 5551112391 before the character ^?
Would you do this with regex?
You can make use of explode:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$arr = explode('^',$var);
$num = $arr[0];
Using regex:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
if(preg_match('/^(\d+)/',trim($var),$m)){
$num = $m[1];
}
Regex overkill, nice...
What about simple cast to int? Will work perfectly OK if the number is in the beginning of data. And definitely faster than regexps...
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$num = (int)$var;
http://www.php.net/manual/en/language.types.type-juggling.php
You're doing it in completely wrong way.
You treat mysql database as a flat text file. But it is not.
All these fields must be separated and stored in separate columns.
To get only certain data from the table, you should not select all rows and then compare one by one but make database do it for you:
SELECT * FROM table WHERE number=5551112391

Categories