Hy,我设计了一个阵列,我需要一些帮助,因为我有一些令人头痛的问题。任何人都可以简单地告诉我如何以一种简单的方式使用它吗?
当我尝试在数组中添加一些值时,大多数错误都是数组偏移错误。
我的阵列:
$info = array
(
"id" => "",
"city" => "",
"university" => array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
)
);
CODE:
<?php
// HTML DOM PARSER LIBRARY
include('hdp.php');
//error_reporting(false);
/*
* Returns an array with field associative name and
* extracted value, using a specified delimiter.
*/
function extractField($str, $delim)
{
$val = '';
// All possible fields
$posFlds = array
(
'Adresa' => 'address',
'Telefon' => 'phone',
'Fax' => 'fax',
'Email' => 'email',
'Website' => 'website',
'Tip' => 'type'
);
foreach($posFlds as $fldName => $assocName)
{
if(strstr($str,$fldName))
{
$val = @explode($delim,$str);
if (!$val[1])
{
print 'Delimiter `'.$delim.'` not found!';
return false;
}
return array($assocName, $val[1]);
}
}
return false;
}
// DB->Table query structure
$info = array
(
"id" => "",
"city" => "",
"university" => array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
)
);
// City identifier
$cityid = 0;
// Rows, columns and deep identifiers
$i = 0; $j = 0; $k = 0;
// Base, secondary and third crawl url
$baseUrl = 'XXX';
$secondaryUrl = ''; $thirdUrl ='';
// Html contents without stripped tags (as is)
$htmlCu = file_get_html($baseUrl);
$htmltUn = ''; $htmltFa = '';
// Selectors for third level deep check
$selectCu = $htmlCu->find('td[width="100%"] table td');
$selectUn = ''; $selectFa ='';
// Text formats (raw and cleaned)
$rtext =''; $ftext = '';
// Contoare
$a=0; $b=0; $c=0;
// Select each row from parent td -> child table -> child td
foreach($selectCu as $row)
{
// Skip first row result
if($i != 0)
{
$rtext = $row->plaintext;
$ftext = trim($rtext,' • ');
if($ftext != '-')
{
// If string contains a dot it is a city
if(strstr($rtext, '•'))
{
print "CITY = ".$ftext."<br>";
$cityid++;
$info[$a]["city"] = $ftext;
}
// If string is not a city, then is a university
else
{
$info[$a]["city"][$b]['university']['name'] = $ftext;
echo "<b>----ID [".$i."]; NAME:</b> ".$ftext."<br>";
$secondaryUrl = $row->find('a',0)->href;
$htmlUn = file_get_html($secondaryUrl);
$selectUn = $htmlUn->find('td[width="100%"] table tr');
foreach($selectUn as $col)
{
// Skip first row result
if($j != 0)
{
$rtext = $col->plaintext;
$ftext = trim($rtext,' • ');
echo "--------ID[".$j."] NAME: <b>".$ftext."</b>; TYPE: ".gettype($col)."<br>";
// key 0 -> associative name; key 1 -> value
$field = extractField($ftext,': ');
if($field)
{
echo "--------<b>Field name:</b> ".$field[0]."<b>, value:</b> ".$field[1]."<br>";
}
/////////////////////////////TESTTTTTT ZONEEEEEEEEEEEEEEE/////////////////////////////
// If string contains a dot it is a faculty
if(strstr($rtext, '•'))
{
$thirdUrl = $col->find('a',0)->href;
$htmlFa = file_get_html($thirdUrl);
$selectFa = $htmlFa->find('td[width="100%"] table tr');
foreach($selectFa as $deep)
{
$rtext = $deep->plaintext;
$ftext = trim($rtext,' • ');
//echo "------------id[".$k."] <-> <b>".$ftext."</b> <-> ".gettype($deep)."<br>";
$field = extractField($ftext,': ');
if($field)
{
echo "------------<b>Field name:</b> ".$field[0]."<b>, value:</b> ".$field[1]."<br>";
}
$k++;
}
echo "<br><br>";
}
}
$j++;
}
echo "<br><br>";
}
}
}
$i++;
if($cityid == 2) break;
}
print "<h1>".($i-1)."</h1>";
print_r($info);
答案 0 :(得分:1)
您在这些调用中以错误的方式访问数组:
$info[$a]["city"] = $ftext;
$info[$a]["city"][$b]['university']['name'] = $ftext;
正确的是:
$info["city"] = $ftext;
$info["city"]['university']['name'] = $ftext;
您似乎想要处理多个$info
- 数组。为此,您必须创建这些信息数组的数组。
答案 1 :(得分:1)
最终和工作代码,如果有人发现这个有用:
// HTML DOM PARSER LIBRARY
include('hdp.php');
error_reporting(true);
// Session to know what last city was
session_start();
if (!$_SESSION['LAST']) $_SESSION['LAST'] = NULL;
/*
* Returns an array with field associative name and
* extracted value, using a specified delimiter.
*/
function extractField($str, $delim)
{
$val = '';
// All possible fields
$posFlds = Array
(
'Adresa' => 'address',
'Telefon' => 'phone',
'Fax' => 'fax',
'Email' => 'email',
'Website' => 'website',
'Tip' => 'type'
);
foreach($posFlds as $fldName => $assocName)
{
if(strstr($str,$fldName))
{
$val = @explode($delim,$str);
if (!$val[1])
{
print 'Delimiter `'.$delim.'` not found!';
return false;
}
return array($assocName, $val[1]);
}
}
return false;
}
// Trying to connect to local server
$conn = mysqli_connect("localhost","root","","educatie") or die("Could not connect to MySQL server!");
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// DB->Table Query structure
$info = Array
(
"city" => Array
(
"name" => "",
"university" => Array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => Array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
),
)
);
// City identifier
$cityid = 0;
// Rows, columns and deep identifiers
$i = 0; $j = 0; $k = 0;
// Base, secondary and third crawl url
$baseUrl = 'http://infoportal.rtv.net/informatii_universitati.html';
$secondaryUrl = ''; $thirdUrl ='';
// Html contents without stripped tags (as is)
$htmlCu = file_get_html($baseUrl);
$htmltUn = ''; $htmltFa = '';
// Selectors for third level deep check
$selectCu = $htmlCu->find('td[width="100%"] table td');
$selectUn = ''; $selectFa ='';
// Text formats (raw and cleaned)
$rtext =''; $ftext = '';
// Ignore tokens
$banned = array(" ","•","""," ");
// Counters A, B and C
$a=0; $b=0; $c=0;
// Temporary variables
$tmpVar = '';
// Select each row from parent td -> child table -> child td
foreach($selectCu as $row)
{
// Skip first row result
if($i != 0)
{
$rtext = $row->plaintext;
$ftext = trim($rtext,' • ');
if($ftext != '-')
{
// If string contains a dot it is a city
if(strstr($rtext, '•'))
{
if(($_SESSION['LAST'] !== $ftext) && ($_SESSION['LAST'] !== NULL)) continue;
print "<h1>-> Oras: ".$ftext."</h1><br>";
echo "SESSION:".$_SESSION['LAST']."|<br />";
$tmpVar = $ftext;
}
// If string is not a city, then is a university
else
{
$a--;
$info[$a]['city']['name'] = $tmpVar;
$info[$a]['city']['university'][$b]['name'] = $ftext;
$secondaryUrl = $row->find('a',0)->href;
$htmlUn = file_get_html($secondaryUrl);
$selectUn = $htmlUn->find('td[width="100%"] table tr');
foreach($selectUn as $col)
{
// Skip first row result
if($j != 0)
{
$rtext = $col->plaintext;
$ftext = trim($rtext,' • ');
// key 0 -> associative name; key 1 -> value
$field = extractField($ftext,': ');
if($field)
{
if(strstr($field[1],'de stat'))
$info[$a]['city']['university'][$b][$field[0]] = 'de stat';
else
$info[$a]['city']['university'][$b][$field[0]] = $field[1];
}
/////////////////////////////TESTTTTTT ZONEEEEEEEEEEEEEEE/////////////////////////////
// If string contains a dot it is a faculty
if(strstr($rtext, '•'))
{
$tempVar = trim(str_replace($banned,'',$ftext),' ');
$thirdUrl = $col->find('a',0)->href;
$htmlFa = file_get_html($thirdUrl);
$selectFa = $htmlFa->find('td[width="100%"] table tr');
foreach($selectFa as $deep)
{
$rtext = $deep->plaintext;
$ftext = trim($rtext,' • ');
$info[$a]['city']['university'][$b]['faculty'][$c]['name'] = $tempVar;
$field = extractField($ftext,': ');
if($field)
{
if($field[0]=='website')
{
$info[$a]['city']['university'][$b]['faculty'][$c][$field[0]] = $deep->find('a',0)->href;
}
else
{
$info[$a]['city']['university'][$b]['faculty'][$c][$field[0]] = $field[1];
}
}
$k++;
}
$facName = @$info[$a]['city']['university'][$b]['faculty'][$c]['name'];
$facAddr = @$info[$a]['city']['university'][$b]['faculty'][$c]['address'];
$facPhone = @$info[$a]['city']['university'][$b]['faculty'][$c]['phone'];
$facFax = @$info[$a]['city']['university'][$b]['faculty'][$c]['fax'];
$facEmail = @$info[$a]['city']['university'][$b]['faculty'][$c]['email'];
$facWeb = @$info[$a]['city']['university'][$b]['faculty'][$c]['website'];
$facTax = @$info[$a]['city']['university'][$b]['faculty'][$c]['type'];
echo "<b>ID UNIVERSITATE:</b> ".$a."<br />";
echo "<b>Nume facultate:</b> ".$facName."<br />";
echo "<b>Adresa:</b> ".$facAddr."<br />";
echo "<b>Telefon:</b> ".$facPhone."<br />";
echo "<b>Fax:</b> ".$facFax."<br />";
echo "<b>Email:</b> ".$facEmail."<br />";
echo "<b>Pagina web:</b> ".$facWeb."<br />";
echo "<b>Tip taxa:</b> ".$facTax."<br /><br />";
mysqli_query($conn,"
INSERT INTO faculty
VALUES ('$a','$facName','$facAddr','$facPhone','$facFax','$facEmail','$facWeb','$facTax')
");
$c++;
}
}
$j++;
}
$univCity = @$info[$a]['city']['name'];
$univName = @$info[$a]['city']['university'][$b]['name'];
$univAddr = @$info[$a]['city']['university'][$b]['address'];
$univPhone = @$info[$a]['city']['university'][$b]['phone'];
$univFax = @$info[$a]['city']['university'][$b]['fax'];
$univEmail = @$info[$a]['city']['university'][$b]['email'];
$univWeb = @$info[$a]['city']['university'][$b]['website'];
$univTax = @$info[$a]['city']['university'][$b]['type'];
echo "<b>ID UNIVERSITATE:</b> ".$a."<br />";
echo "<b>Oras:</b> ".$univCity."<br />";
echo "<b>Nume universitate:</b> ".$univName."<br />";
echo "<b>Adresa:</b> ".$univAddr."<br />";
echo "<b>Telefon:</b> ".$univPhone."<br />";
echo "<b>Fax:</b> ".$univFax."<br />";
echo "<b>Email:</b> ".$univEmail."<br />";
echo "<b>Pagina web:</b> ".$univWeb."<br />";
echo "<b>Tip taxa:</b> ".$univTax."<br />";
mysqli_query($conn,"
INSERT INTO university
VALUES ('$a','$univCity','$univName','$univAddr','$univPhone','$univFax','$univEmail','$univWeb','$univTax')
");
$b++;
echo "<br><br>";
}
$a++;
}
}
// SESSION IT, IF THIS JOB IS COMPLETE
if($tmpVar) $_SESSION['LAST'] = $tmpVar;
$i++;
}
print "<h1>".($i-1)."</h1>";
print_r($info);