I have a multi dim. array with duplicate values. I need it to show only one single value of each in a dropdown, which in turn are used for a search Query. The indexs are not important here.
I tried with array_map, serialize, array_unique and so on, but can't get it right. Some values allways comes out twice.
I'm on php version 4.3.2, so can't use functions made after that version.
The array looks like this:
Array
(
[1] => Array
(
[1] => 0,9
[2] => 1,3
[3] => 1,6
[4] => 1,5
[5] => 0,0
[6] => 1,2
)
[2] => Array
(
[3] => 1,0
[4] => 2,0
[5] => 1,78
[6] => 1,44
[7] => 1,87
[8] => 1,56
[9] => 2,4
[10] => 2,93
[11] => 2,50
[12] => 1,4
[13] => 6,0
[14] => 4,0
[15] => 1,80
[16] => 3,00
)
[3] => Array
(
[5] => 1,0
[6] => 1,2
[7] => 1,5
[8] => 1,6
)
[4] => Array
(
[7] => 1,0
)
[5] => Array
(
[1] => 1,0
[2] => 1,5
[3] => 2,0
[4] => 2,5
[5] => 3,0
[6] => 3,5
[7] => 4,0
[8] => 4,5
)
[6] => Array
(
[1] => 1,5
[2] => 2,0
[3] => 2,5
[4] => 3,0
[5] => 3,5
[6] => 4,0
[7] => 4,5
[8] => 5,0
[9] => 5,5
[10] => 6,0
[11] => 6,5
[12] => 7,0
[13] => 7,5
[14] => 8,0
[15] => 8,5
[16] => 9,0
[17] => 9,5
[18] => 10,0
)
[7] => Array
(
[15] => 0,65
[16] => 0,75
[17] => 0,85
[18] => 0,90
[19] => 1,00
[20] => 1,10
[21] => 1,15
[22] => 1,25
[23] => 1,50
[24] => 1,55
[25] => 1,60
[26] => 1,80
[27] => 1,85
[28] => 2,00
[29] => 2,10
)
[8] => Array
(
[30] => 0,90
[31] => 1,00
)
[10] => Array
(
[34] => 0,65
[35] => 0,75
[36] => 0,85
[37] => 0,90
[38] => 1,00
[39] => 1,10
[40] => 1,15
[41] => 1,25
[42] => 1,50
[43] => 1,55
[44] => 1,60
[45] => 1,80
[46] => 1,85
[47] => 2,00
[48] => 2,10
)
[11] => Array
(
[34] => 1,0
[35] => 1,5
[36] => 2,0
[37] => 2,5
[38] => 3,0
[39] => 3,5
[40] => 4,0
[41] => 4,5
)
[12] => Array
(
[43] => 1,00
)
[13] => Array
(
[45] => 2,00
[46] => 2,20
[47] => 2,50
[48] => 2,75
[49] => 3,00
[50] => 3,25
[51] => 3,50
[52] => 2,60
[53] => 2,85
[54] => 1,50
[55] => 2,10
[56] => 2,35
[57] => 2,80
[58] => 2,40
[59] => 2,70
[60] => 2,25
[61] => 3,40
[62] => 2,55
[63] => 2,30
[64] => 2,65
)
)
Any help would be appreciated.
To make each sub-array unique compared to other sub-arrays (the order is important):
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
To make only values within each sub-array unique:
$array = array_map('array_unique', $array);
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
is exactly what I tried, and from that same thread, but it comes out like this:
Array(
[1] => Array
(
[1] => 0,9
[2] => 1,3
[3] => 1,6
[4] => 1,5
[5] => 0,0
[6] => 1,2
)
[2] => Array
(
[3] => 1,0
[4] => 2,0
[5] => 1,78
[6] => 1,44
[7] => 1,87
[8] => 1,56
[9] => 2,4
[10] => 2,93
[11] => 2,50
[12] => 1,4
[13] => 6,0
[14] => 4,0
[15] => 1,80
[16] => 3,00
)
[3] => Array
(
[5] => 1,0
[6] => 1,2
[7] => 1,5
[8] => 1,6
)
[4] => Array
(
[7] => 1,0
)
[5] => Array
(
[1] => 1,0
[2] => 1,5
[3] => 2,0
[4] => 2,5
[5] => 3,0
[6] => 3,5
[7] => 4,0
[8] => 4,5
)
[6] => Array
(
[1] => 1,5
[2] => 2,0
[3] => 2,5
[4] => 3,0
[5] => 3,5
[6] => 4,0
[7] => 4,5
[8] => 5,0
[9] => 5,5
[10] => 6,0
[11] => 6,5
[12] => 7,0
[13] => 7,5
[14] => 8,0
[15] => 8,5
[16] => 9,0
[17] => 9,5
[18] => 10,0
)
[7] => Array
(
[15] => 0,65
[16] => 0,75
[17] => 0,85
[18] => 0,90
[19] => 1,00
[20] => 1,10
[21] => 1,15
[22] => 1,25
[23] => 1,50
[24] => 1,55
[25] => 1,60
[26] => 1,80
[27] => 1,85
[28] => 2,00
[29] => 2,10
)
[8] => Array
(
[30] => 0,90
[31] => 1,00
)
[10] => Array
(
[34] => 0,65
[35] => 0,75
[36] => 0,85
[37] => 0,90
[38] => 1,00
[39] => 1,10
[40] => 1,15
[41] => 1,25
[42] => 1,50
[43] => 1,55
[44] => 1,60
[45] => 1,80
[46] => 1,85
[47] => 2,00
[48] => 2,10
)
[11] => Array
(
[34] => 1,0
[35] => 1,5
[36] => 2,0
[37] => 2,5
[38] => 3,0
[39] => 3,5
[40] => 4,0
[41] => 4,5
)
[12] => Array
(
[43] => 1,00
)
[13] => Array
(
[45] => 2,00
[46] => 2,20
[47] => 2,50
[48] => 2,75
[49] => 3,00
[50] => 3,25
[51] => 3,50
[52] => 2,60
[53] => 2,85
[54] => 1,50
[55] => 2,10
[56] => 2,35
[57] => 2,80
[58] => 2,40
[59] => 2,70
[60] => 2,25
[61] => 3,40
[62] => 2,55
[63] => 2,30
[64] => 2,65
)
)
And I still got multiple values like 1,0
Related
Here is an array that I want to sort. This is taken by an HTML table that is already stored in the database. But here it has 50 indexes. That's the thing I want to reduce to seven.
(
[0] => Array
(
[0] =>
[1] => data
[2] => data
[3] => data
[4] => data
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[2] => Array
(
[0] => 1
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[3] => Array
(
[0] => 2
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[4] => Array
(
[0] => 3
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[5] => Array
(
[0] => 4
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[6] => Array
(
[0] => 5
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[7] => Array
(
[0] => 6
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[8] => Array
(
[0] => 7
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[9] => Array
(
[0] => 8
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[10] => Array
(
[0] => 9
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
)
I tried with for loops, foreach loops to sort this array shows up to 7 indexes. It should be like this.
(
[0] => Array
(
[0] =>
[1] => data
[2] => data
[3] => data
[4] => data
[5] =>
[6] =>
[7] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[2] => Array
(
[0] => 1
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[3] => Array
(
[0] => 2
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[4] => Array
(
[0] => 3
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[5] => Array
(
[0] => 4
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[6] => Array
(
[0] => 5
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[7] => Array
(
[0] => 6
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[8] => Array
(
[0] => 7
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[9] => Array
(
[0] => 8
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[10] => Array
(
[0] => 9
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
)
What did I try is,
for ($row = 0; $row <= sizeof($arr); $row++) {
for ($col = 0; $col < 7; $col++) {
$aar[] = $arr[$row][$col];
}
}
This method wasn't worked. What is the way or any suggestions on this?
If you just want to keep x amount of elements from each sub-array, you can use array_slice():
$newArray = [];
foreach($arr as $sub) {
// The example array you say you want have 8 elements, indexes 0-7
// so start with index 0 and take 8
$newArray[] = array_slice($sub, 0, 8);
}
Here's a demo: https://3v4l.org/WhFm4
Here is the one possible solution to your problem. Just define the key limit in if condition. Hope it will solve your problem.
$row = [
[
'',
'data',
'data',
'data',
'',
'',
'',
''
],
[
'',
'data',
'data',
'data',
'',
'',
'',
''
]
];
$reducedArray = [];
foreach ($row as $col) {
$tempArray = [];
foreach ($col as $innnerKey => $innerValue) {
// Define your index limit here.
// As you can see I have set the limit to 7
if ($innnerKey <= 7) {
$tempArray[] = $innerValue;
}
}
$reducedArray[] = $tempArray;
}
echo '<pre>';
print_r($row);
echo '</pre>';
echo '<br/>';
echo '<pre>';
print_r($reducedArray);
echo '</pre>';
With the following code I am able to generate 2 arrays..
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
//ignores first line of csv
fgetcsv($file_handle);
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'csv1.csv';
$csvFile2 = 'csv2.csv';
$csv = readCSV($csvFile);
$csv2 = readCSV($csvFile2);
echo '<pre>';
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
if(!array_key_exists($value[24],$arr)){
$arr[$value[24]] = [];
}
$arr[$value[24]] = array_merge($arr[$value[24]],$value);
}
//ignores last item in array
array_pop($arr);
$arr2 = [];
//$csv2 is your array
foreach($csv2 as $key => $value){
if(!array_key_exists($value[1],$arr2)){
$arr2[$value[1]] = [];
}
$arr2[$value[1]] = array_merge($arr2[$value[1]],$value);
}
echo '<pre>';
print_r($arr);
print_r($arr2);
echo '</pre>';
This outputs the arrays as follows..
Array 1 ($arr)
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
)
)
And array 2 ($arr2)
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => 1593608448
[2] =>
[3] => John Smith
[4] => John
[5] => Smith
[6] => 1
[7] => Credit Card
[8] => 03/25/20
[9] => Any Street
[10] =>
[11] => Somewhere
[12] => Someplace
[13] => PC 0DE
[14] => United Kingdom
[15] => GBP
[16] => 2.85
[17] =>
[18] =>
[19] => 0.00
[20] => 0.00
[21] => 2.49
[22] => 0
[23] => 5.88
[24] =>
[25] => 0.44
[26] => 4.9
[27] =>
[28] =>
[29] =>
[30] => John Smith
[31] => online
[32] => online_cc
[33] =>
[34] =>
)
)
I have attempted to merge these arrays with..
$merged = array_merge($arr,$arr2);
print_r($merged);
This outputs as..
Array
(
[0] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
)
[1] => Array
(
[0] => 03/25/20
[1] => 1593608448
[2] =>
[3] => John Smith
[4] => John
[5] => Smith
[6] => 1
[7] => Credit Card
[8] => 03/25/20
[9] => Any Street
[10] =>
[11] => Somewhere
[12] => Someplace
[13] => PC 0DE
[14] => United Kingdom
[15] => GBP
[16] => 2.85
[17] =>
[18] =>
[19] => 0.00
[20] => 0.00
[21] => 2.49
[22] => 0
[23] => 5.88
[24] =>
[25] => 0.44
[26] => 4.9
[27] =>
[28] =>
[29] =>
[30] => John Smith
[31] => online
[32] => online_cc
[33] =>
[34] =>
)
)
When I wanted/expected..
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
[32] => 03/25/20
[33] => 1593608448
[34] =>
[35] => John Smith
[36] => John
[37] => Smith
[38] => 1
[39] => Credit Card
[40] => 03/25/20
[41] => Any Street
[42] =>
[43] => Somewhere
[44] => Someplace
[45] => PC 0DE
[46] => United Kingdom
[47] => GBP
[48] => 2.85
[49] =>
[50] =>
[51] => 0.00
[52] => 0.00
[53] => 2.49
[54] => 0
[55] => 5.88
[56] =>
[57] => 0.44
[58] => 4.9
[59] =>
[60] =>
[61] =>
[62] => John Smith
[63] => online
[64] => online_cc
[65] =>
[66] =>
)
)
So keeping the order id (which is always [24] in 1 arr1 and [2] in the arr2) as the key then add on to the end of the matching key in $arr1 the contents of the matching key from $arr2 with the numbers just following on.
I have looked at many similar questions and answers on here but not getting the results I require.
Or another way of putting it is it will check if [24] and [2] match and then append on to the end
It would be better to merge the data as you read it. I was going to change your read so that it read the CSV as it created the arrays, but I have left the readCSV() as it is - except to add array_filter() to remove empty elements.
Rather than create the two arrays and then merge them, I've change the second CSV loop to check $arr and add the data at this point...
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
//ignores first line of csv
fgetcsv($file_handle);
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
$line_of_text = array_filter($line_of_text);
return $line_of_text;
}
$csvFile = 'csv1.csv';
$csvFile2 = 'csv2.csv';
$csv = readCSV($csvFile);
$csv2 = readCSV($csvFile2);
echo '<pre>';
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
if(!array_key_exists($value[24],$arr)){
$arr[$value[24]] = [];
}
$arr[$value[24]] = array_merge($arr[$value[24]],$value);
}
foreach($csv2 as $key => $value){
if(!array_key_exists($value[1],$arr)){
$arr[$value[1]] = [];
}
$arr[$value[1]] = array_merge($arr[$value[1]],$value);
}
echo '<pre>';
print_r($arr);
echo '</pre>';
Use array_merge_recursive instead of array_merge
$merged = array_merge_recursive($arr,$arr2);
Im trying to go through array to create a new one from it. Trying to log everything while runnging this code:
$this->writeToLog(print_r($this->assembledText, true), 'ass.log');
foreach ($this->assembledText as $paragraphsKey => $paragraphs) {
$this->writeToLog("Paragraph Key:".$paragraphsKey.print_r($paragraphs, true), 'para.log');
$i = 0;
foreach ($paragraphs as $words) {
$newText[$paragraphsKey][$i] = $words;
$i++;
}
}
Here Im logging the text before i go through, and here what i have so far:
ass.log gives me this:
[2019-05-18 20:32:38] Array
(
[0] => Array
(
[0] => One
[1] => thing
[2] => was
[3] => certain,
[4] => that
[5] => the
[6] => white
[7] => kitten
[8] => had
[9] => had
[10] => nothing
[11] => to
[12] => do
[13] => with
[14] => it:
[15] => —
[16] => it
[17] => was
[18] => the
[19] => black
[20] => kitten’s
[21] => fault
[22] => entirely.
[23] => For
[24] => the
[25] => white
[26] => kitten
[27] => had
[28] => been
[29] => having
[30] => its
[31] => face
[32] => washed
[33] => by
[34] => the
[35] => old
[36] => cat
[37] => for
[38] => the
[39] => last
[40] => quarter
[41] => of
[42] => an
[43] => hour
[44] => (and
[45] => bearing
[46] => it
[47] => pretty
[48] => well,
[49] => considering);
[50] => so
[51] => you
[52] => see
[53] => that
[54] => it
[55] => couldn’t
[56] => have
[57] => had
[58] => any
[59] => hand in
[61] => the
[62] => mischief.
[63] =>
)
[1] => Array
(
[0] =>
[1] => The
[2] => way
[3] => Dinah
[4] => washed
[5] => her
[6] => children’s
[7] => faces
[8] => was
[9] => this:
[10] => first
[11] => she
[12] => held
[13] => the
[14] => poor
[15] => thing
[16] => down
[17] => by
[18] => its
[19] => ear
[20] => with
[21] => one
[22] => paw,
[23] => and
[24] => then
[25] => with
[26] => the
[27] => other
[28] => paw
[29] => she
[30] => rubbed
[31] => its
[32] => face
[33] => all
[34] => over,
[35] => the
[36] => wrong
[37] => way,
[38] => beginning
[39] => at
[40] => the
[41] => nose:
[42] => and
[43] => just
[44] => now,
[45] => as
[46] => I
[47] => said,
[48] => she
[49] => was
[50] => hard
[51] => at
[52] => work on
[54] => the
[55] => white
[56] => kitten,
[57] => which
[58] => was
[59] => lying
[60] => quite
[61] => still
[62] => and
[63] => trying
[64] => to
[65] => purr
[66] => —
[67] => no
[68] => doubt
[69] => feeling
[70] => that
[71] => it
[72] => was
[73] => all
[74] => meant
[75] => for
[76] => its
[77] => good.
[78] =>
)
[2] => Array
(
[0] =>
[1] => But
[2] => the
[3] => black
[4] => kitten
[5] => had
[6] => been
[7] => finished
[8] => with
[9] => earlier
[10] => in
[11] => the
[12] => afternoon,
[13] => and
[14] => so,
[15] => while
[16] => Alice
[17] => was
[18] => sitting
[19] => curled
[20] => up
[21] => in
[22] => a
[23] => corner
[24] => of
[25] => the
[26] => great
[27] => arm-chair,
[28] => half
[29] => talking
[30] => to
[31] => herself
[32] => and
[33] => half
[34] => asleep,
[35] => the
[36] => kitten
[37] => had
[38] => been
[39] => having
[40] => a
[41] => grand
[42] => game
[43] => of
[44] => romps
[45] => with
[46] => the
[47] => ball
[48] => of
[49] => worsted
[50] => Alice
[51] => had
[52] => been
[53] => trying
[54] => to
[55] => wind up,
[57] => and
[58] => had
[59] => been
[60] => rolling
[61] => it
[62] => up
[63] => and
[64] => down
[65] => till
[66] => it
[67] => had
[68] => all
[69] => come
[70] => undone
[71] => again;
[72] => and
[73] => there
[74] => it
[75] => was,
[76] => spread
[77] => over
[78] => the
[79] => hearth-rug,
[80] => all
[81] => knots
[82] => and
[83] => tangles,
[84] => with
[85] => the
[86] => kitten
[87] => running
[88] => after
[89] => its
[90] => own
[91] => tail
[93] => the
[94] => middle.
[95] =>
)
[3] => Array
(
[0] =>
[1] => ‘Oh,
[2] => you
[3] => wicked
[4] => little
[5] => thing!’
[6] => cried
[7] => Alice,
[8] => catching up
[10] => the
[11] => kitten,
[12] => and
[13] => giving
[14] => it
[15] => a
[16] => little
[17] => kiss
[18] => to
[19] => make
[20] => it
[21] => understand
[22] => that
[23] => it
[24] => was
[25] => in
[26] => disgrace.
[27] =>
)
)
And para.log gives me this, like i dont have the last element of array, but i do have an index:
[2019-05-18 20:32:38] Paragraph Key:0Array
(
[0] => One
[1] => thing
[2] => was
[3] => certain,
[4] => that
[5] => the
[6] => white
[7] => kitten
[8] => had
[9] => had
[10] => nothing
[11] => to
[12] => do
[13] => with
[14] => it:
[15] => —
[16] => it
[17] => was
[18] => the
[19] => black
[20] => kitten’s
[21] => fault
[22] => entirely.
[23] => For
[24] => the
[25] => white
[26] => kitten
[27] => had
[28] => been
[29] => having
[30] => its
[31] => face
[32] => washed
[33] => by
[34] => the
[35] => old
[36] => cat
[37] => for
[38] => the
[39] => last
[40] => quarter
[41] => of
[42] => an
[43] => hour
[44] => (and
[45] => bearing
[46] => it
[47] => pretty
[48] => well,
[49] => considering);
[50] => so
[51] => you
[52] => see
[53] => that
[54] => it
[55] => couldn’t
[56] => have
[57] => had
[58] => any
[59] => hand in
[61] => the
[62] => mischief.
[63] =>
)
[2019-05-18 20:32:38] Paragraph Key:1Array
(
[0] =>
[1] => The
[2] => way
[3] => Dinah
[4] => washed
[5] => her
[6] => children’s
[7] => faces
[8] => was
[9] => this:
[10] => first
[11] => she
[12] => held
[13] => the
[14] => poor
[15] => thing
[16] => down
[17] => by
[18] => its
[19] => ear
[20] => with
[21] => one
[22] => paw,
[23] => and
[24] => then
[25] => with
[26] => the
[27] => other
[28] => paw
[29] => she
[30] => rubbed
[31] => its
[32] => face
[33] => all
[34] => over,
[35] => the
[36] => wrong
[37] => way,
[38] => beginning
[39] => at
[40] => the
[41] => nose:
[42] => and
[43] => just
[44] => now,
[45] => as
[46] => I
[47] => said,
[48] => she
[49] => was
[50] => hard
[51] => at
[52] => work on
[54] => the
[55] => white
[56] => kitten,
[57] => which
[58] => was
[59] => lying
[60] => quite
[61] => still
[62] => and
[63] => trying
[64] => to
[65] => purr
[66] => —
[67] => no
[68] => doubt
[69] => feeling
[70] => that
[71] => it
[72] => was
[73] => all
[74] => meant
[75] => for
[76] => its
[77] => good.
[78] =>
)
[2019-05-18 20:32:38] Paragraph Key:2Array
(
[0] =>
[1] => But
[2] => the
[3] => black
[4] => kitten
[5] => had
[6] => been
[7] => finished
[8] => with
[9] => earlier
[10] => in
[11] => the
[12] => afternoon,
[13] => and
[14] => so,
[15] => while
[16] => Alice
[17] => was
[18] => sitting
[19] => curled
[20] => up
[21] => in
[22] => a
[23] => corner
[24] => of
[25] => the
[26] => great
[27] => arm-chair,
[28] => half
[29] => talking
[30] => to
[31] => herself
[32] => and
[33] => half
[34] => asleep,
[35] => the
[36] => kitten
[37] => had
[38] => been
[39] => having
[40] => a
[41] => grand
[42] => game
[43] => of
[44] => romps
[45] => with
[46] => the
[47] => ball
[48] => of
[49] => worsted
[50] => Alice
[51] => had
[52] => been
[53] => trying
[54] => to
[55] => wind up,
[57] => and
[58] => had
[59] => been
[60] => rolling
[61] => it
[62] => up
[63] => and
[64] => down
[65] => till
[66] => it
[67] => had
[68] => all
[69] => come
[70] => undone
[71] => again;
[72] => and
[73] => there
[74] => it
[75] => was,
[76] => spread
[77] => over
[78] => the
[79] => hearth-rug,
[80] => all
[81] => knots
[82] => and
[83] => tangles,
[84] => with
[85] => the
[86] => kitten
[87] => running
[88] => after
[89] => its
[90] => own
[91] => tail
[93] => the
[94] => middle.
[95] =>
)
[2019-05-18 20:32:38] Paragraph Key:3
Any suggestions?
So, i found the solution in the php Bug report: https://bugs.php.net/bug.php?id=60534
Which means that i have to use & (reference) even if i don't need it, just to avoid this bug
PHP version 5.6.33
I want to cut array in php. My array is listed below :
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
[30] => 7/1/2014
[31] => 7/2/2014
[32] => 7/3/2014
[33] => 7/4/2014
[34] => 7/5/2014
[35] => 7/6/2014
[36] => 7/7/2014
[37] => 7/8/2014
[38] => 7/9/2014
[39] => 7/10/2014
[40] => 7/11/2014
[41] => 7/12/2014
[42] => 7/13/2014
[43] => 7/14/2014
[44] => 7/15/2014
[45] => 7/16/2014
[46] => 7/17/2014
[47] => 7/18/2014
[48] => 7/19/2014
[49] => 7/20/2014
[50] => 7/21/2014
[51] => 7/22/2014
[52] => 7/23/2014
[53] => 7/24/2014
[54] => 7/25/2014
[55] => 7/26/2014
[56] => 7/27/2014
[57] => 7/28/2014
[58] => 7/29/2014
[59] => 7/30/2014
[60] => 7/31/2014
[61] => 8/1/2014
)
In this array 0 to 29 elements if for 6th Month, 30th to 60th elements are for 7th Month etc..
Now i want this array in the below fashion
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
)
Array
(
[0] => 7/1/2014
[1] => 7/2/2014
[2] => 7/3/2014
[3] => 7/4/2014
[4] => 7/5/2014
[5] => 7/6/2014
[6] => 7/7/2014
[7] => 7/8/2014
[8] => 7/9/2014
[9] => 7/10/2014
[10] => 7/11/2014
[11] => 7/12/2014
[12] => 7/13/2014
[13] => 7/14/2014
[14] => 7/15/2014
[15] => 7/16/2014
[16] => 7/17/2014
[17] => 7/18/2014
[18] => 7/19/2014
[19] => 7/20/2014
[20] => 7/21/2014
[21] => 7/22/2014
[22] => 7/23/2014
[23] => 7/24/2014
[24] => 7/25/2014
[25] => 7/26/2014
[26] => 7/27/2014
[27] => 7/28/2014
[28] => 7/29/2014
[29] => 7/30/2014
[30] => 7/31/2014
)
Array
(
[0] => 8/1/2014
)
This calculation should be in a way that if i chose other months then it also do the same process of cutting array for different months.
you can also separate array by month & and put them in main array
$arrres =array();
foreach($arr as $value)
{
$arrres[str_replace('/','',substr($value,0,2))][] = $value ;
}
print_r($arrres);
you can use this:, it will also generate arrays with same num of days in months (28,30,31)
//generate dates for test
$dates = [];
for($i = 0 ; $i < 90 ; $i++){
$dates[] = Date("d/m/Y",time()-($i*24*3600));
}
//make results, also validates that it's same month and year
$result = [];
foreach($dates as $date){
$dStr = Date("m-y",strtotime(str_replace('/', '-', $date)));
if(!isset($result[$dStr])) $result[$dStr] = [];
$result[$dStr][] = $date;
}
var_dump($result);
I'd explode the value of every index in your array and set it as an index to the new array.
$days = [];
foreach($your_array as $day) {
$days[ explode("/", $day)[0] ][] = $day;
}
var_dump( $days );
I have a single dimentional PHP Array that has latitude, longitude, and time data. The data goes eg [lat, long, date, lat, long, date, lat, long, date.... etc etc]
Array ( [0] => -28.0447606 [1] => 153.4340961 [2] => 1424136836118 [3] => -28.0447612 [4] => 153.4340963 [5] => 1424136876189 [6] => -28.0447658 [7] => 153.4340962 [8] => 1424136897993 [9] => -28.0447619 [10] => 153.4340615 [11] => 1424136918045 [12] => -28.0447656 [13] => 153.434057 [14] => 1424136938057 [15] => -28.0447613 [16] => 153.4340484 [17] => 1424136958085 [18] => -28.0447791 [19] => 153.4340959 [20] => 1424136978117 [21] => -28.0447584 [22] => 153.4340501 [23] => 1424136998135 [24] => -28.0447676 [25] => 153.434047 [26] => 1424137018179 [27] => -28.044782 [28] => 153.4340982 [29] => 1424137038185 [30] => -28.0447599 [31] => 153.4340496 [32] => 1424137058214 [33] => -28.0447614 [34] => 153.4340531 [35] => 1424137078589 [36] => -28.0447588 [37] => 153.4340963 [38] => 1424137098731 [39] => -28.0447768 [40] => 153.434098 [41] => 1424137138640 [42] => -28.0447141 [43] => 153.4341097 [44] => 1424137158672 [45] => -28.0447628 [46] => 153.4340962 [47] => 1424137178698 [48] => -28.0447622 [49] => 153.4340962 [50] => 1424137198726 [51] => -28.0447528 [52] => 153.4340936 [53] => 1424137218871 [54] => -28.0447636 [55] => 153.4340472 [56] => 1424137258825 [57] => -28.0447608 [58] => 153.434097 [59] => 1424137279945 [60] => -28.0447656 [61] => 153.4340979 [62] => 1424137300018 )
I am just wondering if there is an easy way to convert this into a two dimensional array so I can access them like $gps[0][1] or $gps[3][0] etc. I've tried a few ways like using a for loop, but surely there's some other way I'm overlooking.
If I'm not wrong this is what you want.
You just need to change the $arr for your variable.
$coords = array_map(function($coords) {
return array(
"lat" => $coords[0], # latitude
"long" => $coords[1], # longitude
"ts" => $coords[2] # timestamp
);
}, array_chunk($arr, 3));
/*
Array (
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
... and so on
)
*/