I'm using array_unique() to remove a duplicate value but it gives me an error when the value comes from a string then converted using explode, and values not displaying correctly
I'm using http://phptester.net/ to test
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = array_unique(array_filter(array_map('trim',explode(',',$email))));
for($i = 0; $i < count($emailList); $i++){
echo $emailList[$i];
}
I would do it like this :
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = (array_map('trim',explode(',',$email)));
$result = array_unique($emailList);
var_dump($result);
In case you want to print the array values, using for-loop, you can do like this:
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = (array_map('trim',explode(',',$email)));
$result = array_unique($emailList);
for($i = 0; $i < count($emailList); $i++){
if( $emailList[$i]!=null)
echo $emailList[$i];
}
Related
I am in too much trouble. I need below type of array:-
$val = "abc";
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
I tried below:-
for($i = 1; $i<16; $i++)
{
$arr.$i["besk"] = $val
}
here I've $val. so not to worry on that. But array is not properly creating. Any help would be appreciated.
first define the array as string
like
$arr = 'arr';
then use the foreach
like
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
You need to use variable variables (not recommended)
for($i = 1; $i<16; $i++)
{
${"arr".$i}["besk"] = $val
}
EDIT : #CBroe is right about his comment, you should use an array instead. So the best solution would be to create a two dimensional array like so :
$arr = [];
for($i = 0; $i<15; $i++)
{
$arr[$i]["besk"] = $val
}
The only difference is your array indexes start from 0 now and if you want to have the third value of your array you need this command $arr[2]["besk"]
it is very simple use this:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val
}
Use this approach:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
add new variable
$val = "abc";
$arrName = "arr"; //this one
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
and to call it
for($i = 1; $i<16; $i++)
{
${$arrName.$i}["besk"] = $val
}
ps. you did not create array, you just create 15 array variable with 1 index("besk" index)
I've been reading in every thread in here that is related to this but I always get it wrong.
Please help cause I always get the error
"Notice: Array to string conversion" in line "$address[] =
mysql_result($row, 0 );"
below. Please help.
if ($p_address=mysql_query($email))
{
$address = array();
while($row = mysql_fetch_assoc($p_address))
{
$address[] = mysql_result($row, 0 );
}
$all_address = implode(',', $address);
Change this line
$address[] = mysql_result($row, 0 );
To this:
$address[] = $row;
And then to see the keys and values available in the new $address array, you can do something like this:
print_r($address);
In order to keep implode() functional, do something like this:
for ($i = 0; $i < count($address); $i++) {
$all_address[] = implode(',', $address[$i]);
}
Final output:
if ($p_address=mysql_query($email))
{
$address = array();
while($row = mysql_fetch_assoc($p_address))
{
$address[] = $row;
}
for ($i = 0; $i < count($address); $i++) {
$all_address[] = implode(',', $address[$i]);
}
// Example for outputting on screen:
foreach ($all_address as $aa) {
print $aa . "<br/>\n";
}
}
Hope that helps...
$row is set in every iteration of the while loop. every time it contains a new table record. So you just need to add each record in the address array.
while($row = mysql_fetch_assoc($p_address))
{
$address[] = $row;
}
I am trying to create arrays dynamically and then populate them by constructing array Names using variable but I am getting the following warnings
Warning: in_array() expects parameter 2 to be array, null given
Warning: array_push() expects parameter 1 to be array, null given
For single array this method worked but for array of arrays this is not working. How should this be done?
<?php
for ($i = 1; $i <= 23; ++$i)
{
$word_list[$i] = array("1");
}
for ($i = 1; $i <= 23; ++$i)
{
$word = "abc";
$arrayName = "word_list[" . $i . "]";
if(!in_array($word, ${$arrayName}))
{
array_push($$arrayName , $word);
}
}
?>
Why are even trying to put array name in a variable and then de-reference that name? Why not just do this:
for ($i = 1; $i <= 23; ++$i)
{
$word = "abc";
$arrayName = "word_list[" . $i . "]";
if(!in_array($word, $word_list[$i]))
{
array_push($word_list[$i] , $word);
}
}
You get the first warning because your $arrayName variable is not actually an array, you made it into a string.
So instead of:
$arrayName = "word_list[" . $i . "]";
You should have this:
$arrayName = $word_list[$i];
You get your second warning because your first parameter is not an array.
So instead of:
array_push($$arrayName , $word);
You should have this:
array_push($arrayName , $word);
If you make these changes you will get an array that looks like this in the end:
$wordlist = array( array("1", "abc"), array("1", "abc"), ... ); // repeated 23 times
And in the for loop, you are accessing the array the wrong way
Here is your corrected code
for ($i = 1; $i <= 23; ++$i)
{
$word = "abc";
$arrayName = $word_list[$i];
if(!in_array($word, $arrayName))
{
array_push($arrayName , $word);
$word_list[$i] = $arrayName;
}
}
I am a newbie in Php and this might be a quite basic question.
I would like to set string array and put values to array then get values which
I put to string array. so basically,
I want
ArrayList arr = new ArrayList<String>;
int limitSize = 20;
for(i = 0; i < limitSize; i++){
String data = i + "th";
arr.add(data);
System.out.println(arr.get(i));
};
How can I do this in php?
It's far less verbose in PHP. Since it isn't strongly typed, you can just append any values onto the array. It can be done with an incremental for loop:
$array = array();
$size = 20;
for ($i = 0; $i < $size; $i++) {
// Append onto array with []
$array[] = "{$i}th";
}
...or with a foreach and range()
foreach (range(0,$size-1) as $i) {
// Append onto array with []
$array[] = "{$i}th";
}
It is strongly recommended that you read the documentation on PHP arrays.
$arr = array();
$limitSize = 20;
for($i = 0; $i < $limitSize; $i++){
$data = $i . "th";
$arr[] = $data;
echo $arr[$i] . "\n";
}
In php arrays are always dynamic. so you can just use array_push($arrayName,$val) to add values and use regular for loop processing and do
for ($i=0; $i<count($arrName); $i++) {
echo $arr[$i];
}
to print /get value at i.
I have the following code which is meant to cycle through names submitted on a form:
$row_count = count($_POST['name']);
if ($row_count > 0) {
mysql_select_db($database, $connection);
$name = array();
$workshop = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name[i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop[i] = mysql_real_escape_string($_POST['workshop'][$i]);
}
$names = "('".implode("','",$name)."')";
.....etc
For some reason $names is only returning the last name submitted on the form, rather than all of the names. Could someone help me get this working correctly?
Thanks,
Nick
problem is here
$name[i] =
$workshop[i] =
solution:
$name[$i] =
$workshop[$i] =
now your code is working in this way:
$name["i"] =
$workshop["i"] =
so you have only one element in $name, $workshop arrays. (last from loop)