This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 5 years ago.
For example I have an array
$array[] = ['name - ic'];
The result I want is
$new_Array[]=['name'];
How do I remove the string that start from the - since the name and ic will be different for everyone? Anyone can help?
Using explode method you can split string into array.
PHP
<?php
$array[] = ['name - ic'];
$array[] = ['name - bc - de'];
$new_Array = array();
foreach($array as $key=>$values){
foreach($values as $k=>$val){
$str = explode("-",$val);
$new_Array[$key][$k] = trim($str[0]);
}
}
?>
OUTPUT
Array
(
[0] => Array
(
[0] => name
)
[1] => Array
(
[0] => name
)
)
You can use explode function for the same.
$array = array('name - ic','abc - xyz','pqr-stu');
$newArray = array();
foreach($array as $obj):
$temp = explode('-',$obj);
$newArray[] = trim($temp[0]);
endforeach;
print_r($newArray);
Result
Array ( [0] => name [1] => abc [2] => pqr )
Let me know if it not works.
Related
I have a string that looks like this:
"illustration,drawing,printing"
I have an array that looks like this:
Array (
[0] => illustration
[1] => drawing
[2] => painting
[3] => ceramics
)
How can I compare the two and only display the terms that exist in both the string and array?
So in the above example, I would want to output something that looks like this:
SKILLS: illustration, drawing
Thank you!
use explode(), array_intersect() and implode()
<?php
$string = "illustration,drawing,printing";
$array = Array (
0 => 'illustration',
1 => 'drawing',
2 => 'painting',
3 => 'ceramics'
);
$stringArray = explode(',',$string);
$common = array_intersect($stringArray,$array);
echo "Skills :".implode(',',$common);
https://3v4l.org/StqYN
You can do something like this:
Function:
function functionName($string, $array){
$skills = '';
// Create a temp array from your string breaking appart from the commas
$temparr = explode(",", $string);
// Iterate through each of the words now
foreach($temparr as $str){
// Check to see if our current string is in the array provided
if(in_array($str, $array)){
// If it is, then add it to the string "skills"
$skills .= "${str}, ";
}
}
// Cut the last space and comma off the end of the str.
$skills = substr($skills, 0, strlen($skills) - 2);
// Return our results
return 'SKILLS: '.$skills;
}
Usage:
$string = "illustration,drawing,printing";
$array = Array (
0 => "illustration",
1 => "drawing",
2 => "painting",
3 => "ceramics"
);
// Just input our string and the array we want to search through
echo(functionName($string, $array));
Live Demos:
https://ideone.com/qyqgzo
http://sandbox.onlinephpfunctions.com/code/65dc988370ad8ce61ab5ccbc232af4bcf8bd3d42
You could replace the comma , and join the string words on OR | and grep for them. This will match the word anywhere in each array element:
$result = preg_grep("/".str_replace(",", "|", $string)."/", $array);
If you want exact matches only then: "/^(".str_replace(",", "|", $string).")$/"
Firstly convert that string into array.
use array_intersect();
$str = "illustration,drawing,printing";
$ar1 = explode(",",$str);
print_r ($ar1);
$ar2 = Array ("illustration","drawing","painting","ceramics");
echo "<br>";
print_r ($ar2);
$result = array_intersect($ar1, $ar2);
echo "Skills :".implode(',',$result);
OUTPUT:
Array (
[0] => illustration
[1] => drawing
[2] => printing
)
Array (
[0] => illustration
[1] => drawing
[2] => painting
[3] => ceramics
)
SKILLS: illustration, drawing
This question already has answers here:
PHP - Generate all combinations of items in array
(2 answers)
Closed 3 years ago.
I have an array() contains string F1, F2, F3, F4.........
<?php
$facts= array("F1", "F2", "F3", "F4);
?>
How can I generate combinations two elements of that array.
The output can be like that:
F1.F2
F1.F3
F1.F4
F2.F3
F2.F4
F3.F4
Please help me
Try this:
Solution
$facts= array("F1", "F2", "F3", "F4");
$new_array = array();
foreach($facts as $key => $val){
foreach($facts as $key2 => $val2){
if($key2 <= $key) continue;
$new_array[] = $val . '.' . $val2;
}
}
print_r($new_array);
Output
Array
(
[0] => F1.F2
[1] => F1.F3
[2] => F1.F4
[3] => F2.F3
[4] => F2.F4
[5] => F3.F4
)
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
here is my array .
$myarray = Array
(
[0] = Array
(
[name] = 17
)
[1] = Array
(
[name] = 18
)
[2] = Array
(
[name] = 19
)
)
I want myvar to return this '17,18,19'
$var = '17,18,19';
You can use array_map;
$temp = array_map(function($i){return $i['name'];}, $myarray);
$output = implode(',', $temp);
You can do it in many ways. One way to do it with array_column() and implode()
<?php
$myarray = array(array('name' => 17),array('name' => 18),array('name' => 19));
$one_d = array_column($myarray, 'name');
echo implode(',',$one_d);
?>
DEMO: https://3v4l.org/rCmKR
A simple foreach could also do the trick.
$var = '';
foreach ($myarray as $value) {
$var .= $value['name'].',';
}
$var = substr($var, 0, -1);
echo $var; // 17,18,19
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I'm trying to loop through an array that I got from preg_match_all result in order to create one string from all results.
Array looks like this:
print_r($matches[0]);
Array
(
[0] => Array
(
[0] => 8147
[1] => 3
)
[1] => Array
(
[0] => 8204
[1] => 20
)
)
And my code:
$found = count($matches[0]);
for ($i = 0; $i <= $found; $i++) {
$string = $matches[0][$i];
}
I would like to get result of $string like this: 8147, 8204.
How I can append $matches[0][0] to $matches[0][1] etc. in string variable using loop?
You can do this some ting like that
$string = "";
foreach($matches[0] as $value) {
$string .= $value[0].", ";
}
$string = rtrim(", ",$string);
With php5.5 and more you can use array_column + implode:
echo implode(', ', array_column($matches, 0));
Try following code. Loop through array and get values
$arr =Array
(
0 => Array
(
0 => 8147,
1 => 3
),
1 => Array
(
0 => 8204,
1 => 20
)
);
$match_array = array();
foreach($arr as $key=>$value)
{
$match_array[] = $value[0];
}
$str = implode(",",$match_array);
echo $str;
DEMO
OR simply use array_column to get specific column as array then use implode
$arr =Array
(
0 => Array
(
0 => 8147,
1 => 3
),
1 => Array
(
0 => 8204,
1 => 20
)
);
$match_array = array_column($arr,0);
$str = implode(",",$match_array);
echo $str;
DEMO
You can use array_column, no need to loop over the array
$result = join(',', array_column($arr, 0));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
i have given two array here.
Array-1
(
[0] => 6
[1] => 11
[2] => 13
)
Array-2
(
[0] => 13.339066309
[1] => 0
[2] => 100
)
I want to replace value of one array to key of another array. something like this:
Array
(
[6] => 13.339066309
[11] => 0
[13] => 100
)
Use array_combine:
$new_array = array_combine($array1, $array2);
take a look at array_combine()
$result = array_combine(array_values($firstArr), array_values($secondArr));
Try this:
$result = array();
foreach ($array1 as $key => $value) {
$result[$value] = $array2[$key];
}
Try something like this
$t = array();
$keys = array_keys($arr1);
for ($i = 0; $i < min(count($keys), count($arr2)); $i++) {
$t[$keys[$i]] = $arr2[$i];
}