How to get only 1 parameter without repeating from array?
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
I want to return a result:
5WG
6WG
3WG
2WG
(if there is a recurring one, it will only show 1 at a time)
https://www.php.net/manual/en/function.array-unique.php
<?php
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
$a = array_unique($a);
print_r($a);
You should php buitin funciton to remove duplicate values.
$unique_value_array = array_unique($your_array);
read more : https://www.php.net/manual/en/function.array-unique.php
also u can do with this code
$new_arr = array();
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
foreach( $a as $k => $v ) {
if( ! in_array( $v, $new_arr ) ) {
$new_arr[] = $v;
}
}
var_dump( $new_arr );
Related
<?php
/*
1) Print all the elements from an Associative Array. Use key as Integer.
2) Raise an Exception if the IndexedArray is Not in sequence.
*/
$arr = [1=>"Red",8=>"Blue",3=>"Black",6=>"Orange",5=>"Green"];
for($x=0; $x<=count($arr); $x++) //for loop to go thorugh each element by sequence
try{
//what should I code here to check if the $arr is in sequence or not?
}catch(Exception $e){
echo $e->getMessage();
}
One approach would be to compare a sorted vs unsorted list of array keys if you'r expecting an integer values
$arr = [1=>"Red",8=>"Blue",3=>"Black",6=>"Orange",5=>"Green"];
//$arr = [1=>"Red",3=>"Black",5=>"Green",6=>"Orange",8=>"Blue"];
function arrayKeysInSequence( $arr )
{
$keys = $sorted = array_keys( $arr );
asort($sorted);
return $keys === $sorted;
}
arrayKeysInSequence( $arr );
Otherwise, you could track previous indexes, and return early when its greater than the current value
function arrayKeysInSequence( $arr )
{
$prev = null;
foreach( $arr as $index => $value )
{
if( $prev !== null and $prev > $index )
{
return false;
}
$prev = $index;
}
return true;
}
I am working on a small php script, currently i have an array like this
[0] yassine#m, [1] yassine#f, [2] Dolmi#m , [3] yassine#l
I want PHP to check if there is a duplicated element (yassine in this case) and return something like this.
[0] yassine , [1] Dolmi#m
array_unique won't work. And i really don't have any clue how to solve this. If looked for a solution on the internet but doesnt seem to find it. Anyone can help Please ?
I think this may work for you.
First sort array by value, then use combination of substr(), strpos() and array_push() to create new array according to your need
then remove duplicate value using array_unique()
<?php
$oldarray = array("suman#1","suman#2","suman#3","sujan#1","suresh#2","");
// first sort array by value so matching value comes together
asort($oldarray);
$newarray = array();
$count = count($oldarray);
for($i=0; $i < $count-1; $i++){
$a = $oldarray[$i];
$b = $oldarray[$i+1];
if($i == 0)
$c = "";
else
$c = $oldarray[$i-1];
if(substr($a,0,strpos($a,"#")) == substr($b,0,strpos($b,"#")) || substr($a,0,strpos($a,"#")) == substr($c,0,strpos($c,"#")) ){
array_push($newarray,substr($a,0,strpos($a,"#")));
}
else
array_push($newarray,$a);
}
print_r($oldarray);
// now remove duplicate value from new array
$newarray = array_unique($newarray);
print_r($newarray);
?>
Check following solution
http://ideone.com/fork/kJlLbs
<?php
function generateUniqueList ($arr){
$ret = array();
foreach ($arr as $value) {
$key = explode("#", $value)[0];
if (array_key_exists($key, $ret)) {
$ret[$key] = $key;
}
else {
$ret[$key] = $value;
}
}
return array_values($ret);
}
$arr = array("yassine#m","yassine#f","Dolmi#m", "yassine#l");
$list = generateUniqueList ($arr);
print_r($list);
$data = array
(
array("Ravi","Kuwait",350),
array("Sameer","UK",400),
array("Aditi","Switzerland",50),
array("Akshay","India",250),
array("rishi","Singapore",200),
array("Mukul","Ireland",100)
);
I want to put condition to the third row such that I can get entries of less than 300.
I suppose that you meant "the third element" in each nested array.Use array_filter function to get an array of elements, those third element's value is less than 300:
$result = array_filter($data, function($v) { return $v[2] < 300; });
print_r($result);
Try this code:
<?php
$data = array
(
array("Ravi","Kuwait",350),
array("Sameer","UK",400),
array("Aditi","Switzerland",50),
array("Akshay","India",250),
array("rishi","Singapore",200),
array("Mukul","Ireland",100)
);
$newArray = array();
foreach($data as $key => $value)
{
if($value[2] <= 100)
$newArray[] = $value;
}
print_r($newArray);
?>
You can achieve this using the PHP function array_filter() :
PHP
function limitArray($array) {
return ($array[2] <= 300);
}
print_r(array_filter($data, 'limitArray'));
evalIN
I am trying to manually sort a PHP array without making use of ksort.
This is how my code looks at the moment:
function my_ksort(&$arg){
foreach($arg as $key1 => $value1){
foreach($arg as $key2 => $value2){
if($key1 > $key2){
$aux = $value2;
$arg[$key2] = $value1;
$arg[$key1] = $aux;
}
}
}
}
It doesn't sort, I can't figure out how to make it sort.
You could try this:
function my_ksort(&$arg)
{
$keys=array_keys($arg);
sort($keys);
foreach($keys as $key)
{
$val=$arg[$key];
unset($arg[$key]);
$arg[$key]=$val;
}
}
I'm sorting the keys separately and then deleting the elements one-by-one and appending them to the end, in ascending order.
I'm using another sorting function (sort()), but if you want to eliminate all available sorting functions from your emulation, sort() is much easier to emulate. In fact, #crypticous's algorithm does just that!
This function return array in ASC. Take in consideration that I'm using goto which is supported in (PHP 5 >= 5.3.0)
function ascending_array($array){
if (!is_array($array)){
$array = explode(",", $array);
}
$new = array();
$flag = true;
iter:
$array = array_values($array); // recount array values with new offsets
(isset($min["max"])) ? $min["value"] = $min["max"] : $min["value"] = $array[0];
$min["offset"] = 0;
for ($i=0;$i<count($array);$i++){
if ($array[$i] < $min["value"]){ // redefine min values each time if statement executed
$min["value"] = $array[$i];
$min["offset"] = $i;
}
if ($flag){ // execute only first time
if ($array[$i] > $min["value"]){ // define max value from array
$min["max"] = $array[$i];
}
$flag = false;
}
if ($i === (count($array)-1)){ // last array element
array_push($new,$min["value"]);
unset($array[$min["offset"]]);
}
}
if (count($array)!=0){
goto iter;
}
print_r($new);
}
$arr = array(50,25,98,45);
ascending_array($arr); // 25 45 50 98
PS. When I was studying php, I wrote this function and now remembered that I had it (that's why I really don't remember what I am doing in it, though fact is it's working properly and hopefully there are comments too), hope you'll enjoy :)
DEMO
I was checking some issue related to this post and i wanted to give my insight about it ! here's what i would have done to implement php's sort :
$array_res = array();
$array = array(50,25,98,45);
$i=0;
$temp = $array[0];
$key = array_search($temp, $array);
while ($i<count($array)-1){
$temp = $array[0];
for($n=0;$n<count($array) ;$n++)
{
if($array[$n]< $temp && $array[$n] != -1 )
{
$temp = $array[$n];
}
else{continue;}
}
//get the index for later deletion
$key = array_search($temp, $array);
array_push($array_res, $temp);
/// flag on those which were ordered
$array[$key] =-1;
$i++;
}
// lastly append the highest number
for($n=0;$n<count($array) ;$n++)
{
if ($array[$n] != -1)
array_push($array_res, $array[$n]);
}
// display the results
print_r($array_res);
This code will display : Array
(
[0] => 25
[1] => 45
[2] => 50
[3] => 98
)
Short and sweet
function custom_ksort($arg)
{
$keys = array_keys($arg);
sort($keys);
foreach($keys as $newV)
{
$newArr[$newV] = $arg[$newV];
}
return $newArr;
}
It looks like your issue is that you're changing "temporary" characters $key1 and $key2 but not the actual arrays. You have to change $arg, not just $key1 and $key2.
Try something like:
$arr = Array(3=>"a",7=>"b");
print_r( $arr );
foreach( $arr as $k=>$v ){
unset($arr[$k]);
$arr[$k+1] = $v;
}
print_r($arr);
$a = Array("one", "two", "three");
$b = "text"
I have been trying to transform the above array into something like this:
$a = Array("one" => Array("two" => Array("three" => "text")));
I am looking for a way to do it without improvising but so far no luck and googleing seems to turn up with everything but what I am looking for.
Use recursion
function make(array $array, $value) {
$first = array_shift($array);
if (count($array) === 0) {
return array($first => $value);
} else {
return array($first => make($array, $value);
}
}
It takes the first item of the array and places it in $first. When placed in $first it is removed from $array. Then it checks if the array has some items left. If so it coninues the loop otherwise it end the loop.
Hope it works for you
So you can call it like:
$a = Array("one", "two", "three");
$b = "text";
$array = make($a, $b);
$i=count($array)-1;
$b=array();
$a=$array[$i];
while($i>0) {
$b=array($array[$i-1]=>$a);
$a=$b;
$i--;
}
var_dump($a);