嗨,我设计了一个数组,我需要一些帮助,因为我很头疼。谁能给我一个简单的想法如何以简单的方式使用它?
当我尝试在我的数组中添加一些值时,大多数错误都是数组偏移错误。
我的数组:
$info = array
(
"id" => "",
"city" => "",
"university" => array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
)
);
代码:
<?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);