How to add string if two arrays are in php? - php

I have array values if there are two values i need to add a string "AND" if single value "AND" string should not be added. i have tried with the following code. cant get the required output
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
$checks = array();
foreach($unserialize_meta as $meta){
$checks[]= $meta;
}
echo implode(" And ",$checks);
Output:
Alcor And President
Alcor And President And
required output:
Alcor And President
Alcor And President

You can use the implode function for this. Details can be found here.
Considering the above code:
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
$checks = implode(" AND ", array_filter($unserialize_meta));
var_dump($checks);
The array_filter will remove any empty values in the array.

I thing you don't need to loop array. you just need to implode array with the string. Please try below code it will add string AND with your array values.
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
if(!empty($unserialize_meta )) {
echo implode(" And ",$unserialize_meta);
}
Output:
Alcor And President And Treasurer

There is mistake saved in array values in array I'm getting empty array. So I add a empty array check :
if(!empty($meta)){
$checks[]= $meta;
}

Related

PHP array search with given key word from array [duplicate]

This question already has answers here:
filter values from an array similar to SQL LIKE '%search%' using PHP
(4 answers)
Closed last month.
I have a search text field, and an array I want to output below.
This is my array:
array=["abc","abcde","ab","abcdef"];
When I enter "ab" in the text field, then the list should appear. "ab" should come first.
ab,
abc,
abcde,
abcdef,
If I type "abc" then list should show:
abc,
abcde,
abcdef,
This should solve your little issue
<?php
$input = preg_quote('cde', '~'); // don't forget to quote input string!
$array=["abc","abcde","ab","abcdef"];
$result = preg_grep('~' . $input . '~', $array);
foreach ($result as $val) {
echo "$val\n";
}
?>
Check this link for more
<?php
$array = ["abc","abcdeab","ab","abcdef"];
$arr = preg_grep('/cde/', $array);
sort($arr);
var_dump($arr);
?>
check this out

how to create simple array(without key) by putting available values in php [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)

how to input elements instead of another array, into end of array?

Due to bad DB design, there may be several values in a column # each row in a table. So I had to take in every string, check if commas exist (multiple values) & place each element into the end of an array.
Did try out functions like strpos, explode, array_push etc. With the folllowing code, how do i input ONLY the multiple elements into the end of an array, without creating another & placing that into an existing array?
$test = array();
$test = array ("testing");
$str = 'a,b,c,d';
$parts = explode(',', $str);
array_push ($test, $parts); //another array inserted into $test, which is not what I want
print_r($test);
Use array_merge.
$test = array_merge($test, $parts);
Example: http://3v4l.org/r7vaB

Prepared String Before Comma

I've got an array;
Array
(
[0] => Test1
[1] => Test2
[2] => Test3
)
Now I've used the Implode Function from which i got the comma separated String:
Test1, Test2, Test3
now I'd like to put a quotes ("") before and after every word e.g.
"Test1", "Test2", "Test3"
How could I change it to work how I want.
Try this simple one-liner:
$quotedStrings = '"' . implode('","', $myArray) . '"';
The "glue" parameter may be a complex string, though you only have to put the " at the beginning and end.
While the answer from Axel is totally fine for the given szenario,
using array_map along with implode will also work.
And this has the advantage, that modifications to each element are not limited to the start/end of the element. For instance, you can turn each entry to lower-case as well, or perform other, more complex operations, before applying implode.
$quotedString = implode(",", array_map("myCallback", $myArray));
function myCallback($entry){
//here you can to whatever you like to EACH element.
return '"'.$entry.'"';
}
Consider this an option
You can use make a new array by using foreach loop and then use implode. Use the code below
<?php
$array = array("Test1","Test2","Test3");
$quotes = array();
foreach($array as $p){
$quotes[] = '"'.$p.'"';
}
echo implode(",",$quotes); // Outputs "Test1","Test2","Test3"
?>
Another way you can do it by enclosing the implode in "" quotes and then implode comma , in quotes.
<?php
$array = array("Test1","Test2","Test3");
echo '"'.implode('","',$array).'"'; // Outputs "Test1","Test2","Test3"
?>
Hope this helps you

Finding last entry in a foreach() process

Simple one guys.
I have an XML parsed using simplexml_load_file().
The following code:
<?php
foreach($xml->actors->actor as $actors) {
echo $actors.", ";
}
?>
Gives the following result:
John Smith, Amy Adams, Charlie Doe,
How do I modify the code such that it gives:
John Smith, Amy Adams, Charlie Doe
This needs to apply across any number of entries in the array. Thanks!
Use the implode function.
echo implode(", ", $xml->actors->actor);
You can get the current index in a foreach:
<?php
foreach($xml->actors->actor as $key => $actors) {
if ($key == (count($actors)-1) echo "Last entry!";
}
?>
In cases like this, however, I prefer to create a temporary array with the entries first, and then implode it:
echo implode(",", $names);
print implode(',',$xml->actors->actor);
I usually solve this problem with a simple array:
<?php
$names = array();
foreach($xml->actors->actor as $actors) {
$names[] = $actors;
}
echo implode(", ", $names);
?>
As a matter of fact why not just say
<?php
echo implode(", ", $xml->actors->actor);
?>
You can either implode the array as many people above have suggested, or assign the actor's names to a variable and concatenate that variable with each iteration, and then perform a rtrim operation on the resulting array to remove the last comma.
But for simplicity's sake, go with an implode.

Categories