Related
I have the following array
Array
(
[22] => Array
(
[0] => 1074
[1] => 1926
)
[1772] => Array
(
[0] => 1080
[1] => 1921
)
[1926] => Array
(
[0] => 1772
)
[1080] => Array
(
[0] => 1833
)
)
I want all the data related to key 1926. Here in above 1926 has value 1772 then in this key has 2 values 1080 and 1921 I have to check both keys and so on.
The result I want:
array(
[0]=>1772
[1]=>1080
[2]=>1921
[3]=>1833
.....
)
I have tried many solutions, but I am not getting desired response, I have created the below code that returns me only one value 1772
function arrayTraverse($targetKey, $array, $returndata=[])
{
foreach($array as $key=>$values){
if($targetKey == $key){
unset($array[$targetKey]);
if(is_array($values))
{
foreach($values as $val){
$returndata = array_merge($returndata, array($val));
arrayTraverse($val, $array, $returndata);
}
}
}
}
return $returndata;
}
Here at last I am getting only [0]=>1772
foreach array
foreach subArray
if isset(array[value])
subArray[key] &= array[value]
I solve the above mine problem, below is the code that will get the exact data
function arrayTraverse($reportingusersArr, $targetkey, $resultarr=[], $checkvalues=[]){
$uservalues = $reportingusersArr[$targetkey];
if($uservalues){
unset($reportingusersArr[$targetkey]);
$resultarr[$targetkey] = $uservalues;
$lastResultvalue = $uservalues;
foreach($uservalues as $keya=>$uva){
$checkvalues[] = $uva;
}
foreach($uservalues as $key=>$uv){
$elementkey = array_search($targetkey, $checkvalues);
unset($checkvalues[$elementkey]);
return arrayTraverse($reportingusersArr, $uv, $resultarr, $checkvalues);
}
}
else{
$othercheckval = $checkvalues;
foreach($checkvalues as $key3=>$uv2){
$elementkey = array_search($uv2, $othercheckval);
unset($othercheckval[$elementkey]);
return arrayTraverse($reportingusersArr, $uv2, $resultarr, $othercheckval);
}
}
$finalarray = [];
foreach($resultarr as $fkey=>$fvaluearr){
foreach($fvaluearr as $fval)
{
$finalarray[] = $fval;
}
}
return $finalarray;
}
I have an array which as dynamic nested indexes in e.g. I am just using 2 nested indexes.
Array
(
[0] => Array
(
[0] => 41373
[1] => 41371
[2] => 41369
[3] => 41370
)
[1] => Array
(
[0] => 41378
[1] => 41377
[2] => 41376
[3] => 41375
)
)
Now I want to create a single array like below. This will have 1st index of first array then 1st index of 2nd array, 2nd index of first array then 2nd index of 2nd array, and so on. See below
array(
[0] =>41373
[1] => 41378
[2] => 41371
[3] => 41377
[4] => 41369
[5] => 41376
[6] => 41370
[7] => 41375
)
You can do something like this:
$results = [];
$array = [[1,2,3,4], [1,2,3,4], [1,2,3,4]];
$count = 1;
$size = count($array)-1;
foreach ($array[0] as $key => $value)
{
$results[] = $value;
while($count <= $size)
{
$results[] = $array[$count][$key];
$count++;
}
$count = 1;
}
I think you need something like this:
function dd(array $arrays): array
{
$bufferArray = [];
foreach($arrays as $array) {
$bufferArray = array_merge_recursive($bufferArray, $array);
}
return $bufferArray;
}
$array1 = ['41373','41371','41369','41370'];
$array2 = ['41378','41377', '41376', '41375'];
$return = array();
$count = count($array1)+count($array2);
for($i=0;$i<($count);$i++){
if($i%2==1){
array_push($return, array_shift($array1));
}
else {
array_push($return, array_shift($array2));
}
}
print_r($return);
first count the arrays in the given array, then count the elements in the first array, than loop over that. All arrays should have the same length, or the first one should be the longest.
$laArray = [
['41373','41371','41369','41370'],
['41378', '41377', '41376', '41375'],
['43378', '43377', '43376', '43375'],
];
$lnNested = count($laArray);
$lnElements = count($laArray[0]);
$laResult = [];
for($lnOuter = 0;$lnOuter < $lnElements; $lnOuter++) {
for($lnInner = 0; $lnInner < $lnNested; $lnInner++) {
if(isset($laArray[$lnInner][$lnOuter])) {
$laResult[] = $laArray[$lnInner][$lnOuter];
}
}
}
this would be the simplest solution:
$firstarr = ['41373','41371','41369','41370'];
$secondarr = ['41378','41377','41376','41375'];
$allcounged = count($firstarr)+count($secondarr);
$dividedintotwo = $allcounged/2;
$i = 0;
while ($i<$dividedintotwo) {
echo $firstarr[$i]."<br>";
echo $secondarr[$i]."<br>";
$i++;
}
Here i want to find unique values,so i am writing code like this but i can't get answer,for me don't want to array format.i want only string
<?php
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$unique_array = array(); // unique array
$duplicate_array = array(); // duplicate array
foreach ($array as $key=>$value){
if(!in_array($value,$unique_array)){
$unique_array[$key] = $value;
}else{
$duplicate_array[$key] = $value;
}
}
echo "unique values are:-<br/>";
echo "<pre/>";print_r($unique_array);
echo "duplicate values are:-<br/>";
echo "<pre/>";print_r($duplicate_array);
?>
You can use array_unique() in single line like below:-
<?php
$unique_array = array_unique($array); // get unique value from initial array
echo "<pre/>";print_r($unique_array); // print unique values array
?>
Output:- https://eval.in/601260
Reference:- http://php.net/manual/en/function.array-unique.php
If you want in string format:-
echo implode(',',$final_array);
Output:-https://eval.in/601261
The way you want is here:-
https://eval.in/601263
OR
https://eval.in/601279
I am baffled by your selection as the accepted answer -- I can't imagine how it can possibly satisfy your requirements.
array_count_values() has been available since PHP4, so that is the hero to call upon here.
Code: (Demo)
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$counts = array_count_values($array); // since php4
foreach ($counts as $value => $count) {
if ($count == 1) {
$uniques[] = $value;
} else {
$duplicates[] = $value;
}
}
echo "unique values: " , implode(", ", $uniques) , "\n";
echo "duplicate values: " , implode(", ", $duplicates);
Output:
unique values: raja, mahi
duplicate values: kani, yuvi
To get unique values from array use array_unique():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
array_unique($array);
print_r(array_unique($array));
Output:
Array
(
[0] => 1
[1] => 2
[3] => 5
[4] => 10
[7] => 7
[8] => 9
)
And to get duplicated values in array use array_count_values():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
print_r(array_count_values($array));
Output:
Array
(
[1] => 3 // 1 is duplicated value as it occurrence is 3 times
[2] => 1
[5] => 2 // 5 is duplicated value as it occurrence is 3 times
[10] => 2 // 10 is duplicated value as it occurrence is 3 times
[7] => 1
[9] => 1
)
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1)); // will print unique values
echo "<br>";
arr_count_val($arr1); // will print array with duplicate values
function arr_unique($arr) {
sort($arr);
$curr = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
function arr_count_val($arr) {
$uni_array = arr_unique($arr1);
$count = 0;
foreach($uni_array as $n1) {
foreach($arr as $n1) {
if($n1 == $n2) {
$count++;
}
}
echo "$n1"." => "."$count"."<br>";
$count=0;
}
}
?>
I have two arrays:
('admin','admin2', 'admin3' can be too many, and names can also differ other than 'admin')
$old = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$new = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
I want to have this array from both above arrays:
(new array with all different keys in both PLUS similar keys from new array)
$output = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
// Remove one level of array to make arrays as ['admin'=>array, 'admin2'=>array]
$old1 = call_user_func_array('array_merge', $old);
$new1 = call_user_func_array('array_merge', $new);
// Make replacement
$ready = array_replace($old1, $new1);
// Return level making every item as array
$result = array();
foreach($ready as $k=>&$v)
$result[] = array($k=>$v);
print_r($result);
demolink
This code will solve your problem :
<?php
$array1 = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$array2 = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
$output = $array1; ///merge array1 into output array
foreach($array2 as $key => $val)
{
$is_present_key = false;
$first_key = key($val);
foreach($output as $k => $v)
{
if(array_key_exists($first_key,$output[$k])) ////check if key exit in $output array
{
$output[$k] = $val; ///push new value if key exists in $output
$is_present_key = true;
}
}
if($is_present_key == false)///skip for duplicate of new values if key exists in $output
{
$output[] = $val;
}
}
echo "<pre>"; print_r($output);
?>
This will give you :
Array
(
[0] => Array
(
[admin] => Array
(
[a] => aaa
[b] => bbb
)
)
[1] => Array
(
[admin2] => Array
(
[e] => eee
[f] => fff
)
)
[2] => Array
(
[admin3] => Array
(
[g] => ggg
[h] => hhh
)
)
)
LIVE EXAMPLE
$old = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$new = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
$arr=array_merge($new,$old);
$new_arr=array();
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
if(array_key_exists($k, $new_arr)){
continue;
}else{
$new_arr[$k]=$v;
}
}
}
echo "<pre>";print_r($new_arr); echo "</pre>";
i am stuck at this stage of my project.
i am trying to get common values from four multidimensional arrays using array_intersect. can anyone help me with this issue ?
here are all four array:
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$res= array_intersect($arr,$arr1,$arr2,$arr3);
print_r($res);
If subarray contain one element always you could chage that value using array_map and current function.
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr = array_map('current', $arr); // getting first value of subarray
$arr1 = array_map('current', $arr1);
$arr2 = array_map('current', $arr2);
$arr3 = array_map('current', $arr3);
print_r($arr3);
// Array
// (
// [0] => 8159
// [1] => 8140
// [2] => 8134
// [3] => 8165
// [4] => 8166
// [5] => 8167
// [6] => 8168
// )
$res= array_intersect($arr,$arr1,$arr2,$arr3);
print_r($res);
// Array
// (
// [2] => 8134
// [3] => 8168
// )
Please check this
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
foreach($arr as $value)
{
$a1[] = $value[0];
}
foreach($arr1 as $value)
{
$a2[] = $value[0];
}
foreach($arr2 as $value)
{
$a3[] = $value[0];
}
foreach($arr3 as $value)
{
$a4[] = $value[0];
}
$res= array_intersect($a1,$a2,$a3,$a4);
print_r($res);