How to group array elements which has same index in php - php

I Have following array and
Array (
[0] => Array ( [70] => Array ( [0] => d ) )
[1] => Array ( [71] => Array ( [0] => a ) )
[2] => Array ( [71] => Array ( [0] => c ) )
[3] => Array ( [71] => Array ( [0] => d ) )
[4] => Array ( [72] => Array ( [0] => b ) )
[5] => Array ( [73] => Array ( [0] => a ) )
[6] => Array ( [73] => Array ( [0] => c ) )
[7] => Array ( [74] => Array ( [0] => a ) )
[8] => Array ( [75] => Array ( [0] => a ) )
)
I want to make the above array like as following one
Array (
[70] => Array ( [0] => c )
[71] => Array ( [1] => b [2] => c )
[72] => Array ( [2] => c )
[73] => Array ( [0] => a [2] => c )
[74] => Array ( [4] => c )
[75] => Array ( [5] => c )
)
Please help me how to make this in php?

Related

Calculate the sum of columns with PHP [duplicate]

This question already has answers here:
How to calculate the sum of variables from JSON data?
(1 answer)
PHP Sum up JSON Object value
(1 answer)
Closed 2 years ago.
I would like to calculate the sum of the numbers with highlight yellow color in a variable and the sum of the other values which are not highlighted in another variable.
My PHP code is:
$symbols=$request->Get('/_get?symbol=JAN&from=1586959285&to=1586959324&data=ba');
if($symbols!=false) {
echo $symbols;
}
and the result of this code is:
{"retcode":"0 Done","answer":[[1.09076,1.09084],[1.09077,1.09086],[1.09072,1.09081],[1.09073,1.09082],[1.09073,1.09083],[1.09074,1.09084],[1.09082,1.09084],[1.09090,1.09097],[1.09091,1.09099],[1.09091,1.09100],[1.09093,1.09101],[1.09094,1.09102],[1.09095,1.09104],[1.09097,1.09106],[1.09098,1.09107],[1.09098,1.09108],[1.09099,1.09108],[1.09097,1.09106],[1.09096,1.09106],[1.09096,1.09105],[1.09097,1.09105],[1.09096,1.09105],[1.09095,1.09105],[1.09095,1.09104],[1.09093,1.09102],[1.09095,1.09102],[1.09095,1.09104],[1.09095,1.09103],[1.09093,1.09102],[1.09082,1.09090],[1.09082,1.09092],[1.09083,1.09092],[1.09082,1.09091],[1.09081,1.09090],[1.09080,1.09089],[1.09081,1.09090],[1.09080,1.09089],[1.09081,1.09089],[1.09080,1.09089],[1.09080,1.09088],[1.09078,1.09087],[1.09079,1.09088],[1.09079,1.09089],[1.09075,1.09084],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09073,1.09083],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09073,1.09083],[1.09073,1.09082],[1.09073,1.09083],[1.09074,1.09083],[1.09073,1.09082],[1.09073,1.09083],[1.09073,1.09082],[1.09072,1.09081],[1.09071,1.09081],[1.09072,1.09081],[1.09072,1.09083],[1.09073,1.09083]]}
If I decode the JSON my code is:
$hugeArray = json_decode($symbols, true);
foreach ($hugeArray as $key => $value)
{
print_r($hugeArray);
}
And the result is:
Array ( [retcode] => 0 Done [answer] => Array ( [0] => Array ( [0] => 1.09076 [1] => 1.09084 ) [1] => Array ( [0] => 1.09077 [1] => 1.09086 ) [2] => Array ( [0] => 1.09072 [1] => 1.09081 ) [3] => Array ( [0] => 1.09073 [1] => 1.09082 ) [4] => Array ( [0] => 1.09073 [1] => 1.09083 ) [5] => Array ( [0] => 1.09074 [1] => 1.09084 ) [6] => Array ( [0] => 1.09082 [1] => 1.09084 ) [7] => Array ( [0] => 1.0909 [1] => 1.09097 ) [8] => Array ( [0] => 1.09091 [1] => 1.09099 ) [9] => Array ( [0] => 1.09091 [1] => 1.091 ) [10] => Array ( [0] => 1.09093 [1] => 1.09101 ) [11] => Array ( [0] => 1.09094 [1] => 1.09102 ) [12] => Array ( [0] => 1.09095 [1] => 1.09104 ) [13] => Array ( [0] => 1.09097 [1] => 1.09106 ) [14] => Array ( [0] => 1.09098 [1] => 1.09107 ) [15] => Array ( [0] => 1.09098 [1] => 1.09108 ) [16] => Array ( [0] => 1.09099 [1] => 1.09108 ) [17] => Array ( [0] => 1.09097 [1] => 1.09106 ) [18] => Array ( [0] => 1.09096 [1] => 1.09106 ) [19] => Array ( [0] => 1.09096 [1] => 1.09105 ) [20] => Array ( [0] => 1.09097 [1] => 1.09105 ) [21] => Array ( [0] => 1.09096 [1] => 1.09105 ) [22] => Array ( [0] => 1.09095 [1] => 1.09105 ) [23] => Array ( [0] => 1.09095 [1] => 1.09104 ) [24] => Array ( [0] => 1.09093 [1] => 1.09102 ) [25] => Array ( [0] => 1.09095 [1] => 1.09102 ) [26] => Array ( [0] => 1.09095 [1] => 1.09104 ) [27] => Array ( [0] => 1.09095 [1] => 1.09103 ) [28] => Array ( [0] => 1.09093 [1] => 1.09102 ) [29] => Array ( [0] => 1.09082 [1] => 1.0909 ) [30] => Array ( [0] => 1.09082 [1] => 1.09092 ) [31] => Array ( [0] => 1.09083 [1] => 1.09092 ) [32] => Array ( [0] => 1.09082 [1] => 1.09091 ) [33] => Array ( [0] => 1.09081 [1] => 1.0909 ) [34] => Array ( [0] => 1.0908 [1] => 1.09089 ) [35] => Array ( [0] => 1.09081 [1] => 1.0909 ) [36] => Array ( [0] => 1.0908 [1] => 1.09089 ) [37] => Array ( [0] => 1.09081 [1] => 1.09089 ) [38] => Array ( [0] => 1.0908 [1] => 1.09089 ) [39] => Array ( [0] => 1.0908 [1] => 1.09088 ) [40] => Array ( [0] => 1.09078 [1] => 1.09087 ) [41] => Array ( [0] => 1.09079 [1] => 1.09088 ) [42] => Array ( [0] => 1.09079 [1] => 1.09089 ) [43] => Array ( [0] => 1.09075 [1] => 1.09084 ) [44] => Array ( [0] => 1.09074 [1] => 1.09083 ) [45] => Array ( [0] => 1.09074 [1] => 1.09084 ) [46] => Array ( [0] => 1.09074 [1] => 1.09083 ) [47] => Array ( [0] => 1.09074 [1] => 1.09084 ) [48] => Array ( [0] => 1.09074 [1] => 1.09083 ) [49] => Array ( [0] => 1.09073 [1] => 1.09083 ) [50] => Array ( [0] => 1.09074 [1] => 1.09083 ) [51] => Array ( [0] => 1.09074 [1] => 1.09084 ) [52] => Array ( [0] => 1.09074 [1] => 1.09083 ) [53] => Array ( [0] => 1.09073 [1] => 1.09083 ) [54] => Array ( [0] => 1.09073 [1] => 1.09082 ) [55] => Array ( [0] => 1.09073 [1] => 1.09083 ) [56] => Array ( [0] => 1.09074 [1] => 1.09083 ) [57] => Array ( [0] => 1.09073 [1] => 1.09082 ) [58] => Array ( [0] => 1.09073 [1] => 1.09083 ) [59] => Array ( [0] => 1.09073 [1] => 1.09082 ) [60] => Array ( [0] => 1.09072 [1] => 1.09081 ) [61] => Array ( [0] => 1.09071 [1] => 1.09081 ) [62] => Array ( [0] => 1.09072 [1] => 1.09081 ) [63] => Array ( [0] => 1.09072 [1] => 1.09083 ) [64] => Array ( [0] => 1.09073 [1] => 1.09083 ) ) ) Array ( [retcode] => 0 Done [answer] => Array ( [0] => Array ( [0] => 1.09076 [1] => 1.09084 ) [1] => Array ( [0] => 1.09077 [1] => 1.09086 ) [2] => Array ( [0] => 1.09072 [1] => 1.09081 ) [3] => Array ( [0] => 1.09073 [1] => 1.09082 ) [4] => Array ( [0] => 1.09073 [1] => 1.09083 ) [5] => Array ( [0] => 1.09074 [1] => 1.09084 ) [6] => Array ( [0] => 1.09082 [1] => 1.09084 ) [7] => Array ( [0] => 1.0909 [1] => 1.09097 ) [8] => Array ( [0] => 1.09091 [1] => 1.09099 ) [9] => Array ( [0] => 1.09091 [1] => 1.091 ) [10] => Array ( [0] => 1.09093 [1] => 1.09101 ) [11] => Array ( [0] => 1.09094 [1] => 1.09102 ) [12] => Array ( [0] => 1.09095 [1] => 1.09104 ) [13] => Array ( [0] => 1.09097 [1] => 1.09106 ) [14] => Array ( [0] => 1.09098 [1] => 1.09107 ) [15] => Array ( [0] => 1.09098 [1] => 1.09108 ) [16] => Array ( [0] => 1.09099 [1] => 1.09108 ) [17] => Array ( [0] => 1.09097 [1] => 1.09106 ) [18] => Array ( [0] => 1.09096 [1] => 1.09106 ) [19] => Array ( [0] => 1.09096 [1] => 1.09105 ) [20] => Array ( [0] => 1.09097 [1] => 1.09105 ) [21] => Array ( [0] => 1.09096 [1] => 1.09105 ) [22] => Array ( [0] => 1.09095 [1] => 1.09105 ) [23] => Array ( [0] => 1.09095 [1] => 1.09104 ) [24] => Array ( [0] => 1.09093 [1] => 1.09102 ) [25] => Array ( [0] => 1.09095 [1] => 1.09102 ) [26] => Array ( [0] => 1.09095 [1] => 1.09104 ) [27] => Array ( [0] => 1.09095 [1] => 1.09103 ) [28] => Array ( [0] => 1.09093 [1] => 1.09102 ) [29] => Array ( [0] => 1.09082 [1] => 1.0909 ) [30] => Array ( [0] => 1.09082 [1] => 1.09092 ) [31] => Array ( [0] => 1.09083 [1] => 1.09092 ) [32] => Array ( [0] => 1.09082 [1] => 1.09091 ) [33] => Array ( [0] => 1.09081 [1] => 1.0909 ) [34] => Array ( [0] => 1.0908 [1] => 1.09089 ) [35] => Array ( [0] => 1.09081 [1] => 1.0909 ) [36] => Array ( [0] => 1.0908 [1] => 1.09089 ) [37] => Array ( [0] => 1.09081 [1] => 1.09089 ) [38] => Array ( [0] => 1.0908 [1] => 1.09089 ) [39] => Array ( [0] => 1.0908 [1] => 1.09088 ) [40] => Array ( [0] => 1.09078 [1] => 1.09087 ) [41] => Array ( [0] => 1.09079 [1] => 1.09088 ) [42] => Array ( [0] => 1.09079 [1] => 1.09089 ) [43] => Array ( [0] => 1.09075 [1] => 1.09084 ) [44] => Array ( [0] => 1.09074 [1] => 1.09083 ) [45] => Array ( [0] => 1.09074 [1] => 1.09084 ) [46] => Array ( [0] => 1.09074 [1] => 1.09083 ) [47] => Array ( [0] => 1.09074 [1] => 1.09084 ) [48] => Array ( [0] => 1.09074 [1] => 1.09083 ) [49] => Array ( [0] => 1.09073 [1] => 1.09083 ) [50] => Array ( [0] => 1.09074 [1] => 1.09083 ) [51] => Array ( [0] => 1.09074 [1] => 1.09084 ) [52] => Array ( [0] => 1.09074 [1] => 1.09083 ) [53] => Array ( [0] => 1.09073 [1] => 1.09083 ) [54] => Array ( [0] => 1.09073 [1] => 1.09082 ) [55] => Array ( [0] => 1.09073 [1] => 1.09083 ) [56] => Array ( [0] => 1.09074 [1] => 1.09083 ) [57] => Array ( [0] => 1.09073 [1] => 1.09082 ) [58] => Array ( [0] => 1.09073 [1] => 1.09083 ) [59] => Array ( [0] => 1.09073 [1] => 1.09082 ) [60] => Array ( [0] => 1.09072 [1] => 1.09081 ) [61] => Array ( [0] => 1.09071 [1] => 1.09081 ) [62] => Array ( [0] => 1.09072 [1] => 1.09081 ) [63] => Array ( [0] => 1.09072 [1] => 1.09083 ) [64] => Array ( [0] => 1.09073 [1] => 1.09083 ) ) )
Any Suggestions? Thanks in advance.

How to merge many array in PHP

I have array who showing result like this
Array
(
[0] => AED
)
Array
(
[0] => 1
)
Array
(
[0] => 4071.64
)
Array
(
[0] => 3603.98
)
Array
(
[0] => AUD
)
Array
(
[0] => 1
)
Array
(
[0] => 10151.11
)
Array
(
[0] => 9964.65
)
Array
(
[0] => CHF
)
Array
(
[0] => 1
)
Array
(
[0] => 14162.66
)
Array
(
[0] => 13912.70
)
Array
(
[0] => CNY
)
Array
(
[0] => 1
)
Array
(
[0] => 2177.73
)
Array
(
[0] => 2024.07
)
Array
(
[0] => EUR
)
Array
(
[0] => 1
)
Array
(
[0] => 16080.01
)
Array
(
[0] => 15813.89
)
Array
(
[0] => GBP
)
Array
(
[0] => 1
)
Array
(
[0] => 18476.74
)
Array
(
[0] => 18180.11
)
Array
(
[0] => HKD
)
Array
(
[0] => 1
)
Array
(
[0] => 1802.15
)
Array
(
[0] => 1778.28
)
Array
(
[0] => JPY
)
Array
(
[0] => 1
)
Array
(
[0] => 128.40
)
Array
(
[0] => 125.26
)
Array
(
[0] => NZD
)
Array
(
[0] => 1
)
Array
(
[0] => 9711.35
)
Array
(
[0] => 9529.10
)
Array
(
[0] => SAR
)
Array
(
[0] => 1
)
Array
(
[0] => 3765.13
)
Array
(
[0] => 3727.50
)
Array
(
[0] => SGD
)
Array
(
[0] => 1
)
Array
(
[0] => 10496.44
)
Array
(
[0] => 10308.02
)
Array
(
[0] => USD
)
Array
(
[0] => 1
)
Array
(
[0] => 14120.00
)
Array
(
[0] => 13980.00
)
I want to merge that array into one array. My question is how to merge that array the output must be like this
array(
[0]=>array(
[0]=>AED
[1]=>1
[2]=>4071.64
[3]=>3603.98
)
[1]=>array(
[0]=>AUD
[1]=>1
[2]=>10151.11
[3]=>9964.65
)
[3]=>array(
[0]=>CHF
[1]=>1
[2]=>14162.66
[3]=>13912.70
)
//and so on
)
OR the result like this
Array
(
[0] => AED
[1] => 1
[2] => 4071.64
[3] => 3603.98
[4] => AUD
[5] => 1
[6] => 10151.11
[7] => 9964.65
[8] => CHF
[9] => 1
[10] => 14162.66
[11] => 13912.70
[12] => CNY
[13] => 1
[14] => 2177.73
[15] => 2024.07
[16] => EUR
[17] => 1
[18] => 16080.01
[19] => 15813.89
[20] => GBP
[21] => 1
[22] => 18476.74
[23] => 18180.11
[24] => HKD
[25] => 1
[26] => 1802.15
[27] => 1778.28
[28] => JPY
[29] => 1
[30] => 128.40
[31] => 125.26
[32] => NZD
[33] => 1
[34] => 9711.35
[35] => 9529.10
[36] => SAR
[37] => 1
[38] => 3765.13
[39] => 3727.50
[40] => SGD
[41] => 1
[42] => 10496.44
[43] => 10308.02
[44] => USD
[45] => 1
[46] => 14120.00
[47] => 13980.00
)
Please help me, brothers and sisters, I have tried using array_merge() and array_push() but not showing the right result. Thanks
You can use array_chunk for that if it's always inner arrays of length == 4.
$text = "AED 1 4071.64 3603.98 AUD 1 10151.11 9964.65";
$result= array_chunk( explode( ' ', $text ), 4 );
var_dump( $result );
Here's a php sandbox link.
Try this code:
$array_data = array(
array(
AED, 1, 4071.64, 3603.98
),
array(
AUD, 1, 10151.11, 9964.65
),
array(
CHF, 1, 14162.66, 13912.70
),
);
$a = [];
foreach($array_data as $data) {
$a = array_merge($a, $data);
}

Convert associative array from the result of mysqli_fetch_all to simple indexed array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
I have code like this
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($years_of_service,MYSQLI_NUM);
When i try to echo that result to see what happened with that code, i found this kind of array
Array ( [0] => Array ( [0] => 25 ) [1] => Array ( [0] => 25 ) [2] => Array ( [0] => 25 ) [3] => Array ( [0] => 25 ) [4] => Array ( [0] => 25 ) [5] => Array ( [0] => 14 ) [6] => Array ( [0] => 14 ) [7] => Array ( [0] => 14 ) [8] => Array ( [0] => 14 ) [9] => Array ( [0] => 14 ) [10] => Array ( [0] => 12 ) [11] => Array ( [0] => 12 ) [12] => Array ( [0] => 12 ) [13] => Array ( [0] => 12 ) [14] => Array ( [0] => 12 ) [15] => Array ( [0] => 11 ) [16] => Array ( [0] => 11 ) [17] => Array ( [0] => 11 ) [18] => Array ( [0] => 11 ) [19] => Array ( [0] => 11 ) [20] => Array ( [0] => 10 ) [21] => Array ( [0] => 10 ) [22] => Array ( [0] => 10 ) [23] => Array ( [0] => 10 ) [24] => Array ( [0] => 10 ) [25] => Array ( [0] => 9 ) [26] => Array ( [0] => 9 ) [27] => Array ( [0] => 9 ) [28] => Array ( [0] => 9 ) [29] => Array ( [0] => 8 ) [30] => Array ( [0] => 8 ) [31] => Array ( [0] => 8 ) [32] => Array ( [0] => 8 ) [33] => Array ( [0] => 7 ) [34] => Array ( [0] => 7 ) [35] => Array ( [0] => 7 ) [36] => Array ( [0] => 7 ) [37] => Array ( [0] => 6 ) [38] => Array ( [0] => 6 ) [39] => Array ( [0] => 6 ) [40] => Array ( [0] => 6 ) [41] => Array ( [0] => 6 ) [42] => Array ( [0] => 6 ) [43] => Array ( [0] => 6 ) [44] => Array ( [0] => 6 ) [45] => Array ( [0] => 6 ) [46] => Array ( [0] => 5 ) [47] => Array ( [0] => 5 ) [48] => Array ( [0] => 5 ) [49] => Array ( [0] => 5 ) [50] => Array ( [0] => 5 ) [51] => Array ( [0] => 5 ) [52] => Array ( [0] => 5 ) [53] => Array ( [0] => 5 ) [54] => Array ( [0] => 5 ) [55] => Array ( [0] => 4 ) [56] => Array ( [0] => 4 ) [57] => Array ( [0] => 4 ) [58] => Array ( [0] => 4 ) [59] => Array ( [0] => 4 ) [60] => Array ( [0] => 4 ) [61] => Array ( [0] => 4 ) [62] => Array ( [0] => 4 ) [63] => Array ( [0] => 4 ) [64] => Array ( [0] => 4 ) [65] => Array ( [0] => 4 ) [66] => Array ( [0] => 4 ) [67] => Array ( [0] => 4 ) [68] => Array ( [0] => 4 ) [69] => Array ( [0] => 3 ) [70] => Array ( [0] => 3 ) [71] => Array ( [0] => 3 ) [72] => Array ( [0] => 3 ) [73] => Array ( [0] => 3 ) [74] => Array ( [0] => 3 ) [75] => Array ( [0] => 3 ) [76] => Array ( [0] => 3 ) [77] => Array ( [0] => 3 ) [78] => Array ( [0] => 2 ) [79] => Array ( [0] => 2 ) [80] => Array ( [0] => 2 ) [81] => Array ( [0] => 2 ) [82] => Array ( [0] => 1 ) [83] => Array ( [0] => 1 ) [84] => Array ( [0] => 1 ) [85] => Array ( [0] => 1 ) [86] => Array ( [0] => 0 ) [87] => Array ( [0] => 0 ) [88] => Array ( [0] => 0 ) [89] => Array ( [0] => 0 ) )
I want to make it become a simple array containing only its value like
Array(25,25,25,....)
What is the best approach i can use. I've tried to make it simple array with this
$simple = array_values($result);
But it did'nt work. It show the same output with $result. I will use it for map and reduce function to categorize years of service by specific range.
If PHP > 5.5.0 you can simply use array_column like as
$result = array_column($your_array,0);
or you can simply use call_user_func_array like as
call_user_func_array('array_merge',$your_array);
Demo
The reason this is happening is you're retrieving all rows (outer array) which are being returned as an array of columns (the inner arrays).
What you need to do is loop through the rows:
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($work_period,MYSQLI_NUM);
$data = [];
foreach ($result as $row) {
$data[] = $row[0];
}
Your array will now be 'flat':
$data => [
0 => 25,
1 => 24,
...
];
which will now return what you want when you use array_values:
$simple = array_values($data); // $simple will now equal Array(25, 24, ...)
You could try something like:
$simple = array();
foreach ($result as $value) {
$simple[] = $value[0];
}

php array_unique bug in 2d ARRAY

I'm working on Array list with have many duplication
I use array_unique
foreach ($aData as $test) {
preg_match( "~http://www.site.com/.*?/~", $test,$match) ;
$a[] = $match;
}
output
Array (
[0] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[1] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[2] => Array ( )
[3] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[4] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[5] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[6] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[7] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[8] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[9] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[10] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[11] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[12] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[13] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[14] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[15] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[16] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[17] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[18] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[19] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[20] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[21] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[22] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/ )
[23] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/ )
[24] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/)
[25] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[26] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[27] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[28] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
[29] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
[30] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
)
It's look like array_unique cant go deep in to array like : []**[x]**
so only thing i get after array_unique is my first [0][string]
I tried to use only $match but its only show me last string which is useless
So how could I resolve this BUG?
By default array_unique compares the items as if they are string, which means that it actually converts the arrays to string (and any array casted to string will be the same - the string 'Array'). So you have to do:
$a = array_unique($a, SORT_REGULAR)

php - matching elements in an array

I've been playing with this all day and haven't figured out a good way to do it...
I have two arrays and am trying to create an array based on matching values.
//$original
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[1] => DZ
[2] => ER
[3] => TY
)
[1] => Array
(
[0] => KV
[1] => EN
[2] => CR
)
[2] => Array
(
[0] => HU
[1] => GO
[2] => GA
[3] => FI
)
)
)
)
//$compare
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[1] => AN
[2] => ER
)
[1] => Array
(
[0] => KV
)
[2] => Array
(
[0] => HU
[1] => XV
[2] => ZL
[3] => FI
)
)
)
)
And I'm trying to produce
//$similar
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[2] => ER
)
[1] => Array
(
[0] => KV
)
[2] => Array
(
[0] => HU
[3] => FI
)
)
)
)
Use array_intersect.
$similar = $compare;
$similar[0]['items'] = array_intersect($compare[0]['items'], $original[0]['items']);
codepad example

Categories