Last element of array - php

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

Related

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

split big array into multiple arrays where empty element occurs in php

This is Input:-print_r($result4);
Output:-Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III [5] => [6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above [11] => [12] => A)-Cholestasis [13] => B)-Cholecystitis [14] => C)-Cholelithiasis [15] => D)-Hepatic-encephalopathy [16] => E)-Ascites [17] => [18] => A-Acetyl-salicylic-acid [19] => B-Ibuprofen [20] => C-Acetaminophen [21] => D-Pepto-Bismol [22] => E-All-of-the-above [23] => [24] => [25] => A-dark-urine [26] => B-stomach-pain [27] => C-blood-in-stools [28] => D-yellowing-of-skin [29] => E-Yellowing-of-eye-and-mucus [30] => [31] => A-Hepatitis-A [32] => B-Hepatitis-B [33] => C-Dukoral [34] => D-Gerdasil [35] => E-None-of-the-above [36] => [37] => cervical-cancer-caused-by-papilloma-virus [38] => A-Glucuronidation [39] => B-Glutathione-conjugation [40] => C-Acetylation [41] => D-Sulfate-conjugation [42] => E-Methylation [43] => [44] => A-Acetylcysteine [45] => B-cysteine [46] => C-Mercapturic-acid [47] => D-Glutathione-conjugation [48] => E-Glutathione-only [49] => [50] => administered-in-teenagers-to-prevent-cervical-cancer. [51] => A-Hepatitis-A [52] => B-Hepatitis-C [53] => C-Hepatitis-D [54] => D-Hepatitis-A,-B-&-C [55] => E-Hepatitis-A,-B-&-D [56] => [57] => [58] => A)-ALT [59] => B)-AST [60] => C)-Bilirubin- [61] => D)-Albumin [62] => E)-Proteins [63] => [64] => A--Saliva- [65] => B-Bile- [66] => C-Pancreatic-duct---- [67] => D-Gastric-secretions [68] => [69] => A)-Sexual-contact [70] => B)-Blood-transfusion [71] => C-Food-and-drink-contamination [72] => D-Traveling-abroad [73] => E-Drugs [74] => [75] => A)-Hepatitis-A-only [76] => B)-Hepatitis-B-only [77] => C)-Hepatitis-A-and-B [78] => D)-Hepatitis-A,-B-and-C [79] => E)-Hepatitis-B-and-C [80] => [81] => [82] => A)-Proton-pump-inhibitors [83] => B)-Warfarin [84] => C)-antacids [85] => D)Lipid-soluble-drugs [86] => E)-Parenteral-drugs [87] => [88] => A)-spironolactone- [89] => B)-NSAIDs- [90] => C)-acetaminophen- [91] => D)-Ibuprofen- [92] => E)-codeine [93] => [94] => A)-constipation- [95] => B)-ascites- [96] => C)-encephalitis- [97] => D)-liver-cirrhosis- [98] => E)-Hepatitis [99] => [100] => encephalitis. [101] => contaminated-hepatitis? [102] => A)-Hepatitis-A [103] => B)-Hepatitis-B [104] => C)-Hepatitis-A-and-B [105] => D)-Hepatitis-C [106] => E)-Hepatitis-D [107] => [108] => A)-Hepatitis-A [109] => [110] => B)-Hepatitis-A-and-B [111] => C)-Hepatitis-B-and-C [112] => D)-Hepatitis-A,-B,-C [113] => [114] => [115] => )
now i want to split array into multiple arrays where empty element occurs like
Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III ) Array([6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above )....similarly
Below will work,
$new_array = array();
$i = 0;
foreach($array as $k => $v)
{
if (!empty($v))
{
$new_array[$i][$k] = $v;
continue;
}
$i++;
}
//This will fix your missing fourth index<br/>
$new_array = array_values($new_array);
var_dump($new_array);
DEMO.

Can't run exec()/shell_exec() on 1&1 server

EDIT
I'm trying to run a php file in background without success using one of those function.
This is not the file that will be execute in backgorund (the real one will send emails), I use this method to find out which command parameter is the correct one
File permission 0644; Folder permission: 0755
tets.php:
<html>
<head>
</head>
<body>
<pre>
<?php
$ex='php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
exec($ex,$return);
echo print_r($return,true);
?>
</pre>
</body>
</html>
run.php(works from url):
<html>
<head>
</head>
<body>
<pre>
<?php
$fp = fopen('/homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/data.txt', 'w+');
fwrite($fp, '1');
fclose($fp);
echo 'printed';
?>
</pre>
</body>
</html>
these are my tries:
$ex='php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
$ex='php -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
$ex='/usr/lib/php6 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
Fisrt->
Array
[89] => [79] => [69] => [59] => [49] => [39] => [29] => [19] => [9] => (
[90] => [80] => [70] => [60] => [50] => [40] => [30] => [20] => [10] => [0] => X-Powered-By: PHP/5.2.17
[91] => [81] => [71] => [61] => [51] => [41] => [31] => [21] => [11] => [1] => Content-type: text/html
[92] => [82] => [72] => [62] => [52] => [42] => [32] => [22] => [12] => [2] =>
[93] => [83] => [73] => [63] => [53] => [43] => [33] => [23] => [13] => [3] =>
[94] => [84] => [74] => [64] => [54] => [44] => [34] => [24] => [14] => [4] =>
[95] => [85] => [75] => [65] => [55] => [45] => [35] => [25] => [15] => [5] =>
[96] => [86] => [76] => [66] => [56] => [46] => [36] => [26] => [16] => [6] =>
[97] => [87] => [77] => [67] => [57] => [47] => [37] => [27] => [17] => [7] =>
[98] => [88] => [78] => [68] => [58] => [48] => [38] => [28] => [18] => [8] =>
[99] => [89] => [79] => [69] => [59] => [49] => [39] => [29] => [19] => [9] => Warning: exec() [function.exec]: Unable to fork [php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php] in /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/test.php on line 8
[100] => [90] => [80] => [70] => [60] => [50] => [40] => [30] => [20] => [10] => Array
[101] => [91] => [81] => [71] => [61] => [51] => [41] => [31] => [21] => [11] => (
[102] => [92] => [82] => [72] => [62] => [52] => [42] => [32] => [22] => [12] => )
[103] => [93] => [83] => [73] => [63] => [53] => [43] => [33] => [23] => [13] =>
[104] => [94] => [84] => [74] => [64] => [54] => [44] => [34] => [24] => [14] =>
[105] => [95] => [85] => [75] => [65] => [55] => [45] => [35] => [25] => [15] =>
[106] => [96] => [86] => [76] => [66] => [56] => [46] => [36] => [26] => )
the second the same array without the warning and the third is an empty array
It's probably disabled on your host, but php itself isn't, so why don't you just include the file?
If your php is going to take a long time to run you can do the following:
ignore_user_abort(true);
set_time_limit(0);
your_long_function();
This will mean your script keeps running indefinitely (you can leave the page), which is potentially a dangerous thing to do. Be careful not to make any infinite loops.
ignore_user_abort
set_time_limit
See this answer for some other options too.

Extract data from a page

Hello I would like to create a page html and php that is able to take the data in the table contained at this link: http://www.comuni-italiani.it/province.html
I would love to have any tips, I would use the file_get_content but then I do not know how to take all the various data
Can you explain us more clearly what you want to exactly take from this page?
Anyway, to do the trick, you can use file_get_contents to fetch the page then, according to what you want to take from the page (I suppose you want to take every <td> element from the page inside a table), you may use PHP regular expressions (preg_match, preg_match_all) to fetch all the data you need.
Example for your case:
$page = file_get_contents("http://www.comuni-italiani.it/province.html");
$output = array();
preg_match_all('/<td.*.<\/td>/',$page,$output);
print_r($output);
This will output:
Array ( [0] => Array ( [0] => [1] => [2] => Agrigento [3] => Alessandria [4] => Ancona [5] => Aosta [6] => Arezzo [7] => Ascoli Piceno [8] => Asti [9] => Avellino [10] => Bari [11] => Barletta-Andria-Trani [12] => Belluno [13] => Benevento [14] => Bergamo [15] => Biella [16] => Bologna [17] => Bolzano [18] => Brescia [19] => Brindisi [20] => Cagliari [21] => Caltanissetta [22] => Campobasso [23] => Carbonia-Iglesias [24] => Caserta [25] => Catania [26] => Catanzaro [27] => Chieti [28] => Como [29] => Cosenza [30] => Cremona [31] => Crotone [32] => Cuneo [33] => Enna [34] => Fermo [35] => Ferrara [36] => Firenze [37] => Foggia [38] => Forlì-Cesena [39] => Frosinone [40] => Genova [41] => Gorizia [42] => Grosseto [43] => Imperia [44] => Isernia [45] => La Spezia [46] => L'Aquila [47] => Latina [48] => Lecce [49] => Lecco [50] => Livorno [51] => Lodi [52] => Lucca [53] => Macerata [54] => Mantova [55] => Massa-Carrara [56] => Matera [57] => Messina [58] => Milano [59] => Modena [60] => Monza e della Brianza [61] => Napoli [62] => Novara [63] => Nuoro [64] => Olbia-Tempio [65] => Oristano [66] => Padova [67] => Palermo [68] => Parma [69] => Pavia [70] => Perugia [71] => Pesaro e Urbino [72] => Pescara [73] => Piacenza [74] => Pisa [75] => Pistoia [76] => Pordenone [77] => Potenza [78] => Prato [79] => Ragusa [80] => Ravenna [81] => Reggio Calabria [82] => Reggio Emilia [83] => Rieti [84] => Rimini [85] => Roma [86] => Rovigo [87] => Salerno [88] => Medio Campidano [89] => Sassari [90] => Savona [91] => Siena [92] => Siracusa [93] => Sondrio [94] => Taranto [95] => Teramo [96] => Terni [97] => Torino [98] => Ogliastra [99] => Trapani [100] => Trento [101] => Treviso [102] => Trieste [103] => Udine [104] => Varese [105] => Venezia [106] => Verbano-Cusio-Ossola [107] => Vercelli [108] => Verona [109] => Vibo Valentia [110] => Vicenza [111] => Viterbo [112] => CercaNel Sito e sul WebPagine UtiliElenco Province per PopolazionePrincipali Città ItalianeLista Alfabetica RegioniAmministrazioni LocaliScuole in Italia [113] => ) )
which can, of course, be filtered.
In your case, for example, by adding a little foreach loop... :
$page = file_get_contents("http://www.comuni-italiani.it/province.html");
$output = array();
preg_match_all('/<td.*.<\/td>/',$page,$output);
$provinces = array();
foreach ($output as $id => $list) {
for ($i = 2; $i <= 111; $i++) {
array_push($provinces,$list[$i]);
}
}
print_r($provinces);
Will give you this:
Array ( [0] => Agrigento [1] => Alessandria [2] => Ancona [3] => Aosta [4] => Arezzo [5] => Ascoli Piceno [6] => Asti [7] => Avellino [8] => Bari [9] => Barletta-Andria-Trani [10] => Belluno [11] => Benevento [12] => Bergamo [13] => Biella [14] => Bologna [15] => Bolzano [16] => Brescia [17] => Brindisi [18] => Cagliari [19] => Caltanissetta [20] => Campobasso [21] => Carbonia-Iglesias [22] => Caserta [23] => Catania [24] => Catanzaro [25] => Chieti [26] => Como [27] => Cosenza [28] => Cremona [29] => Crotone [30] => Cuneo [31] => Enna [32] => Fermo [33] => Ferrara [34] => Firenze [35] => Foggia [36] => Forlì-Cesena [37] => Frosinone [38] => Genova [39] => Gorizia [40] => Grosseto [41] => Imperia [42] => Isernia [43] => La Spezia [44] => L'Aquila [45] => Latina [46] => Lecce [47] => Lecco [48] => Livorno [49] => Lodi [50] => Lucca [51] => Macerata [52] => Mantova [53] => Massa-Carrara [54] => Matera [55] => Messina [56] => Milano [57] => Modena [58] => Monza e della Brianza [59] => Napoli [60] => Novara [61] => Nuoro [62] => Olbia-Tempio [63] => Oristano [64] => Padova [65] => Palermo [66] => Parma [67] => Pavia [68] => Perugia [69] => Pesaro e Urbino [70] => Pescara [71] => Piacenza [72] => Pisa [73] => Pistoia [74] => Pordenone [75] => Potenza [76] => Prato [77] => Ragusa [78] => Ravenna [79] => Reggio Calabria [80] => Reggio Emilia [81] => Rieti [82] => Rimini [83] => Roma [84] => Rovigo [85] => Salerno [86] => Medio Campidano [87] => Sassari [88] => Savona [89] => Siena [90] => Siracusa [91] => Sondrio [92] => Taranto [93] => Teramo [94] => Terni [95] => Torino [96] => Ogliastra [97] => Trapani [98] => Trento [99] => Treviso [100] => Trieste [101] => Udine [102] => Varese [103] => Venezia [104] => Verbano-Cusio-Ossola [105] => Vercelli [106] => Verona [107] => Vibo Valentia [108] => Vicenza [109] => Viterbo )
(Sorry for the huge arrays).
It is, however, keeping the links inside the array so, if you want to take the values only and NOT the anchor associated to it, just feel free to use another regular expression.
Hope this helps.
(take this as an example, keep in mind that this foreach trick may not work anymore if the page gets changed, I posted it just to give you an idea on how you may have solved that case).
Try to look more into DOMDocument reference : http://php.net/manual/en/class.domdocument.php
Also Those questions might be helpful for you:
Getting DOM elements by classname
PHP Parse HTML code

Only show content of 1 array by it's name

If have and array like this:
Array (
[Example1] => Array
(
[0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] =>
)
[Example2] => 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] => [51] => [52] => [53] => [54] =>
[55] => [56] => [57] => [58] => [59] =>
[60] => [61] => [62] => [63] => [64] =>
[65] => [66] => [67] => [68] => [69] =>
[70] => [71] => [72] => [73] => [74] =>
[75] => [76] => [77] => [78] => [79] =>
[80] => [81] => [82] => [83] => [84] =>
[85] => [86] => [87] => [88] => [89] =>
[90] => [91] => [92] => [93] => [94] =>
[95] => [96] => [97] => [98] => [99] =>
[100] => [101] => [102] => [103] => [104] =>
[105] => [106] => [107] => [108] => [109] =>
[110] => [111] => [112] => [113] => [114] =>
[115] => [116] => [117] => [118] => [119] =>
[120] => [121] => [122] => [123] => [124] =>
[125] => [126] => [127] => [128] => [129] =>
[130] => [131] => [132] => [133] => [134] =>
)
)
There's is 2 primary arrays (Example1 & Example2) and in those another array is made. I would like to know how I can only call only 1 array like "Example2" so it print only that one and ignore "Example1"
Please note that there could be more than 2 primary arrays.
And what if with that array I want to build a drop down menu, here what I have:
$__selectGroups = '';
foreach ($groups as $key => $options)
{
sort($options);
if ($key !== '')
{
$__selectGroups .= '<optgroup label="'.$key.'">';
}
$__selectGroups .= implode("\n", $options);
if ($key !== '')
{
$__selectGroups .= '</optgroup>';
}
}
How can I tell to build the drop down with only Example2 and ignore the others?
I'm not sure I understand the question... seems too simple:
print_r($yourArray['Example1']);
Since you edited your question, I think you want to do this?
foreach ($yourArray['Example1'] as $key => $value) {
}
This paradigm for accessing nested arrays applies across the board...
You need something like this:
echo '<pre>';
print_r($yourArray['Example1']);
print_r($yourArray['Example2']);
echo '<pre>';
Or you need something more elaborated?

Categories