Sort arr[] in PHP 50 indexes to 7 indexes - php
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>';
Related
PHP multi dimensional array search by conditions
I am developing a financial web application I am really stuck with this issue. I need to search with conditions and return the array if the condition is true.. I know the multi-dimensional array search, but I did't get any idea for this. For example : if array[0][7] > array[0][8] && array[0][12] < array[0][15] Please suggest me the solution Array ( [0] => Array ( [0] => 54452 [1] => 'KSB' [2] => 'INE999A01015' [3] => 'EQ' [4] => 'Ksb Limited' [5] => -0.70 [6] => -0.10 [7] => 662.90 [8] => 663.60 [9] => 669.35 [10] => 678.25 [11] => 651.55 [12] => 7874 [13] => 676.91 [14] => 690.93 [15] => 703.61 [16] => 664.23 [17] => 650.21 [18] => 637.53 [19] => 623.51 [20] => 2530 [21] => 32.13 [22] => 825.00 [23] => 539.00 [24] => 665.89 [25] => 631.37 [26] => 616.65 [27] => 610.14 [28] => 615.89 [29] => 656.25 [30] => 680.74 [31] => 60.71 [32] => 60.71 [33] => 13.62 [34] => 29.39 [35] => -38.97 [36] => 129.47 [37] => 2019-09-26 [38] => 27632 ), [1] => Array ( [0] => 53772 [1] => 'ASPINWALL' [2] => 'INE991I01015' [3] => 'EQ' [4] => 'Aspinwall And Company Limited' [5] => -10.50 [6] => -7.74 [7] => 127.45 [8] => 137.95 [9] => 135.60 [10] => 144.50 [11] => 112.60 [12] => 9583 [13] => 143.76 [14] => 160.08 [15] => 175.66 [16] => 128.18 [17] => 111.86 [18] => 96.28 [19] => 79.96 [20] => 5108 [21] => 53.30 [22] => 214.80 [23] => 112.60 [24] => 135.17 [25] => 137.13 [26] => 137.46 [27] => 136.86 [28] => 137.59 [29] => 143.00 [30] => 153.89 [31] => 39.41 [32] => 39.41 [33] => -0.90 [34] => 37.96 [35] => -58.05 [36] => -361.91 [37] => 2019-09-26 [38] => 26324 ) ) Thanks in advance
Give a try to following code and make necessary changes according to your need function search_sg_dma($array, $pre_close, $dma_value, $close_price){ ... $result = array(); for( $i = 0; $i < count( $array ); $i++ ) { if ( $array[$i][$pre_close] < $array[$i][$dma_value] && $array[$i][$close_price] > $array[$i][$dma_value] ) { $result[] = array[$i]; } } return $result; }
Last element of array
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
Cut array in php
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 );
removing duplicate values in multi dimensional array
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
PHP grabbing an array from another array
I have an array like so Array ( [0] => Array ( [64579] => Array ( [title] => title [description] => blah, blah, blah [image] => Array ( [original] => Array ( [src] => image1.jpg ) [reference] => Array ( [src] => image1_big.jpg ) [thumb] => Array ( [src] => image1_thumb.jpg ) ) What I am trying to grab is the src of reference image...I have tried the following foreach($images as $row => $value){ $gallery[] = $value['image']; } but it returns like so 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] => ) What do I have to do to add the reference image src to my new gallery array?
What you have is a three dimensional array... you need to wrap this in another loop.. foreach($images as $imageGroupList) foreach($imageGroupList as $row => $value){ $gallery[] = $value['image']; } } of if your top level array only ever has a single element you can just iterate of it foreach($images[0] as $row => $value){ $gallery[] = $value['image']; }