Good day.
I would be like get 3 keys from $arr where value $arN[0] will be more than other...
Code:
$ar1=array(201,281);
$ar2=array(1252760,1359724);
$ar3=array(452760,34349724);
$ar4=array(1260,134344);
$ar5=array(232750,1359724);
$ar6=array(60,1439724);
$arr[]=array(6299927 => $ar1);
$arr[]=array(1252760 => $ar2);
$arr[]=array(3432444 => $ar3);
$arr[]=array(3435543 => $ar4);
$arr[]=array(7645466 => $ar5);
$arr[]=array(4574534 => $ar6);
Next function sorting array $a descending:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? 1 : -1;
}
$a = array(3, 2, 5, 6, 1);
usort($, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}
outpoot: 0:6 1:5 2:4 3:2 4:1
But how change this function for my example(for my big array)?
Tell me please how make this?
How write right?
Very confusing but I think you are looking for something like this. It will give you the index of the array whose first item is greater than the first item in the next array.
foreach($arr as $key => $array) {
if($array[0] > $arr[$key+1][0]) {
echo $key;
}
}
$dates = array();
foreach ($arr as $key => $row) {
$subkeys = array_keys($row); // Elements are one-element associative arrays
$dates[$key] = $row[$subkeys[0]][0]; // Get the element, then get its [0] sub-element
}
array_multisort($dates, SORT_DESC, $arr);
print_r($arr);
$ans_array= array();
// for shuffle array without repeat
for($q=0;$q<count($arr);$q++)
{
$n = rand(0,count($arr));
if($ans_array[$n] == NULL || $ans_array[$n] == '')
{
$ans_array[$n] = $arr[$q];
}
else
{ if($q==0) $q=0;
else $q = $q-1;
}
}
Related
Let's say I Have two arrays.
$arr1 = ['A','B','C','D'];
$arr2 = ['C','D'];
now compare two arrays.if there is no match for value of $arr1 in $arr2 then index is left empty.
for the above arrays output should be:
$arr3 = ['','','C','D']
I tried array_search() function.But couldn't achieve desired output.
Any possible solutions?
You can use foreach with in_array and array_push like:
$arr1 = ['A','B','C','D'];
$arr2 = ['C','D'];
$arr3 = [];
foreach($arr1 as $value){
$arr3[] = (in_array($value, $arr2)) ? $value : '';
}
print_r($arr3);
/*
Result
Array
(
[0] =>
[1] =>
[2] => C
[3] => D
) */
Foreach the first array then test with in_array if exist, if true push into array 3 else push empty value
You can use the following function. In the following code, the function search_in_array return true and false based on the searching within the 2nd array. So you can push the empty or searched value in final array.
<?php
$arr1 = array('A','B','C','D');
$arr2 = array('C','D');
$arr3 = array();
function search_in_array ($value, $array)
{
for ($i=0; $i<count($array); $i++)
{
if ($value == $array[$i])
{
return true;
}
}
return false;
}
for ($i=0; $i<count($arr1); $i++)
{
$value = $arr1[$i];
$result = search_in_array ($value, $arr2);
if ($result)
{
array_push ($arr3, $value);
}
else
{
array_push ($arr3, '');
}
}
print_array($arr3);
?>
I have this array where the first two elements are integers and the third element is another array containing 2 arrays, each with one element. I can loop through the first 2 elements fine but not the third element.
I have tried using a second foreach loop and also a for loop containing a foreach loop.
$rows = array(1, 2, "qa" => array("q" => array(1,2), "b" => array(3,4)));
$f=1;
foreach($rows as $r) {
if($f == 1){
$e = $rows[0];
$f=$f+1;
}
if($f == 2){
$u = $rows[1];
}
if($r == "qa"){
$c = $value["q"];
$d = $value["b"];
}
}
echo $e;
echo $u;
print_r($c);
print_r($d);
I hope this helps:
$rows = array(1, 2, "qa" => array("q" => array(1,2), "b" => array(3,4)));
view($rows);
function view($arr) {
foreach ($arr as $value) {
if (is_array($value))
view($value);
else
echo $value;
}
}
You could use array_walk_recursive
array_walk_recursive($rows, function($v){echo $v;});
DEMO: https://3v4l.org/ITXAu
I have an array like the one below:
$v = array(1,2,3,4,2,3);
How do I get the keys of all elements in array where the value is equal to 2?
If you have a value in an array and you want to get the keys you can use array_keys() with the optional search_value:
$v = array(1,2,3,4,2,3);
$keys = array_keys($v, '2');
print_r($keys);
// Array
// (
// [0] => 1
// [1] => 4
// )
For the output check https://3v4l.org/N8EBH
You can do it like
<?php
$v = array(1,2,3,4,2,3);
$keys = array();
foreach($v as $k=>$x)
{
if($x == 2)
$keys[] = $k;
}
echo "<pre>";print_r($keys);echo "</pre>";
Try this code
$v = array(1,2,3,4,2,3);
$a = 2;
$key = array();
foreach($v as $k=>$val)
{
if($a == $val)
{
$key[] = $k;
}
}
print_r($key);
Suppose, we have 2 associative arrays:
<?php
$array1 = array(
1 => 2,
2 => 1,
);
$array2 = array(
2 => 1,
1 => 2,
);
They contain same elements, but in different order. I wanted to write a comparison function, that will give true only if:
Arrays have the same key => value pairs.
Order of pairs is the same.
So, I tried the following:
1 try
if ($array1 == $array2)
{
print "equal\n";
}
Prints: equal
2 try
print count(array_diff_assoc($array1, $array1));
Prints: 0
My custom function
Then I created the following function:
function compare(&$array1, &$array2)
{
$n1 = count($array1);
$n2 = count($array2);
if ($n1 == $n2)
{
$keys1 = array_keys($array1);
$keys2 = array_keys($array2);
for ($i = 0; $i < $n1; ++$i)
{
if ($keys1[$i] == $keys2[$i])
{
if ($array1[$keys1[$i]] != $array2[$keys2[$i]])
{
return false;
}
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
This works correctly, but it won't work, when we want this strict comparison applied for nested arrays of arrays because of != operator in this if:
if ($array1[$keys1[$i]] != $array2[$keys2[$i]])
{
return false;
}
This can be fixed, using a recursive function, switching by type of data. But this simplified version was ok for me.
Is there any standard solution for this type of comparison?
As described under array operators, what you want is the === equality operator.
if ($array1 === $array2) {
echo "same key pairs and same element order\n";
}
can you try the this below one. which returns true
if the key=>value and will be in any order
otherwise returns false
$array1 = array(
1 => 2,
2 => 1);
$array2 = array(
2 => 1,
1 => 2);
var_dump(compareArray($array1,$array2));
function compareArray ($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if ($array2[$key] != $value)
{
return false;
}
}
return true;
}
If i have two array as following:
$array1 = array(array('id'=>11,'name'=>'Name1'),array('id'=>22,'name'=>'Name2'), array('id'=>33,'name'=>'Name3'),array('id'=>44,'name'=>'Name4'),array('id'=>55,'name'=>'Name5'));
$array2 = array(array('id'=>22,'name'=>'Name2'),array('id'=>55,'name'=>'Name5'));
My expect result, the array2 should be always at the beginning :
$newarray = array(array('id'=>22,'name'=>'Name2'),array('id'=>55,'name'=>'Name5'), array('id'=>11,'name'=>'Name1'), array('id'=>33,'name'=>'Name3'),array('id'=>44,'name'=>'Name4'));
My current solution is using two for loops:
foreach($array2 as $Key2 => $Value2) {
foreach($array1 as $Key1 => $Value1){
if($Value1['id'] != $Value2['id']) {
//push array
}
}
}
Edit:
The result "$newarray" should not include the duplicate ids from array1.
But i am looking for a faster and simpler solution.
SOLUTION:
$a1 = array();
foreach ($array1 as $v) $a1[$v['uuid']] = $v;
$a2 = array();
foreach ($array2 as $v) $a2[$v['uuid']] = $v;
$filtered = array_values(array_diff_key($a1, $a2));
//print_r($filtered);
$newarray = array_merge($array2, $filtered);
Thank you guys!!!!
Thanks.
Regards Jack
you want $new_array = array_merge($array2, $array1); puts the second array onto the end of the first one
You can use array_merge() function for that, like this:
$newarray = array_merge($array2, $array1);
I am not sure about your requirement but to sort multi-dimensional array on some specific key
You need to use usort function
Try the code below:
$cmp = function ($a, $b){
$a_val = $a['id'];
$b_val = $b['id'];
if ( $a_val == $b_val) {
return 0;
}
return ($a_val < $b_val) ? -1 : 1;
};
usort($array2,$cmp);
$array2 will be sorted by 'id'