I have this array:-
Array ( [0] => Prefectorial Board/Prefect/2011 [1] => Prefectorial Board/Head Prefect/2011 [2] => Class Positions/Head Prefect/2011 [3] => Prefectorial Board/Head Prefect/2011 )
How to detect this array have duplicate value and I want the participant to re-fill in his data.
I have try to use this method to detect:-
$max = max(array_values($lr_str));
$keys = array_keys($lr_str, $max);
but it seem like not work for me.
any idea on this?
thanks for advance.
I do not know if I understand you correctly, but you can test the array has duplicated values by using this:
if (count($your_array) === count(array_unique($your_array)) {
// array has no duplicated values
} else {
// array has duplicated values - see compared arrays
}
Are you looking for array_unique?
You can use array_unique() to remove duplicate values. If no values were removed then the returned array should have the same number of items as the original:
if(count($lr_str) == count(array_unique($lr_str))){
//tell participant to re=fill their data
}
Related
I have the following scenario
$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];
and i need to sort the second array which would give me
[1,2,3,4]
which i can do by
sort($valuesInPair)
but i need the same to happen in first array as well, based on the sorting of the second array
["zzz","xxx","yyy","xyz"];
EDIT
As i had to fix it urgently, i guess i wasn't able to share more information, and the question seemed vague
basically the idea is that i have two sets of arrays, same number of elements in all cases, and they are linked to each other, The second array is a set of order IDs. and first array is set of names,
so in the above example, i have
4 relates to xyz
2 relates to xxx
3 relates to yyy
1 relates to zzz
So i need to sort based on IDs, and have the same order reflect in the first array as well,
so the final result would be,
["zzz","xxx","yyy","xyz"];
which is sorted based on
[1, 2, 3, 4];
Hopefully this clears things above
You can achieve this by using array_multisort method.
array_multisort($valuesInPair, SORT_ASC, $elementsInPair);
Use this below code, this does exactly what you're looking for.
$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];
$data = array_combine($elementsInPairs,$valuesInPair);
asort($data);
$dumpdata = [];
foreach($data as $x => $x_value) {
$dumpdata[] = $x;
}
print_r($dumpdata);
I hope this helps you.
You kan do like this :
<?php
$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];
//use [asort][1] - Sort an array in reverse order and maintain index association
asort($valuesInPair);
// and make a new array to sort elementsInPairs
$newelementsInPairs = array();
foreach($valuesInPair as $key=>$val){
$newelementsInPairs[] = $elementsInPairs[$key];
}
print_r(implode(",",$valuesInPair)."\n");
print_r(implode(",",$newelementsInPairs));
/** Output
1,2,3,4
zzz,xxx,yyy,xyz
**/
Hi please combine two array and sort
$newArray =array_combine($valuesInPair,$elementsInPairs);
then sort($newArray);
You can use array_combine(), ksort() and array_values():
<?php
$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair = [4,2,3,1];
$newArray = array_combine($valuesInPair, $elementsInPairs);
ksort($newArray);
$sortedElements = array_values($newArray);
print_r($sortedElements);
will output
Array
(
[0] => zzz
[1] => xxx
[2] => yyy
[3] => xyz
)
I have two array variables like this
$order_qty=array('1','2','3');
$quantity_per_pack= array('50','100','100')
I want to multiply these two variables and get stored in third variable which will be in array form such as
$total_order_qty = array('50','200','300')
This is my code:
for ($i=0;$i<10;$i++)
{
$total_order_qty[$i] = $quantity_per_pack[$i] * $order_qty[$i] ;
echo $total_order_qty[$i];
}
I declared these three variables as array before for loop.
Please help me to get solution for this.
Hope this will help your
you can use array_map for that like this
working demo :https://eval.in/1014627
function multi($n, $m)
{
return($n*$m);
}
$order_qty = array('1','2','3');
$quantity_per_pack = array('50','100','100');
$total_order_qty = array_map("multi", $order_qty, $quantity_per_pack);
print_r($total_order_qty );
Program Output
Array
(
[0] => 50
[1] => 200
[2] => 300
)
For more : http://php.net/manual/en/function.array-map.php
First time trying to find something in an array using an array as both a needle and a haystack. So, example of 2 arrays:
My Dynamically formed array:
Array (
[0] =>
[1] => zpp
[2] => enroll
)
My Static comparison array:
Array (
[0] => enroll
)
And my in_array() if statement:
if (in_array($location_split, $this->_acceptable)) {
echo 'found';
}
$location_split; // is my dynamic
$this->_acceptable // is my static
But from this found is not printed out, as I'd expect it to be? What exactly am I failing on here?
If i'm understanding you right, you want to see whether the first array's entries are present in the second array.
You might look at array_intersect, which would return you an array of stuff that's present in all the arrays you pass it.
$common = array_intersect($this->_acceptable, $location_split);
if (count($common)) {
echo 'found';
}
If the count of that array is at least 1, then there's at least one element in common. If it's equal to your dynamic array's length, and the array's values are distinct, then they're all in there.
And, of course, the array will be able to tell you which values matched.
Because there is no element in your array containing array('enroll') in it (only 'enroll').
Your best bet would be to use array_diff(), if the result is the same as the original array, no match is found.
You are interchanging the parameters for in_array (needle & haystack). It needs to be,
if(in_array($this->_acceptable, $location_split))
{
echo 'found';
}
Edit: Try using array_intersect.
Demo
I am looping between 2 times in 10 minute intervals, which works fine and outputs a time like so:
<?=$time->format('H:i')?>
I then am pulling data from the database of times I then want to see if the data in the loop matches whats coming out of the database. I have created a method to get me all the records from the database and outputs them into an array. I then wanted to use in_array to match them up then run the value through another method to get data about it. Problem is that it doesnt match up, problem being:
if (array_search($time->format('H:i'), $bookings))
echo "Match";
$booking is a multi-dimension array looking like:
Array (
[0] => Array ( [id] => 1 [time] => 12:00 )
[1] => Array ( [id] => 2 [time] => 15:00 )
...
)
Thanks in advance!
It would be much easier if you get the values directly from database. still if you want to process it in php, you can try with array_walk(). I am not sure about the syntax but should be something like
function search($value, $key, $needle)
{
array_search($needle, $value);
}
array_walk($bookings, 'search', $time->format('H:i'));
where $value will be your inner arrays.
Guys, please correct me if i am wrong with the syntax
It would probably be simpler to directly query the database accordingly - if you can - and than your query would be something like
select * from `table_name` WHERE `date_field` = $your_date
If that does not form a solution you can use array_walk as above or simply loop a little more:
foreach($bookings as $array) {
if(array_search($time->format('H:i'), $array)) {
echo 'match';
// If you don't want to keep searching use 'break'.
}
}
I am checking a list of 10 spots, each spot w/ 3 top users to see if a user is in an array before echoing and storing.
foreach($top_10['top_10'] as $top10) //loop each spot
{
$getuser = ltrim($top10['url'], " users/" ); //strip url
if ($usercount < 3) {
if ((array_search($getuser, $array) !== true)) {
echo $getuser;
$array[$c++] = $getuser;
}
else {
echo "duplicate <br /><br />";
}
}
}
The problem I am having is that for every loop, it creates a multi-dimensional array for some reason which only allows array_search to search the current array and not all combined. I am wanting to store everything in the same $array. This is what I see after a print_r($array)
Array ( [0] => 120728 [1] => 205247 ) Array ( [0] => 232123 [1] => 091928 )
There seems to be more to this code. As there are variables being called in it that aren't defined, such as $c, $usercount, etc.. And using array_search with the 2nd parameter of $array if it doesn't exist is not a good idea also. Since it seems like $array is being set within the if statement for this only.
And you don't seem to be using the $top10 value within the foreach loop at all..., why is this?
It would help to see more of the code for me to be able to help you.