make the data into an array and get the values - php

I have the data in my table like this
Arabic,Assamese,Azerbaijani,Belarusian
I want to show the data in an array so that I can use foreach and get the values for the array. So can someone tell me how to make it as an array and get values?

$string = "Arabic,Assamese,Azerbaijani,Belarusian";
$language_array= explode(',',$string);

Supposing you have perform the query to the database and already has the data you can use explode function. See this DEMO.
$data = $row['data_from_table']; //your data is Arabic,Assamese,Azerbaijani,Belarusian
$exp = explode(",", $data);
foreach ($exp as $value){
echo $value;
echo PHP_EOL;
}
?>

Use explode function
$database_value = $db['your_data'];
$value_array = explode(',',$database_value);
print_r($value_array);
foreach ($value_array as $value){
echo $value.'<br>';
}

Related

String to an array and then foreach

I have a variable string which is passed to an and array then foreach that just does not want to work. Below is the code im using.
$explode = $_obj->getModDependencies();
//this variable will returns/echos the string as #ModA,#Mod_b,#Mod3 etc (yes # is in each value)
and the foreach and array php code im using
$arr = array($explode);
foreach ($arr as $value) {
echo ''.$value.'';
}
if i use the above code it echos one hyperlink with each value at the end of it (http://myurl/mod?mod_id=#ModA,#Mod_b,#Mod3) but i want to echo each hyperlink for each value.
Which would be
http://myurl/mod?mod_id=#ModA
http://myurl/mod?mod_id=#Mod_b
and so forth.
But if i place the actual variable output string directly into the array it echos out how i want it (see below code that works)
$arr = array(#ModA,#Mod_b,#Mod3);
foreach ($arr as $value) {
echo ''.$value.'';
}
Any help wold be awesome!!
$arr = array($explode);
Thats the issue, just by saying something is within array() doesnt really make it an array as you expect it to be. You only gave it 1 argument.
You also mentioned the value of $explode is like this #ModA,#Mod_b,#Mod3. Just by naming something $explode doesn't explode it. You have to explode it yourself
$arr=explode(",","#ModA,#Mod_b,#Mod3");
//$arr=explode(",",$explode) in your case
Once that is done, your loop is already fine
foreach ($arr as $value) {
echo ''.$value.'';
}
Fiddle
When your variable $explode will be a string as '#ModA,#Mod_b,#Mod3' then you have to explode it.
$arr = explode(',', $explode);
foreach ($arr as $value) {
echo ''.$value.'';
}

Grabbing array from database, then getting information using each array value

totally stumped in this, basically I'm getting a comma seperated list from a user table, using it as an array and then using each value in the array to fetch data from a different table and output then.
$award_array = array($user_class->awards);
foreach($award_array as $award) {
$getaward = mysql_query("SELECT `name`, `text`, `image` FROM `awards_av` WHERE `id` = '".$award."'");
$awardstuff = mysql_fetch_array($getaward);
echo "<img src='".$awardstuff['image']."' alt='".$awardstuff['name']."' title='".$awardstuff['text']."' />";
}
This is only giving out the first number in the array ($user_class->awards in this case is 1,2,3,4,5,6)
Any help is much appreciated!
I think I see the problem. You cannot simply take a string of a comma separated list, and throw it inside an array() tag and expect it to automatically convert it to an array. You must use the explode() function to do that.
What you're trying to do:
<?php
$string = 'a,b,c,d,e';
$myarray = array($string);
foreach ($myarray as $k => $v) {
print $v .'<br />';
}
?>
Except that doesn't work. You need to convert the comma-separated list of values (which is a string) into an array, but you don't use array() to do that. You use PHP's built-in function called explode() -> http://php.net/explode like this...
<?php
$string = 'a,b,c,d,e';
$myarray = explode(',' $string);
foreach ($myarray as $k => $v) {
print $v .'<br />';
}
?>
I think that is the problem you're having

Build comma separated string in PHP Loop

Hello Guys i need to do this,
I have a common loop
foreach ($stuffs as $stuff) {
echo $stuff;
}
Lets assume $stuff is an 'id' of a mysql table what i have and i dont want to be showed in next results, so i want to build a string like this
1,23,54,67 (comma separated)
So that string will be in mysql query to exclude results that already have been shown.
how can i do that?
Should be with implode? How can i achieve that?
implode should be the tool:
implode(",", $stuffs);
will return a comma separated list.
Test
$myarray=array(1,2,"hello",4,5);
echo implode(",", $myarray);
returns
1,2,hello,4,5
If you really wanna have the loop:
$values = "";
foreach ($stuffs as $stuff) {
$values != "" && $values .= ",";
$values .= $stuff;
}
echo $values;
I suggest using implode, but the loop can really give you more power if you wanna do some further stuff.
This worked in my case (detects if isn't the loop last iteration):
foreach($array as $key => $val){
...
if($key!==count($array)-1){echo ',';}
}
Should be as simple as:
$string = implode(",",$stuffs);
echo $string

PHP - Search array for string

I have a page with a form where I post all my checkboxes into one array in my database.
The values in my database looks like this: "0,12,0,15,58,0,16".
Now I'm listing these numbers and everything works fine, but I don't want the zero values to be listed on my page, how am I able to search through the array and NOT list the zero values ?
I'm exploding the array and using a for each loop to display the values at the moment.
The proper thing to do is to insert a WHERE statement into your database query:
SELECT * FROM table WHERE value != 0
However, if you are limited to PHP just use the below code :)
foreach($values AS $key => $value) {
//Skip the value if it is 0
if($value == 0) {
continue;
}
//do something with the other values
}
In order to clean an array of elements, you can use the array_filter method.
In order to clean up of zeros, you should do the following:
function is_non_zero($value)
{
return $value != 0;
}
$filtered_data = array_filter($data, 'is_non_zero');
This way if you need to iterate multiple times the array, the zeros will already be deleted from them.
you can use array_filter for this. You can also specify a callback function in this function if you want to remove items on custom criteria.
Maybe try:
$out = array_filter(explode(',', $string), function ($v) { return ($v != 0); });
There are a LOT of ways to do this, as is obvious from the answers above.
While this is not the best method, the logic of this might be easier for phpnewbies to understand than some of the above methods. This method could also be used if you need to keep your original values for use in a later process.
$nums = '0,12,0,15,58,0,16';
$list = explode(',',$nums);
$newList = array();
foreach ($list as $key => $value) {
//
// if value does not equal zero
//
if ( $value != '0' ) {
//
// add to newList array
//
$newList[] = $value;
}
}
echo '<pre>';
print_r( $newList );
echo '</pre>';
However, my vote for the best answer goes to #Lumbendil above.
$String = '0,12,0,15,58,0,16';
$String = str_replace('0', '',$String); // Remove 0 values
$Array = explode(',', $String);
foreach ($Array AS $Values) {
echo $Values."<br>";
}
Explained:
You have your checkbox, lets say the values have been converted into a string. using str_replace we have removed all 0 values from your string. We have then created an array by using explode, and using the foreach loop. We are echoing out all the values of th array minux the 0 values.
Oneliner:
$string = '0,12,0,15,58,0,16';
echo preg_replace(array('/^0,|,0,|,0$/', '/^,|,$/'), array(',', ''), $string); // output 12,15,58,16

is it possible to use regex to search inside an array using php

I have an array of string i need to search an string inside the array using regex is it possible if so please explain..
$a = preg_grep("/search_word/",$array_of_strings);
print_r($a);
You can use a foreach loop to loop through all the elements and use a preg_match on each of them. If it matches, add it to an array of matches.
foreach($array as $check) {
if (preg_match("/expression/", $check)) $matches[] = $check;
}
Very simple example.
You can iterate through the array using a foreach loop and search for the key in each element. An example:
<?php
$days = array('Sunday','Monday','Tuesday');
$key = "Sunday";
foreach($days as $day) {
if(preg_match("/$key/",$day)) {
echo "Key $key found !!";
}
}
?>

Categories