this code is in php
$v = 1,2,3,4,5;
as I have to concat _1 in above variable
as I need this output $v = 1_1,2_1,3_1,4_1,5_1
Please refer to the PHP Manual:
implode — Join array elements with a string
explode — Split a string by string
In your case:
$v = "1,2,3,4,5";
echo implode("_1,", explode(",", $v)) . "_1";
On a side note: since your string is a comma separated value, you might also be interested in
str_getcsv — Parse a CSV string into an array
Without exploding/imploding, you can:
echo str_replace(',', '_1,', '1,2,3,4,5') . '_1';
$v = "1,2,3,4,5;";
$newValue = str_replace(",","_1,",$v); //replace , with _1,
$newValue = str_replace(";","_1;",$newValue); //replace ; with _1;
output:
1_1,2_1,3_1,4_1,5_1;
Use array map
$v = '1,2,3,4,5';
$arr = explode(',',$v);
$arr = array_map(function ($val){
return $val.'_1';
},$arr);
echo implode(',',$arr);
demo
I think you should put those numbers in quote.
$v = '1,2,3,4,5';
$new_v = explode(',', $v);
foreach ($new_v as $x) {
$v1[] = $x.'_1';
}
print_r($v1);
It will return array like this.
Array ( [0] => 1_1 [1] => 2_1 [2] => 3_1 [3] => 4_1 [4] => 5_1 )
Related
I have a php variable that contain value of textarea as below.
Name:Jay
Email:jayviru#demo.com
Contact:9876541230
Now I want this lines to in array as below.
Array
(
[Name] =>Jay
[Email] =>jayviru#demo.com
[Contact] =>9876541230
)
I tried below,but won't worked:-
$test=explode("<br />", $text);
print_r($test);
you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it
<?php
$text = 'Name:Jay
Email:jayviru#demo.com
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);
$final_data = array();
foreach ($array_data as $data){
$format_data = explode(':',$data);
$final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);
and output is :
Array
(
[Name] => Jay
[Email] => jayviru#demo.com
[Contact] => 9876541230
)
Easiest way to do :-
$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);
$final_array = array();
foreach($textarea_array as $textarea_arr){
$exploded_array = explode(':',$textarea_arr);
$final_array[trim($exploded_array[0])] = trim($exploded_array[1]);
}
print_r($final_array);
Output:- https://eval.in/846556
This also works for me.
$convert_to_array = explode('<br/>', $my_string);
for($i=0; $i < count($convert_to_array ); $i++)
{
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array); ?>
I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array
I'm looking to get an array of ID's from the following string.
[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]
Ideally, i'd like to look at this string and get an array of the INT values within images. e.g.
array("3057", "2141", "234");
find images value and explode it to receive array
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
if (preg_match('/images\s*=\s*\"([^\"]+)\"/', $str, $m)) {
$res = explode(',', $m[1]);
print_r($res);
}
Another solution using explode and strpos functions:
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
foreach (explode(" ", $str) as $v) {
if (strpos($v, "images=") === 0) {
$result = explode(",", explode('"', $v)[1]);
break; // avoids redundant iterations
}
}
print_r($result);
The output:
Array
(
[0] => 3057
[1] => 2141
[2] => 234
)
My query
$sql ="select tTrack from tblcourse where tCourseCode = 'AZURE'";
$rs = mysql_query($sql);
$row_rs_course_category = mysql_fetch_assoc($rs);
gives me value Array ( [tTrack] => MS,CLOUD )
but i need to conver that array to this format from PHP
Array ( [0] => MS [1] => CLOUD )
how do i do it in php
You can use explode function to get this.
$newValue = explode(",", $row_rs_course_category['tTrack']);
Use explode().
$value = explode(',',$row_rs_course_category['tTrack'] );
echo "<pre>";
print_r($value);
//Array ( [0] => MS [1] => CLOUD )
I think you can use explode function to achieve this
$array = array ( 'tTrack' => 'MS,CLOUD' );
$array1 = explode(',', $array['tTrack']);
var_dump($array1);
this will output
array(2) { [0]=> string(2) "MS" [1]=> string(5) "CLOUD" }
explode() can help you
try this
$array = explode("," , $row_rs_course_category['tTrack']);
print_r($array);
You can use PHP explode function: http://php.net/manual/en/function.explode.php
$newArr = explode(',', $row_rs_course_category['tTrack']);
print_r($newArr);
User Php explode() function:
Look on Below link :
http://php.net/manual/en/function.explode.php
$sql ="select tTrack from tblcourse where tCourseCode = 'AZURE'";
$rs = mysql_query($sql);
$row_rs_course_category = mysql_fetch_assoc($rs);
if(isset($row_rs_course_category['tTrack']) && $row_rs_course_category['tTrack'] != '')
{
$result = explode(',',$row_rs_course_category['tTrack'] );
echo "<pre>";
print_r($value);
}
array explode ( string $delimiter , string $string [, int $limit ] )
delimiter
The boundary string.
string
The input string.
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
If the limit parameter is negative, all components except the last -limit are returned.
If the limit parameter is zero, then this is treated as 1.
Switch to sqli, and do what they said explode the array.
$your_array=Array ( 'tTrack' => "MS,CLOUD" );
$new_array=explode(",",$your_array['tTrack']);
print_r($new_array);
$data1 = "mathews,luks,john,ethan,neil";
$data2 = "80%,60%,78%,55%,62%";
want to combine both like
mathews = 80%
Luks = 60%
John = 78%
etc.... how to do this?? I want print result exactly like Mathews = 80%
http://us.php.net/manual/en/function.array-combine.php
$result = array_combine ( explode ( ',', $data1 ), explode ( ',', $data2 );
to extract:
if you know key name: echo $result ['john'];
if all: foreach ( $result as $key => $value ) { echo $key . ' : ' . $value; }
to gey list of keys: $list = explode ( ',', $data1 ) or $list = array_keys ( $result );
http://us.php.net/manual/en/function.array-keys.php
How about this...?
print_r(array_combine(explode(',',$data1),explode(',',$data2)));
$arr = array_combine(explode(',', $data1), explode(',', $data2));
Or
$names = explode(',', $data1);
$par = explode(',', $data2);
$arr = array();
foreach ($names as $idx=>$name)
{
$arr[$name] = $par[$idx];
}
Use the built in array_combine
You may consider using preg_split to the string:
$names = preg_split("/[,]+/", "mathews,luks,john,ethan,neil");
$percent = preg_split("/[,]+/", "80%,60%,78%,55%,62%");
Then you can use array_combine like many others suggest, or simply use 2 arrays $names and $percent along with the shared index, for example, $names[2] goes along with $percent[2].
As stated by bensui:
$data1 = "mathews,luks,john,ethan,neil";
$data2 = "80%,60%,78%,55%,62%";
$array = array_combine(explode(',',$data1), explode(',',$data2));
This will result in:
print_r($array);
Array
(
[mathews] => 80%
[luks] => 60%
[john] => 78%
[ethan] => 55%
[neil] => 62%
)
This will give you the ability easily update the assocaited scores simple by doing:
$array['mathews'] = '90%';
You can then use the array_slice I offered you earlier to discard john and ethan:
$newArray = array_slice($array, 2, 2);
This will give you:
print_r($newArray);
Array
(
[mathews] => 80%
[luks] => 60%
[neil] => 62%
)
Note: there is nothing wrong with using loops, the fact of the matter is they are often over used when there are methods built in to the language to solve many common problems.
If you are some kind of anti-loop fanatic, you could create a recursive function.
function no_loop_combine($data1,$data2,$index)
{
if($index == count($data1))
{
return '';
}
else
{
return $data1[$index] . ' = ' . $data2[index]
. '\n' . no_loop_combine($data1,$data2,$index + 1);
}
}
Then you just have to call your function specifying $index = 0, and you will get the result. Using array_combine probably is still using a loop, just masked by the fact that you are calling a function. Using recursion means there is no loop. Just realized that your initial input is a string, and not a loop. So you could call explode before passing it into the function as so:
no_loop_combine(explode(',', $data1 ),explode(',',$data2),0);
I have string:
ABCDEFGHIJK
And I have two arrays of positions in that string that I want to insert different things to.
Array
(
[0] => 0
[1] => 5
)
Array
(
[0] => 7
[1] => 9
)
Which if I decided to add the # character and the = character, it'd produce:
#ABCDE=FG#HI=JK
Is there any way I can do this without a complicated set of substr?
Also, # and = need to be variables that can be of any length, not just one character.
You can use string as array
$str = "ABCDEFGH";
$characters = preg_split('//', $str, -1);
And afterwards you array_splice to insert '#' or '=' to position given by array
Return the array back to string is done by:
$str = implode("",$str);
This works for any number of characters (I am using "#a" and "=b" as the character sequences):
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
$s = "ABCDEFGHIJK";
$arr = str_split($s);
$arr_add1 = array(0=>0, 1=>5);
$arr_add2 = array(0=>7, 1=>9);
$char1 = '#a';
$char2 = '=b';
$arr = array_insert($arr, $arr_add1[0], $char1);
$arr = array_insert($arr, $arr_add1[1] + strlen($char1), $char2);
$arr = array_insert($arr, $arr_add2[0]+ strlen($char1)+ strlen($char2), $char1);
$arr = array_insert($arr, $arr_add2[1]+ strlen($char1)+ strlen($char2) + strlen($char1), $char2);
$s = implode("", $arr);
print_r($s);
There is an easy function for that: substr_replace. But for this to work, you would have to structure you array differently (which would be more structured anyway), e.g.:
$replacement = array(
0 => '#',
5 => '=',
7 => '#',
9 => '='
);
Then sort the array by keys descending, using krsort:
krsort($replacement);
And then you just need to loop over the array:
$str = "ABCDEFGHIJK";
foreach($replacement as $position => $rep) {
$str = substr_replace($str, $rep, $position, 0);
}
echo $str; // prints #ABCDE=FG#HI=JK
This works by inserting the replacements starting from the end of string. And it would work with any replacement string without having to determine the length of that string.
Working DEMO