I have this array which contains 2 keys and values..
array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
)
So How can I convert this into single query with distinct values like this
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
20 => 14,
)
That means it should be first merge and then create distinct values array without using more foreach/for loop.
This is the code I have tried http://codepad.org/x881cBt1
You can use array_walk_recursive to flatten your multidimensional array:
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);
This should give:
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
Check EVAL
<?php
$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
$required_data = array_merge($required_data, $data_set[$i]);
unset($data_set[$i]);
}
var_dump($required_data);
This will solve the issue
$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}
$final_arr=array_unique($final_arr);
print_r($final_arr);
see it live ideone
//Here 1st you have to Merge array & then remove duplicate entry
<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));
$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}
//var_dump($temp_array);
$final_array=array_unique($temp_array);
echo "array:";
var_dump($final_array);
?>
you can use this code as you said this code is without any loop
$myArray = array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
);
$objTmp = (object) array('array' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);
print_r (array_unique($objTmp->array));
/* output
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
*/
Thank you..
Related
hi all i have 2 arrays
[0] => CD Alaves
[1] => Granada CF
[2] => Getafe
[3] => CD Leganes
[4] => Barcelona
[5] => Getafe
[6] => Atletico Madrid
[7] => Getafe
[8] => Sevilla
[9] => Athletic Bilbao
[10] => CD Leganes
and their corresponding values
[0] => 11
[1] => 11
[2] => 11
[3] => 11
[4] => 11
[5] => 10
[6] => 10
[7] => 10
[8] => 10
[9] => 10
[10] => 9
I am currently using
$teamdata=array_combine($clubs,$stats);
echo "<pre>";
print_r($teamdata);
echo "</pre>";
however it outputs as
[CD Alaves] => 1
[Granada CF] => 1
[Getafe] => 1
[CD Leganes] => 1
[Barcelona] => -
[Atletico Madrid] => 1
[Sevilla] => 1
[Athletic Bilbao] => -
[Real Betis] => 1
[Espanyol] => 1
[Osasuna] => 1
[Villarreal] => 1
[Real Madrid] => 1
[Levante] => 1
giving unique values on the left and 1s on the right
it should read
[CD Alves]=>11
[Granada CF]=>11
....
Thanks a lot to anyone who sees this and takes the time to respond.
As long as your two originating arrays are valid, then array_combine works just fine:
<?php
$arr1 = array(
0 => "CD Alaves",
1 => "Granada CF",
2 => "Getafe",
3 => "CD Leganes",
4 => "Barcelona",
5 => "Getafe",
6 => "Atletico Madrid",
7 => "Getafe",
8 => "Sevilla",
9 => "Athletic Bilbao",
10 => "CD Leganes"
);
$arr2 = array(
0 => 11,
1 => 11,
2 => 11,
3 => 11,
4 => 11,
5 => 10,
6 => 10,
7 => 10,
8 => 10,
9 => 10,
10 => 9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Gives:
Array
(
[CD Alaves] => 11
[Granada CF] => 11
[Getafe] => 10
[CD Leganes] => 9
[Barcelona] => 11
[Atletico Madrid] => 10
[Sevilla] => 10
[Athletic Bilbao] => 10
)
A working example: http://sandbox.onlinephpfunctions.com/code/a6da80745eff9ff9b598fe4a2d7fbd8d71d3fb08
Even if your arrays are single values without explicit keys, it will work (php adds numeric indexes anyway)
<?php
$arr1 = array(
"CD Alaves",
"Granada CF",
"Getafe",
"CD Leganes",
"Barcelona",
"Getafe",
"Atletico Madrid",
"Getafe",
"Sevilla",
"Athletic Bilbao",
"CD Leganes"
);
$arr2 = array(
11,
11,
11,
11,
11,
10,
10,
10,
10,
10,
9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Prints the same output as above.
I had the exact same problem and this worked for me. Please mark this answer as accepted if it works for you.
Code from #axiac on https://stackoverflow.com/a/46671863/19402660
// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
// Build the result here
$Array3 = [];
// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
// If the key $key was not encountered yet then add it to the result
if (! array_key_exists($key, $Array3)) {
$Array3[$key] = 0;
}
// Add the value associate with $key to the sum in the results array
$Array3[$key] += $Array2[$index];
}
print_r($Array3);
Its output:
Array
(
[bus] => 20
[int] => 10
)
I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.
I tried to make table from PHP array. But here all key are dynamic and have also child array
Here is the array structure:
array (
1371618448317 =>
array (
0 =>
array (
0 => '23.77311734',
1 => '90.396355125',
2 => '23.77313316',
3 => '90.396411867187',
4 => '23.77309048',
5 => '90.396419484375',
6 => '23.77307348',
7 => '90.3963645',
),
1 => 20911,
2 =>
array (
1371618713208 =>
array (
0 => 1,
1 => 'BRAC Delivery Centre',
2 => '371/A Shahinbag',
3 => 25,
4 => 91,
5 => 221,
6 => 1,
7 => 11,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 1,
),
),
),
1371619410448 =>
array (
0 =>
array (
0 => '23.77894566',
1 => '90.39968559375',
2 => '23.77916362',
3 => '90.400307765625',
4 => '23.77889887',
5 => '90.400401515625',
6 => '23.77870083',
7 => '90.399780515625',
),
1 => 24612,
2 =>
array (
1371619950162 =>
array (
0 => 1,
1 => 'EPI Centre (Govt.)',
2 => ' Mohakhali Road Mohakhali',
3 => 20,
4 => 91,
5 => 11,
6 => 1,
7 => 12,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 0,
),
),
),
1371621080807 =>
array (
0 =>
array (
0 => '23.77746206',
1 => '90.399232078125',
2 => '23.77744917',
3 => '90.399623390625',
4 => '23.77712934',
5 => '90.39958940625',
6 => '23.77714809',
7 => '90.399205125',
),
1 => 24566,
2 =>
array (
1371621897771 =>
array (
0 => 1,
1 => 'Society for Assistance to Hearing Impaired Children (SAHIC)',
2 => 'N/A Sattola Road Mohakhali',
3 => 20,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 99,
13 => 99,
14 => 99,
15 => 1,
16 => 0,
),
),
),
1371622305777 =>
array (
0 =>
array (
0 => '23.77357261',
1 => '90.36189965625',
2 => '23.77359605',
3 => '90.36197925',
4 => '23.77344028',
5 => '90.36201675',
6 => '23.77341802',
7 => '90.361931296875',
),
1 => 1325,
2 =>
array (
1371622497359 =>
array (
0 => 1,
1 => 'Natoinal Health Care Network',
2 => '3/Ka Pisiculture Housing Society Shamoly',
3 => 29,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 99,
10 => 99,
11 => 1,
12 => 99,
13 => 99,
14 => 1,
15 => 1,
16 => 0,
),
),
)
My Desire tale will be look like:
BRAC Delivery Centre 371/A Shahinbag 23.77311734 90.396355125
EPI Centre (Govt.) Mohakhali Road 23.77746206 90.399232078125
from lat long array I always try to collect first lat long.
Can any one give me solution ?
Here is your solution
Input
$array = array(
1371618448317 => array (
array ('23.77311734','90.396355125','23.77313316','90.396411867187','23.77309048','90.396419484375','23.77307348','90.3963645'),
20911,
array ('1371618713208' => array (1,'BRAC Delivery Centre','371/A Shahinbag',25,91,221,1,11,1,99,1,99,99,99,99,99,1))
),
1371619410448 => array (
array ('23.77894566','90.39968559375','23.77916362','90.400307765625','23.77889887','90.400401515625','23.77870083','90.399780515625',),
24612,
array ('1371619950162' => array (1,'EPI Centre (Govt.)',' Mohakhali Road Mohakhali',20,91,11,1,12,1,99,1,99,99,99,99,99,0))
),
1371621080807 => array(
array ('23.77746206','90.399232078125','23.77744917','90.399623390625','23.77712934','90.39958940625','23.77714809','90.399205125'),
24566,
array ('1371621897771' => array (1,
'Society for Assistance to Hearing Impaired Children (SAHIC)','N/A Sattola Road Mohakhali',20,91,222,1,7,1,1,1,1,99,99,99,1,0)
)
),
1371622305777 => array (
array ('23.77357261','90.36189965625','23.77359605','90.36197925','23.77344028','90.36201675','23.77341802','90.361931296875'),
1325,
array ('1371622497359' => array (1,
'Natoinal Health Care Network','3/Ka Pisiculture Housing Society Shamoly',29,91,222,1,7,1,99,99,1,99,99,1,1,0)
)
)
);
Solution
Coordinates are in 1st array.
Name and address is in 3rd.
Coordinates picks easily by $row[0][0].','.$row[0][1]; in given loop below.
For address and name you need to pick key of 3rd array, for this here I used $row[2][key($row[2]);
Now check the code below.
$new_array = array();
$i=0;
foreach($array as $key => $row){
//echo "<pre>";print_r(key($row[2]));
$new[$i]['coordinates'] = $row[0][0].','.$row[0][1];
$new[$i]['name'] = $row[2][key($row[2])][1];
$new[$i]['address'] = $row[2][key($row[2])][2];
$i++;
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[coordinates] => 23.77311734,90.396355125
[name] => BRAC Delivery Centre
[address] => 371/A Shahinbag
)
[1] => Array
(
[coordinates] => 23.77894566,90.39968559375
[name] => EPI Centre (Govt.)
[address] => Mohakhali Road Mohakhali
)
[2] => Array
(
[coordinates] => 23.77746206,90.399232078125
[name] => Society for Assistance to Hearing Impaired Children (SAHIC)
[address] => N/A Sattola Road Mohakhali
)
[3] => Array
(
[coordinates] => 23.77357261,90.36189965625
[name] => Natoinal Health Care Network
[address] => 3/Ka Pisiculture Housing Society Shamoly
)
)
solved myself:
$assoc = true;
$result = json_decode ($json, $assoc);
echo "<table border='1'>";
echo "<tr><td>Hospital Name</td><td>Address</td><td>Lat1</td><td>Long1</td><td>Lat2</td><td>Long1</td><td>Lat3</td><td>Long3</td><td>Lat4</td><td>Long4</td></tr>";
foreach($result as $single_res){
$lat1 = $single_res[0][0];
$long1 = $single_res[0][1];
$lat2 = $single_res[0][2];
$long2 = $single_res[0][3];
$lat3 = $single_res[0][4];
$long3 = $single_res[0][5];
$lat4 = $single_res[0][6];
$long4 = $single_res[0][7];
$key = $single_res[2];
$key_val = array_keys($key);
$key_val = $key_val[0];
$name = $key[$key_val][1];
$address = $key[$key_val][2];
I have problem with matrix operation in php.
I have two multidimensional array
array1=
Array([0] => Array([0] => 5000, [1] => 6, [2] => 325, [3] => 3, [4] => 3, [5] => 517000000)
[1] => Array([0] => 20000, [1] => 5, [2] => 217, [3] => 5, [4] => 3, [5] => 1692000000)
[2] => Array([0] => 12150, [1] => 3, [2] => 254, [3] => 4, [4] => 4, [5] => 1370520000)
[3] => Array([0] => 4200, [1] => 4, [2] => 351, [3] => 3, [4] => 2, [5] => 394800000)
[4] => Array([0] => 24700, [1] => 7, [2] => 237, [3] => 3, [4] => 4, [5] => 2089620000)
[5] => Array([0] => 18500, [1] => 5, [2] => 314, [3] => 5, [4] => 4, [5] => 1739000000)
[6] => Array([0] => 12150, [1] => 5, [2] => 247, [3] => 3, [4] => 3, [5] => 1142100000)
[7] => Array([0] => 15000, [1] => 5, [2] => 307, [3] => 4, [4] => 3, [5] => 1410000000)
[8] => Array([0] => 10000, [1] => 4, [2] => 231, [3] => 4, [4] => 4, [5] => 940000000)
[9] => Array([0] => 27500, [1] => 6, [2] => 347, [3] => 5, [4] => 4, [5] => 2147483647))
the table display is
5000 6 325 3 3 517000000
20000 5 217 5 3 1692000000
12150 3 254 4 4 1370520000
4200 4 351 3 2 394800000
24700 7 237 3 4 2089620000
18500 5 314 5 4 1739000000
12150 5 247 3 3 1142100000
15000 5 307 4 3 1410000000
10000 4 231 4 4 940000000
27500 6 347 5 4 2147483647
and the second array is
array2=
Array(
[0] => Array
(
[0] => 27500
[1] => 7
[2] => 351
[3] => 3
[4] => 4
[5] => 394800000
))
then, I want to do mathematical operation for each column.
for column 0,1,2,4 the operation is array1[][]/array2[][].
and for column 3 and 5 the operation is array2[][]/array1[][]
how do I do that ?
I have tried for this function :
function perkalian_matriks($array1, $array2) {
$result= array();
for ($i=0; $i<sizeof($array1); $i++) {
for ($j=0; $j<sizeof($array2[0]); $j++) {
$temp = 0;
$temp1 = 0;
$temp2 = 0;
for ($k=0; $k<sizeof($array2); $k++) {
if(isset($array1[$i][3]) || isset($array1[$i][5])){
$temp1 += ($array2[0][$j]/$array1[$i][$j])/2;
}else{
$temp2 += $array1[$i][$j]/$array2[0][$j];
}
$temp += $array1[$i][$j]/$array2[0][$j];
}
$result[$i][$j] = $temp;
$result1[$i][$j] = $temp1;
$result2[$i][$j] = $temp2;
}
}
return $result;
return $temp;
return $result1;
return $result2;
}
But the final result that I expected is
0.181818 0.857143 0.925926 1 0.75 0.763636
0.727273 0.714286 0.618234 0.6 0.75 0.233333
0.441818 0.428571 0.723647 0.75 1 0.288066
0.152727 0.571429 1 1 0.5 1
0.898182 1 0.675214 1 1 0.188934
0.672727 0.714286 0.894587 0.6 1 0.227027
0.441818 0.714286 0.703704 1 0.75 0.345679
0.545455 0.714286 0.874644 0.75 0.75 0.28
0.363636 0.571429 0.65812 0.75 1 0.42
1 0.857143 0.988604 0.6 1 0.183843
Thank you.
first of all doing this:
return $result;
return $temp;
return $result1;
return $result2;
will execute every time just return $result; so that really ve no meaning.
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. ( from php doc )
The second array don't need to be a matrix.
anyway you can achieve what you need using 2 nested loop:
$array1=
array(array(5000,6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000),
array( 12150, 3, 254, 4, 4, 1370520000),
array( 4200, 4, 351, 3, 2, 394800000),
array( 24700, 7, 237, 3, 4, 2089620000),
array( 18500, 5, 314, 5, 4, 1739000000),
array( 12150, 5, 247, 3, 3, 1142100000),
array( 15000, 5, 307, 4, 3, 1410000000),
array( 10000, 4, 231, 4, 4, 940000000),
array( 27500, 6, 347, 5, 4, 2147483647));
$array2=array(27500,
7,
351,
3,
4,
394800000);
$action1 = array(0,1,2,4);
$action2 = array(3,5);
$result = array();
foreach($array1 as $rowNum => $row){
$result[] = array();
foreach($row as $colNum => $col){
if(in_array($colNum,$action1))
$result[$rowNum][] = $col/$array2[$colNum];
else if(in_array($colNum,$action2)){
$result[$rowNum][] = $array2[$colNum]/$col;
}
}
}
echo var_dump($result);
Working Example
It feels I am going way over my head while discovering the ultimate usage of arrays.
I have two arrays, where the first has main keys, and the value is a count of files attached to that key.
The goal is to match the keys of this first array to the values in a second array, but still mainting (and show) the (value)count of Array-1 -- but for only the values in the second array.
Seems somewhat hazy perhaps, but here are the arrays. The second one has the values that should match the keys in the first.
(My problem is that I keep losing the values of array 1 with every attempt I make.)
Hope you can help me out with this one.
(working matches are keys like: 125, 2051 & 2214)
Array 1:
Array (
[6960] => 3
[2214] => 4
[2051] => 4
[6944] => 2
[6938] => 4
[1823] => 1
[766] => 6
[3993] => 4
[5896] => 6
[6927] => 2
[4220] => 3
[77] => 3
[83] => 1
[125] => 2
[6618] => 2
[196] => 1
[4072] => 12
[3718] => 1
[5918] => 1
[3388] => 10
[4500] => 13
[5968] => 2
[3000] => 2
[942] => 1
[4246] => 8
[5868] => 2
[6394] => 3
[1168] => 1
[2163] => 1
[1827] => 2
[2071] => 8
[4597] => 1
[1702] => 7
)
Array 2:
Array (
[0] => 1024
[1] => 1076
[2] => 111
[3] => 124
[4] => 125
[5] => 1301
[6] => 1409
[7] => 2051
[8] => 2214
[9] => 2636
[10] => 3246
[11] => 4838
[12] => 6946
[13] => 6955
[14] => 6961
[15] => 73
[16] => 74
[17] => 8
)
What about doing this:
<?php
$arr1 = array(1 => 1000, 500 => 1111, 1000 => 5000, 5000 => 5555);
$arr2 = array(1, 5000);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
(
[1] => 1000
[5000] => 5555
)
Or, using your data:
<?php
$arr1 = array(6960 => 3, 2214 => 4, 2051 => 4, 6944 => 2, 6938 => 4, 1823 => 1, 766 => 6, 3993 => 4, 5896 => 6, 6927 => 2, 4220 => 3, 77 => 3, 83 => 1, 125 => 2, 6618 => 2, 196 => 1, 4072 => 12, 3718 => 1, 5918 => 1, 3388 => 10, 4500 => 13, 5968 => 2, 3000 => 2, 942 => 1, 4246 => 8, 5868 => 2, 6394 => 3, 1168 => 1, 2163 => 1, 1827 => 2, 2071 => 8, 4597 => 1, 1702 => 7);
$arr2 = array(1024, 1076, 111, 124, 125, 1301, 1409, 2051, 2214, 2636, 3246, 4838, 6946, 6955, 6961, 73, 74, 8);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
Array
(
[2214] => 4
[2051] => 4
[125] => 2
)
array_interset_keys will find the intersection of arrays by keys, not values. Since your second array is an index based array (not an associative array) we need to first flip the keys and values using array_flip. Then the keys can be intersected.
Your question is somewhat unclear, but I think this is what you're looking for:
foreach( $array2 as $key)
{
$count = ( isset( $array1[ $key ]) ? $array1[ $key ] : 0);
echo $key . ' has ' . $count . ' files.';
}
Uhhmm.. i cant seem to understand what you want to imply.. but from the way i see it.. if you want to have the keys of array 1 as value to array 2.. just do this code..
foreach($array1 as $key=>$val) {
$array2[] = $key;
}
This should grab the KEYS of array1 and insert it to your array2[].
Hope this helps you.. Cheers :)
This should print out what you need:
foreach($array2 as $key=>$val) {
echo $val;
foreach($array1 as $key2 => $val2){
if($key == $val2){
echo $val2;
}
}
echo '\n'; // new line
}