I have two arrays like given below from these two arrays I am getting
missing element that is also given below:
$news=[1,2,3,4,5,6,7];
$new=[1,2,4,6,7];
$missing=[3,5];
I want when I have two values or multi-values in that case if the condition
should work and for another value else part to be executed
if(in_array($news,$missing))
{
echo "ok";
}
else
{
echo "no";
}
case would be :[3] ,[5] and [3,5];
I want if array value =3 output should be no if value=5 output should
be=no if both then also output should be no but when value is
different like [4] or [4,6] output would be ok;
can anyone please help me related this I am stuck here. Or is there any way to solve this kind of problems
You can use array_intersect()
count(array_intersect($news,$missing))
If count > 0 means there is atleast one element in both arrays, so answer will be no. It count = 0, means both arrays have different values, so answer will be ok.
Related
I'm receiving this array of objects and need to iterate over it, but the problem is that I need to get the value of the next item when its iterating, to compare the value of the current object with the next one, and if it's a different value, split this array in two.
So, I was doing it with next() php function:
//looking for the next register in the array
$next = next($finances);
//first array if exist different values
$aiuaEd = [];
//second array if exist different values
$aiua = [];
foreach ($finances as $finance) {
if ($finance->cnpj <> $next->cnpj) {
$aiua[] = $finance;
} else {
$aiuaEd[] = $finance;
}
}
This code works fine at some point, but then I got this error:
Trying to get property 'cnpj' of non-object in
I don't know why sometimes works well and sometimes don't, debugging this variables, I found that if my array have 2 objects inside only, when I'm looping over it, the $next->cnpj variable came as empty, and sometimes don't.
Can someone help me with this?
I solved it with a different approach, instead of using php next(), I first loop over this array saving the cnpj's into an array.
$cnpjs = [];
foreach($finances as $finance){
$cnpj[] = $finance->cnpj;
}
Then I use array_unique() to group this 2 differents CNPJ's and sort() to get the correct keys order.
//grouping cnpjs as unique, should exist only 2 keys
$cnpj = array_unique($cnpj);
//sort array keys to get in order
sort($cnpj);
Then I iterate over my $finances array again, but now I'm counting if this $cnpj array has more than 2 positions, which means that I have to split this data in two differents arrays.
foreach($finances as $finance){
if(count($cnpj) > 1){
if($finance->cnpj == $cnpj[1]){
$aiua[] = $finance;
}else{
$aiuaEd[] = $finance;
}
}else{
$aiuaEd[] = $finance;
}
}
I'm pretty sure that this is not the best approach for that problem, or at least the most optimized one, so I'm open for new approach's suggestions!
Just posting how I solved my problem in case anyone having the same one.
Notice that this approach is only usable because I know that will not exist more than 2 different's CNPJ's in the array.
I have this code:
$fookerdos = '';
foreach (glob("records/*/*/kerdos.txt") as $somekerdos) {
$fookerdos .= file_get_contents($someposoA);
//to print them i you want
print $fookerdos;
So my problem that for this code will outputs many numbers becouse of many files.
for example will out output this
3.5 -6.7 6.68 -0.2 and so on..
now i want all this numbers to make them (addition)
i know how to addition some 2-3 variables, but i additions many numbers that I even dont know how many they are.
for example
print "3.5 + "-6.7" "6.68" "-0.2";
Thx :)
Does each file contain only a single number, or can they have more than one numbers?
From your previous edits, it seems as if one file contain only a number.
In that case, you can store the values in an array and sum the numbers using array_sum() or perform any other calculation as needed.
Here is a sample code for you:
$fookerdos = array ();
foreach (glob("records/*/*/kerdos.txt") as $somekerdos) {
$fookerdos[] = file_get_contents($somekerdos);
}
echo array_sum ($fookerdos);
$i=0;
foreach($tagss as $tagdetails)
{
if($i==0)
$tags_array[]["00"]=array("id"=>$i,"name"=>"all","type"=>"gift_finder","parent_id"=>null,"url"=>"all","readonly"=>"0","deleted"=>"0");
$tags_array[][$tagdetails->id]=array("id"=>$tagdetails->id,"name"=>$tagdetails->title,"type"=>"gift_finder","parent_id"=>null,"url"=>$tagdetails->title,"readonly"=>"0","deleted"=>"0");
$i++;
}
echo json_encode($tags_array);
my out put of above code is :-
[{"00":{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0","deleted":"0"}},{"1":{"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},{"2":{"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}}]
that is the right but i want the out put like (just need 0 instead of 00)
so i tried :-
$i=0;
foreach($tagss as $tagdetails)
{
if($i==0)
$tags_array[]["0"]=array("id"=>$i,"name"=>"all","type"=>"gift_finder","parent_id"=>null,"url"=>"all","readonly"=>"0","deleted"=>"0");
$tags_array[][$tagdetails->id]=array("id"=>$tagdetails->id,"name"=>$tagdetails->title,"type"=>"gift_finder","parent_id"=>null,"url"=>$tagdetails->title,"readonly"=>"0","deleted"=>"0");
$i++;
}
echo json_encode($tags_array);
ouput of the above code :-
[[{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0","deleted":"0"}],{"1":{"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},{"2":{"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}}]
that is logically right that it is put the first element in the array and treat next element at first index but i need 0 index separately
Any suggestion please
thanks in advance .
in summary I need
[{"0":{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0",
"deleted":"0"}},
{"1":"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},
{"2":"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}
}]
You just need to pass option to your json_encode() call. It is JSON_FORCE_OBJECT:
echo json_encode($tags_array, JSON_FORCE_OBJECT);
then you'll able to result in an object for numeric keys (so you don't need to pass keys from PHP at all). Please, note, that is available since PHP 5.3
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 have two arrays i.e$ar1=array("Mobile","shop","software","hardware");and$arr2=arry("shop","Mobile","shop","software","shop")
i want to compare the elements of arr2 to arr1 i.e
foreach($arr2 as $val)
{
if(in_array($val, $arr1))
{
//mycode to insert data into mysql table
variable++; // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table.
}
$query="update table set shop='$variable',Mobile='$variable'.......";
}
the $variable should be an integer value so that for later use i can use this variable(shop i.e in this example its value should be 3) to find the match.
My question is how can i get the variable that will increamented each time match found.
Sorry, I don't fully understand the purpose of your code. You can use array_intersect to get common values and array_diff to get the unique values when comparing two arrays.
i want to compare the elements of arr2 to arr1 i.e
Then you are essentially doing the same search for shop three times. It's inefficient. Why not sort and eliminate duplicates first?
Other issues. You are comparing arr2 values with the ones in arr1, which means the number of repetation for "shop" will not be 3 it will be one. Doing the opposite might give you the number of repetation of arr1[1] in arr2 =3.
There are multitude of ways to solve this problem. If efficiency is required,you might wish to sort so you don't have to go beyond a certain point (say s). You can learn to use indexes. Infact the whole datastructure is revolved around these kinds of things - from quick and dirty to efficient.
Not sure I understand the connection between your two arrays. But you can use this to count how many items are in your second array:
$items = array("shop","Mobile","shop","software","shop");
$count = array();
foreach($items as $item)
{
if(isset($count[$item]))
{
$count[$item]++;
}
else
{
$count[$item] = 1;
}
}
print_r($count); // Array ( [shop] => 3 [Mobile] => 1 [software] => 1 )