Convert repeating single dimensional array to multidimensional array in PHP - php

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
)
*/

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

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

How to delete the last element of an array if it is empty

Below is the print_r version of my array ($hamle). As you can see index(80) is empty or there is a end of line or a similar character there. I tried many options to delete it if it exists and is empty but couldn't do it. I strongly think that it is reading an empty line during the 'read while' loop. It doesn't appear in all files. It appears sometimes. Can anyone think of a chic way out?
Array ( [0] => 1.b4 [1] => d5 [2] => 2.Bb2 [3] => Qd6 [4] => 3.a3 [5] => e5 [6] => 4.e3 [7] => a5 [8] => 5.b5 [9] => Nf6 [10] => 6.c4 [11] => Bg4 [12] => 7.Be2 [13] => Bxe2 [14] => 8.Qxe2 [15] => Nbd7 [16] => 9.d4 [17] => dxc4 [18] => 10.Nf3 [19] => e4 [20] => 11.Ne5 [21] => Nb6 [22] => 12.Nd2 [23] => Qe6 [24] => 13.O-O [25] => Bd6 [26] => 14.Nexc4 [27] => Nxc4 [28] => 15.Nxc4 [29] => O-O [30] => 16.Rfc1 [31] => Be7 [32] => 17.a4 [33] => Nd5 [34] => 18.Ba3 [35] => Bb4 [36] => 19.Qb2 [37] => Rfc8 [38] => 20.Bxb4 [39] => axb4 [40] => 21.Nd2 [41] => c6 [42] => 22.b6 [43] => Qe7 [44] => 23.Rc5 [45] => Ra6 [46] => 24.a5 [47] => g6 [48] => 25.Nb3 [49] => Rca8 [50] => 26.Rac1 [51] => Kg7 [52] => 27.Qd2 [53] => Qd6 [54] => 28.R1c4 [55] => Rd8 [56] => 29.Rxb4 [57] => Nxb4 [58] => 30.Qxb4 [59] => Qe7 [60] => 31.h3 [61] => Rd5 [62] => 32.Kf1 [63] => Ra8 [64] => 33.Qa4 [65] => Qd6 [66] => 34.Ke2 [67] => Kf8 [68] => 35.Rxd5 [69] => cxd5 [70] => 36.Nc5 [71] => Ke7 [72] => 37.Qb5 [73] => Rb8 [74] => 38.Kd2 [75] => f5 [76] => 39.Kc3 [77] => g5 [78] => 40.Kb4 [79] => 1-0 [80] => )
The code =>
while(! feof($file))
{
$n=$n+1;
$hamle1= fgets($file);
$hamle1 = str_replace("\n", "", $hamle1);
$hamle1 = str_replace("\r", "", $hamle1);
$hamle1 = trim($hamle1);
$hamle1 = explode(" ", $hamle1);
foreach($hamle1 as $item)
{
$hamle[] = $item;
}
array_filter will remove empty entries
$hamle = array_filter($hamle);
To delete the last element of array if it's empty then use below code.
// get the last element of array
if(trim(end($hamle)) == '')
{
// Remove last element from array
array_pop($hamle) ;
}
Kindly visit below links to understand the functions which are used in above code.
trim
end
array_pop

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

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

Categories