PHP数组在函数外部为空

时间:2013-12-14 07:20:21

标签: php arrays function global-variables scope

<?php
echo '<pre>';
error_reporting(E_ALL);

$pid = '129';

$families = array
(
      "Griffin"=>array
                      (
                      "PTY"=>"Peter",
                      "STY"=>"X",
                      "QTY"=>"A|F"
                      ),
      "Quagmire"=>array
                      (
                      "NTY"=>"Glenn"
                      ),
      "Brown"=>array
                  (
                  "FTY"=>"Cleveland",
                  "OTY"=>"Q|G|T|Y",
                  "PTY"=>"Junior"
                  )
);

global $allid;

$allid = array();
function buildid($pid,$key,$val){

    if (preg_match("/\|/",$val)){
        $val = explode("|",$val);
            foreach($val as $val1){
                $id = $pid.'-'.$key.'-'.$val1;
                $allid[] = $id;

            }
    }
}

print_r($allid);  

foreach($families as $familieskey=>$familiesvalue){
    foreach($familiesvalue as $skey=>$sval){
        buildid($pid,$skey,$sval);
    }
}
echo '</pre>';
?>

以上代码的预期输出:

情况1:

   Array
    (
        [0] => 129-QTY-A
        [1] => 129-QTY-F
    )
    Array
    (
        [0] => 129-OTY-Q
        [1] => 129-OTY-G
        [2] => 129-OTY-T
        [3] => 129-OTY-Y
    )

情况2:

 Array
    (
      [0] => 129-QTY-A
      [1] => 129-QTY-F
      [2] => 129-OTY-Q
      [3] => 129-OTY-G
      [4] => 129-OTY-T
      [5] => 129-OTY-Y
    )

3 个答案:

答案 0 :(得分:3)

global $allid;进入函数本身,而不是在外面,即

   function buildid($pid,$key,$val){
        global $allid;
        if (preg_match("/\|/",$val)){
    ...

documentation指出

  

在函数外部使用global关键字不是错误。如果文件包含在函数内部,则可以使用它。

- &GT;除非你从函数内部包含它,否则它没有效果。

编辑添加:您还需要在运行代码后放置print_r(即echo '</pre>';之前) - 目前,您正在初始化数组后显示数组的内容,然后用数据填充它,然后程序结束。

答案 1 :(得分:0)

首先。在设置此数组之前,您需要print_r($allid)。你只在“foreach($families as $familieskey=>$familiesvalue){ ...”]

中做到了

<强> Second.Done。

<强> TRY

error_reporting(E_ALL);

$pid = '129';

$families = array
(
      "Griffin"=>array
                      (
                      "PTY"=>"Peter",
                      "STY"=>"X",
                      "QTY"=>"A|F"
                      ),
      "Quagmire"=>array
                      (
                      "NTY"=>"Glenn"
                      ),
      "Brown"=>array
                  (
                  "FTY"=>"Cleveland",
                  "OTY"=>"Q|G|T|Y",
                  "PTY"=>"Junior"
                  )
);


function buildid($pid,$key,$val){
 global $allid;
    if (preg_match("/\|/",$val)){
        $val = explode("|",$val);
            foreach($val as $val1){
                $id = $pid.'-'.$key.'-'.$val1;
                $allid[] = $id;

            }
    }
}



foreach($families as $familieskey=>$familiesvalue){
    foreach($familiesvalue as $skey=>$sval){
        buildid($pid,$skey,$sval);
    }
}


print_r($allid); 

答案 2 :(得分:0)

在设置其值之前输出数组。所以代码应该是这样的。 您的函数buildid()已定义,但在输出数组之前未调用。 所以print_r($ allid);应该称为afters,它的值在foreach循环中填充。

<?php
echo '<pre>';
error_reporting(E_ALL);

$pid = '129';

$families = array
(
    "Griffin"=>array
    (
        "PTY"=>"Peter",
        "STY"=>"X",
        "QTY"=>"A|F"
    ),
    "Quagmire"=>array
    (
        "NTY"=>"Glenn"
    ),
    "Brown"=>array
    (
        "FTY"=>"Cleveland",
        "OTY"=>"Q|G|T|Y",
        "PTY"=>"Junior"
    )
);

$allid = array();

function buildid($pid,$key,$val){
     //Global should be inside function like this.
    global $allid;
    if (preg_match("/\|/",$val)){
        $val = explode("|",$val);
        foreach($val as $val1){
            $id = $pid.'-'.$key.'-'.$val1;
            $allid[] = $id;

        }
    }
}

foreach($families as $familieskey=>$familiesvalue){
    foreach($familiesvalue as $skey=>$sval){
        buildid($pid,$skey,$sval);
    }
}
print_r($allid);
echo '</pre>';
?>