Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I got this string
$string = '1:23:health,2:24:mana';
$v = explode(",", $string);
foreach($v as $data)
{
echo $data[0][1];
}
What im looking is to making a "multidimensional string" by exploding ','
Wanna do this:
data[0][0] = 1;
data[0][1] = 23;
data[0][2] = "health";
How can i do this in PHP?
$string = '1:23:health,2:24:mana';
$result = [];
foreach (explode(',', $string) as $step) {
$result[] = explode(':', $step);
}
print_r($result);
An alternative is:
$string = '1:23:health,2:24:mana';
$result = array_chunk(preg_split('/[:,]/', $string), 3);
print_r($result);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have probably information about whether it is available in PHP to detect a duplicate and a book about not removing it and adding it to -1, -2, -3
Example:
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
Result:
$output = 'H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2'
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$parts = explode(',', $string); // split by comma
$used = []; // array to count the number of occurrences
$result = []; // array to take the "new" elements
foreach($parts as $part) {
if(isset($used[$part])) { // if there is an entry in the counter array already,
// increment it by one,
$used[$part]++;
}
else { // else initialize with 0
$used[$part] = 0;
}
// put part into new array, append -(counter) if counter > 0
$result[] = $part . ($used[$part] ? '-'.$used[$part] : '');
}
echo implode(',', $result); // join together with comma
Pretty much the same as misorude's answer.
<?php
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$values = explode(',', $string);
foreach($values as $k => $value)
if($counts[$value] = !isset($counts[$value]) ? 0 : $counts[$value]-1)
$values[$k] = $value . $counts[$value];
print implode(',', $values);
Output:
H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
(PHP) how can I split a string in multiple strings separated by comma,
like is I have a string "Hello my neighbor",
then after the split process, it should be
("Hello my neighbor","Hello my","Hello neighbor","my neighbor","Hello","my","neighbor")
try this :
<?php
$string = "Hello my neighbor";
$str = explode(" ",$string);
$newArray = [];
$newArray[] = $string;
for($i = 0; $i<count($str); $i++) {
if($i <= 1) {
$newArray[] = $str[$i]." ".$str[$i+1];
if($i != 0) {
$newArray[] = $str[$i-1]." ".$str[$i+1];
}
}
$newArray[] = $str[$i];
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array with comma separated numbers and I want to remove all duplicated numbers. Is the approach below the best choice or is there a smarter one?
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = '';
foreach($filter_tags_array as $filter_tags) {
$tags_id .= $filter_tags . ',';
}
$tags_id = implode(',', array_unique(explode(',', $tags_id)));
echo "<pre>";
print_r($tags_id);
print_r results: 4,6,2,5,8,1,3,7,11,9,12,10,
It would be better to implode the array rather than concatenate each string. This is because php has to create a new string every time.
https://3v4l.org/fUEtn
This should work for you, see here
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = implode(',',array_unique(explode(',', (implode(',', $filter_tags_array)))));
print($tags_id);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have two arrays.
$a = array("one","two","three");
$b = array ( 0,1,2,3,4,5,6,7,8,9);
Look at the user contributed example in http://www.php.net/manual/en/class.infiniteiterator.php
$obj = new stdClass();
$obj->Mon = "Monday";
$obj->Tue = "Tuesday";
$obj->Wed = "Wednesday";
$obj->Thu = "Thursday";
$obj->Fri = "Friday";
$obj->Sat = "Saturday";
$obj->Sun = "Sunday";
$infinate = new InfiniteIterator(new ArrayIterator($obj));
foreach (new LimitIterator($infinate, 0, 14) as $value ) {
print($value . PHP_EOL);
}
You could create a mapping function and use array_map as follows:
function numToName($num) {
return array("zero","one","two","three","four","five",
"six","seven","eight","nine")[$num];
}
$b = array (0,8,2,3,7,5,6,0,4,1);
$result = array_map("numToName", $b);
print_r($result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I found problem with this:
$explode = explode($start, $data);
$abc = explode($end, $explode[1]);
$found = $abc[0] . '<br/>';
$found .= $abc[1] . '<br/>';
$found .= $abc[2] . '<br/>';
return $found;
The abc[0], abc[1] and more is randomly based by exploded results. How to define if $found is array or something I can loop it in foreach?
That's. Thank You for answer.
explode($start, $data) will return an array unless $start is an empty string "" in which case it will return false. You can confirm whether $found is an array by using is_array($found) which will return true if $found is an array and false otherwise.