Php 简单的数组访问和操作方法

Php 简单的数组访问和操作方法,php,arrays,Php,Arrays,我设计了一个阵列,我需要一些帮助,因为我有很多头疼的事。谁能给我一个简单的想法,如何用一种简单的方式来处理这个问题 当我尝试在数组中添加一些值时,大多数错误都是数组偏移错误 我的阵列: $info = array ( "id" => "", "city" => "", "university" => array ( "name" => "", "address" => "", "

我设计了一个阵列,我需要一些帮助,因为我有很多头疼的事。谁能给我一个简单的想法,如何用一种简单的方式来处理这个问题

当我尝试在数组中添加一些值时,大多数错误都是数组偏移错误

我的阵列:

$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,'&nbsp;&bull; ');
        if($ftext != '-')
        {
            // If string contains a dot it is a city
            if(strstr($rtext, '&bull;'))
            {
                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,'&nbsp;&bull; ');
                        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, '&bull;'))
                        {
                            $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,'&nbsp;&bull; ');
                                //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);
代码:

$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,'&nbsp;&bull; ');
        if($ftext != '-')
        {
            // If string contains a dot it is a city
            if(strstr($rtext, '&bull;'))
            {
                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,'&nbsp;&bull; ');
                        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, '&bull;'))
                        {
                            $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,'&nbsp;&bull; ');
                                //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);

在这些调用中,您以错误的方式访问阵列:

$info[$a]["city"] = $ftext;
$info[$a]["city"][$b]['university']['name'] = $ftext;
正确答案是:

$info["city"] = $ftext;
$info["city"]['university']['name'] = $ftext;

似乎要处理多个
$info
-数组。为此,您必须创建一个由这些信息数组组成的数组。

最终代码和工作代码,如果有人认为这有用:
// 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("&nbsp;","&bull;","&quot;","  ");

// 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,'&nbsp;&bull; ');
        if($ftext != '-')
        {
            // If string contains a dot it is a city
            if(strstr($rtext, '&bull;'))
            {
                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,'&nbsp;&bull; ');
                        // 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, '&bull;'))
                        {
                            $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,'&nbsp;&bull; ');
                                $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);
//HTML DOM解析器库
包括('hdp.php');
错误报告(真);
//了解上一个城市是什么
会话_start();
如果(!$\u会话['LAST'])$\u会话['LAST']=NULL;
/*
*返回具有字段关联名称和
*使用指定的分隔符提取的值。
*/
函数提取字段($str,$delim)
{
$val='';
//所有可能的字段
$posFlds=数组
(
“地址”=>“地址”,
'电话'=>'电话',
“传真”=>“传真”,
“电子邮件”=>“电子邮件”,
“网站”=>“网站”,
'提示'=>'类型'
);
foreach($posFlds作为$fldName=>$assocName)
{
if(strstr($str,$fldName))
{
$val=@explode($delim,$str);
如果(!$val[1])
{
打印“分隔符”。$delim。“未找到!”;
返回false;
}
返回数组($assocName,$val[1]);
}
}
返回false;
}
//正在尝试连接到本地服务器
$conn=mysqli_connect(“localhost”、“root”、“eductie”)或die(“无法连接到MySQL服务器!”);
if(mysqli\u connect\u errno($conn))
{
echo“未能连接到MySQL:”.mysqli_connect_error();
}
//DB->表查询结构
$info=数组
(
“城市”=>数组
(
“名称”=>“”,
“大学”=>数组
(
“名称”=>“”,
“地址”=>“”,
“电话”=>“”,
“传真”=>“”,
“电子邮件”=>“”,
“网站”=>“”,
“类型”=>“”,
“教员”=>数组
(
“名称”=>“”,
“地址”=>“”,
“电话”=>“”,
“传真”=>“”,
“电子邮件”=>“”,
“网站”=>“”,
“类型”=>“”,
)
),
)
);
//城市标识符
$cityid=0;
//行、列和深度标识符
$i=0$j=0$k=0;
//基本、辅助和第三个爬网url
$baseUrl='0http://infoportal.rtv.net/informatii_universitati.html';
$secondaryUrl=''$thirdUrl='';
//不带剥离标记的Html内容(按原样)
$htmlCu=file\u get\u html($baseUrl);
$htmltUn=''$htmltFa='';
//三级深度检查选择器
$selectCu=$htmlCu->find('td[width=“100%”]table td');
$selectUn=''$选择fa='';
//文本格式(原始和清理)
$rtext=''$ftext='';
//忽略代币
$banked=数组(“,”&bull;“,”,”);
//计数器A、B和C
$a=0;$b=0;$c=0;
//临时变量
$tmpVar='';
//从父td->子表->子td中选择每一行
foreach($selectCu作为$row)
{
//跳过第一行结果
如果($i!=0)
{
$rtext=$row->纯文本;
$ftext=trim($rtext,&bull;');
如果($ftext!='-')
{
//如果字符串包含一个点,则它是一个城市
if(strstr($rtext,&bull;'))
{
如果($_会话['LAST']!=$ftext)和($_会话['LAST']!==NULL))继续;
打印“->Oras:“.ftext.$br>”;
echo“SESSION:”.$_SESSION['LAST'].“|
”; $tmpVar=$ftext; } //如果string不是一个城市,那么它就是一所大学 其他的 { $a--; $info[$a]['city']['name']=$tmpVar; $info[$a][$city']['university'][$b]['name']=$ftext; $secondaryUrl=$row->find('a',0)->href; $htmlUn=file\u get\u html($secondaryUrl); $selectUn=$htmlUn->find('td[width=“100%”]table tr'); foreach($selectUn作为$col) { //跳过第一行结果 如果($j!=0) { $rtext=$col->纯文本; $ftext=trim($rtext,&bull;'); //键0->关联名称;键1->值 $field=extractField($ftext,,:'); 如果($field) { if(strstr($field[1],'de stat')) $info[$a]['city']['university'][$b][$field[0]]]='de stat'; 其他的 $info[$a]['city']['university'][$b][$field[0]]=$field[1]; } /////////////////////////////测试区///////////////////////////// //若字符串包含一个点,那个么它就是一个点 if(strstr($rtext,&bull;')) { $tempVar=修剪(str_replace($banked)、$ftext、“”); $thirdUrl=$col->find('a',0)->href; $htmlFa=文件获取html($thirdUrl); $selectFa=$htmlFa->find('td[width=“100%”]table tr'); foreach($selectFa作为$deep) { $rtext=$deep->plaintext; $ftext=trim($rtext,&bull;'); $info[$a][$a]['city']['university'][$b]['faculty'][$c]['name']=$tempVar; $field=extractField($ftext,,:'); 如果($field) {