PHP remove duplicated values from array [closed] - php

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);

Related

how to explode a string using a foreach [closed]

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);

Duplicate words in PHP [closed]

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

Separate column with comma [closed]

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 6 years ago.
Improve this question
i can separate join column with comma, but how to remove comma if on the second column is null?
in below, i have 2 column i want join otot2 and otot3
1. how to not shown comma after otot2 if otot3 null?
2. and how to not shown anything comma if otot2 and otot 3 null?
3. otot2 and otot3 flexible ( not always has data)
here my php code for output query:
if(mysqli_num_rows($hasil) > 0)
{
$response = array();
while($data = mysqli_fetch_array($hasil))
{
$h['main muscle'] = $data['otot1'];
$h['other muscle'] = $data['otot2'].', '.$data['otot3'];
array_push($response, $h);
}
$response["success"];
echo json_encode($response);
A simple solution would be -
$response = array();
while($data = mysqli_fetch_array($hasil))
{
// other code
$temp = array($data['otot2'], $data['otot3']); // Store them in array
$temp = array_filter($temp); // remove empty values
$h['other muscle'] = implode(',', $temp); // implode them with (,)
// Other codes
array_push($response, $h);
}
By using array_filter & implode you don't have to worry about the ,, as array_filter would remove the empty values and if there is 2 values in the array them the string would be , separated else not.
Or you can try this also
$h['other muscle'] = $data['otot2'];
if($data['otot3'] !== null)
$h['other muscle'] .= ', ' . $data['otot3']; // Concatenate
Demo

How can I get result as array (one, two, three, one, two, three, one, two, three, one) [closed]

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);

Foreach from explode [closed]

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.

Categories