PHP multi dimensional array search by conditions - php

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;
}

Related

Sort arr[] in PHP 50 indexes to 7 indexes

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>';

Glob patterns for file matching in PHP - Get directory contents which do not start with a string group

How can I ignore all images that begin with the string 'blogCover'?
At first I did not exclude a picture for the test
all pictures in img directory:
$imgDir = 'img';
$imgs = glob($imgDir.'/*{.jpg}', GLOB_BRACE); //all images!
echo'<pre>'; print_r($imgs); echo'</pre>';
result:
Array
(
[0] => img/blogCover-large.jpg
[1] => img/blogCover-large_2x.jpg
[2] => img/blogCover-medium.jpg
[3] => img/blogCover-medium_2x.jpg
[4] => img/blogCover-small.jpg
[5] => img/blogCover-small_2x.jpg
[6] => img/boddy-large.jpg
[7] => img/boddy-large_2x.jpg
[8] => img/boddy-medium.jpg
[9] => img/boddy-medium_2x.jpg
[10] => img/boddy-small.jpg
[11] => img/boddy-small_2x.jpg
[12] => img/face-large.jpg
[13] => img/face-large_2x.jpg
[14] => img/face-medium.jpg
[15] => img/face-medium_2x.jpg
[16] => img/face-small.jpg
[17] => img/face-small_2x.jpg
[18] => img/hand-large.jpg
[19] => img/hand-large_2x.jpg
[20] => img/hand-medium.jpg
[21] => img/hand-medium_2x.jpg
[22] => img/hand-small.jpg
[23] => img/hand-small_2x.jpg
[24] => img/head-large.jpg
[25] => img/head-large_2x.jpg
[26] => img/head-medium.jpg
[27] => img/head-medium_2x.jpg
[28] => img/head-small.jpg
[29] => img/head-small_2x.jpg
)
my goal: Ignore all images that begin with the string 'blogCover'
$imgDir = 'img';
$imgs = glob($imgDir.'/*{[!blogCover*].jpg}', GLOB_BRACE);
echo'<pre>'; print_r($imgs); echo'</pre>';
unexpected result:
Array
(
[0] => img/blogCover-large_2x.jpg
[1] => img/blogCover-medium.jpg
[2] => img/blogCover-medium_2x.jpg
[3] => img/blogCover-small_2x.jpg
[4] => img/boddy-large_2x.jpg
[5] => img/boddy-medium.jpg
[6] => img/boddy-medium_2x.jpg
[7] => img/boddy-small_2x.jpg
[8] => img/face-large_2x.jpg
[9] => img/face-medium.jpg
[10] => img/face-medium_2x.jpg
[11] => img/face-small_2x.jpg
[12] => img/hand-large_2x.jpg
[13] => img/hand-medium.jpg
[14] => img/hand-medium_2x.jpg
[15] => img/hand-small_2x.jpg
[16] => img/head-large_2x.jpg
[17] => img/head-medium.jpg
[18] => img/head-medium_2x.jpg
[19] => img/head-small_2x.jpg
)
all '* -large.jpg', '* -small.jpg' images are missing
expected result:
Array
(
[0] => img/boddy-large.jpg
[1] => img/boddy-large_2x.jpg
[2] => img/boddy-medium.jpg
[3] => img/boddy-medium_2x.jpg
[4] => img/boddy-small.jpg
[5] => img/boddy-small_2x.jpg
[6] => img/face-large.jpg
[7] => img/face-large_2x.jpg
[8] => img/face-medium.jpg
[9] => img/face-medium_2x.jpg
[10] => img/face-small.jpg
[11] => img/face-small_2x.jpg
[12] => img/hand-large.jpg
[13] => img/hand-large_2x.jpg
[14] => img/hand-medium.jpg
[15] => img/hand-medium_2x.jpg
[16] => img/hand-small.jpg
[17] => img/hand-small_2x.jpg
[18] => img/head-large.jpg
[19] => img/head-large_2x.jpg
[20] => img/head-medium.jpg
[21] => img/head-medium_2x.jpg
[22] => img/head-small.jpg
[23] => img/head-small_2x.jpg
)
I expect to ignore these pictures:
blogCover-large.jpg
blogCover-large_2x.jpg
blogCover-medium.jpg
blogCover-medium_2x.jpg
blogCover-small.jpg
blogCover-small_2x.jpg
The right syntax is:
$imgs = glob($imgDir.'/[^blogCover]*.jpg', GLOB_BRACE);

Sort Images by Number in PHP

I am trying to get my image slides to display in order by number with the following:
$image=array();
$img_folder = $params->get('path');
mt_srand((double)microtime()*1000);
if(is_dir($img_folder)){
krsort($file);
$imgs = dir($img_folder);
while ($file = $imgs->read()) {
if ((eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)))
$image[] = "$file";
} closedir($imgs->handle);
print_r($image);
return $image;
}
However, the array works but the order is off:
Array ( [0] => Slide1.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [11] => Slide2.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [22] => Slide3.png [23] => Slide30.png [24] => Slide31.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png )
If I put natsort($image); after $image[] = "$file"; the order is correct but the array is off:
Array ( [0] => Slide1.png [11] => Slide2.png [22] => Slide3.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [23] => Slide30.png [24] => Slide31.png )
I need them to order Slide1 - Slide30, not like listed above. I could change the names from numeric (Slide1) to alphabetic (SlideA) but would like to keep this naming convention. Is there a way to do this without changing the image names?
This image slide is a "how to" slide show.
NatSort is what you are looking for.
just use natsort($image);
You can reference it at:
http://php.net/manual/es/function.natsort.php
I didn't know about natsort(), this should do it too though..
$imgs = scandir(dir($img_folder));
$sorted = array();
foreach($imgs as $img){
$index = intval(trim($img, "Slide.png"));
$sorted[$index] = $img;
}
ksort($sorted);
$sorted = array_values($sorted);
You can use natsort() function
natsort
e.g.
$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array);
print_r($array);
This way you will get out put is :
Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )
You can also use natcasesort($image). It sort an array using a case insensitive "natural order" algorithm.
This did the trick!!!
Just after
$image[] = "$file";
I added
sort($image, SORT_NATURAL | SORT_FLAG_CASE);
This correctly orders the images in the array and the order:
Array ( [0] => Slide1.png [1] => Slide2.png [2] => Slide3.png [3] => Slide4.png [4] => Slide5.png [5] => Slide6.png [6] => Slide7.png [7] => Slide8.png [8] => Slide9.png [9] => Slide10.png [10] => Slide11.png [11] => Slide12.png [12] => Slide13.png [13] => Slide14.png [14] => Slide15.png [15] => Slide16.png [16] => Slide17.png [17] => Slide18.png [18] => Slide19.png [19] => Slide20.png [20] => Slide21.png [21] => Slide22.png [22] => Slide23.png [23] => Slide24.png [24] => Slide25.png [25] => Slide26.png [26] => Slide27.png [27] => Slide28.png [28] => Slide29.png [29] => Slide30.png [30] => Slide31.png )

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 );

How to get the specific piece of information based on string start point and end point using PHP

I have below array:
[0] => href="/favicon.ico"
[1] => href="/w/opensearch_desc.php"
[2] => href="http://www.xxxxx.org/w/api.php?action=rsd"
[3] => href="http://www.gnu.org/copyleft/fdl.html"
[4] => href="/w/index.php?title=Special:RecentChanges&feed=atom"
[5] => href="/w/skins/common/commonPrint.css"
[6] => href="#navigation"
[7] => href="#p-wikisearch"
[8] => href="/wiki/Bookshelf_How-To"
[9] => href="/wiki/Category:DE_B%C3%BCcherregal"
[10] => href="/wiki/Category:FR_Genre"
[11] => href="/wiki/Category:IT_Biblioteca"
[12] => href="/wiki/Category:PT_Prateleira"
[13] => href="/wiki/Category:Agriculture_Bookshelf"
[14] => href="/wiki/Category:Animal"
[15] => href="/wiki/Category:Children%27s_Bookshelf"
[16] => href="/wiki/Category:Classics_Bookshelf"
[17] => href="/wiki/Category:Countries_Bookshelf"
[18] => href="/wiki/Category:Crime_Bookshelf"
[19] => href="/wiki/Category:Education_Bookshelf"
[20] => href="/wiki/Category:Fine_arts_Bookshelf"
[21] => href="/wiki/Category:General_Works_Bookshelf"
[22] => href="/wiki/Category:Geography_Bookshelf"
[23] => href="/wiki/Category:Health_Bookshelf"
[24] => href="/wiki/Category:History_Bookshelf"
[25] => href="/wiki/Category:Language_and_Literature_Bookshelf"
[26] => href="/wiki/Category:Law_Bookshelf"
[27] => href="/wiki/Category:Library_Science"
[28] => href="/wiki/Category:Music_Bookshelf"
[29] => href="/wiki/Category:Periodicals_Bookshelf"
[30] => href="/wiki/Category:Political_Science_Bookshelf"
[31] => href="/wiki/Category:Psychology_and_Philosophy_Bookshelf"
[32] => href="/wiki/Category:Religion_Bookshelf"
[33] => href="/wiki/Category:Science_Bookshelf"
[34] => href="/wiki/Category:Social_Sciences_Bookshelf"
[35] => href="/wiki/Category:Technology_Bookshelf"
[36] => href="/wiki/Category:Wars_Bookshelf"
[37] => href="/w/index.php?title=Category:Bookshelf&pagefrom=The+Galaxy+%28Bookshelf%29#mw-pages"
[38] => href="/wiki/Adventure_(Bookshelf)"
[39] => href="/wiki/Africa_(Bookshelf)"
[40] => href="/wiki/African_American_Writers_(Bookshelf)"
Now, simply I want this array as a result:
[13] => Agriculture_Bookshelf
[14] => Animal
[15] => Children%27s_Bookshelf
[16] => Classics_Bookshelf
[17] => Countries_Bookshelf
[18] => Crime_Bookshelf
[19] => Education_Bookshelf
[20] => Fine_arts_Bookshelf
[21] => General_Works_Bookshelf
[22] => Geography_Bookshelf
[23] => Health_Bookshelf
[24] => History_Bookshelf
[25] => Language_and_Literature_Bookshelf
[26] => Law_Bookshelf
[27] => Library_Science
[28] => Music_Bookshelf
[29] => Periodicals_Bookshelf
[30] => Political_Science_Bookshelf
[31] => Psychology_and_Philosophy_Bookshelf
[32] => Religion_Bookshelf
[33] => Science_Bookshelf
[34] => Social_Sciences_Bookshelf
[35] => Technology_Bookshelf
[36] => Wars_Bookshelf
I am using this code for getting the above results but still no lucks.
$match[0] contains the above array.
$str = '_Bookshelf';
if(in_array($str,$match[0])){
echo "FOUND!!";
foreach($match[0] as $asd=>$asds){
echo "<pre>";echo str_replace('href="/wiki/Category:','',$asds);echo "</pre>";
}
}
Please can anybody rectify my code for the desired results.
Use a regular expression to test the URLs for the pattern.
// Find the first _Bookshelf element
foreach ($match[0] as $key => $value) {
if (preg_match('#/wiki/Category:(.*_Bookshelf)"$#', $value)) {
$first_index = $key;
break;
}
}
// Find the last _Bookshelf element by searching the reversed array
foreach (array_reverse($match[0]) as $key => $value) {
if (preg_match('#/wiki/Category:(.*_Bookshelf)"$#', $value)) {
$last_index = $key;
break;
}
}
// Get the selected part of the array
$result = array_slice($match[0], $first_index, -$last_index);
// Remove the URL prefix and quote at the end
foreach ($result as &$val) {
$val = preg_replace('#.*/wiki/Category:(.*)"$#', '$1', $val);
}

Categories