PHP将关联数组从一个函数传递到另一个函数,同时保持密钥不变

时间:2010-12-22 23:46:31

标签: php arrays associative-array

我有两个函数和一个数组

function componentBuildRoute(& $ query) {    $ page =“”;     $ segments = array();

//PAGE
if(isset($query["page"])){
    $page = $query["page"];
    unset($query["page"]);
} else {
    $page = "front";
}

//USERID
if(isset($query["userid"])){
    $segments["userid"] = $query["userid"];
    unset($query["userid"]);
}

//OPTION
if(isset($query["option"])){
    $segments["option"] = $query["option"];
    unset($query["option"]);
}

//CATID
if(isset($query["catid"])){
    $segments["catid"] = $query["catid"];
    $category_alias = getCategoryTitle($query["catid"]);
    $segments["category_alias"] = $category_alias;  
    unset($query["catid"]);
}

开关($ page){

    //page?"show_search",$catid,$option
    case "show_search":
        $segments["page"] = "mostrar_busqueda";
    break;

    //case "show_category",$catid,$option,$expand,$text_search,$order,$limitstart,$plugins  
    case "show_category":
        $segments["page"] = "mostrar_categoria";
    break;

    //case "show_rules",$option)
    case "show_rules":
        $segments["page"] = "mostrar_reglas";
    break;

    //case "show_ad",$adid,$option,$plugins
    case "show_ad":
        $segments["page"] = "mostrar_clasificado";
    break;

} 

返回$ segments;

}

/ ** *解析Route的方法 * @param array $ segments * / function componentParseRoute($ segments)
{     $ vars = array();

// Get the active menu item.
$menu   = &JSite::getMenu();
$item   = &$menu->getActive();
$vars["Itemid"] = $item -> id;

//OPTION
if(isset($segments["option"])){
    $vars["option"] = $segments["option"];
}


//CATID
if(isset($segments["catid"])){
    $vars["catid"] = $segments["catid"];
}


//$adid
if(isset($segments["adid"])){
    $vars["adid"] = $segments["adid"];
}

switch($segments["page"]){
    //?page="show_profile",$userid,$option
    case "mostrar_perfil":
        $vars["page"] = "show_profile";
    break;

    //page?"show_search",$catid,$option
    case "mostrar_busqueda":
        $vars["page"] = "show_search";
    break;

    //case show_category
            //may use $catid,$option,$expand,$text_search,$order,$limitstart,$plugins
    case "mostrar_categoria":
        $vars["page"] = "show_category";
    break;

    //case "show_rules",$option
    case "mostrar_reglas":
        $vars["page"] = "show_rules";
    break;

    //case "show_ad",$adid,$option,$plugins
    case "mostrar_clasificado":
        $vars["page"] = "show_ad";
    break;
}
echo "VARS 2: <pre>";
print_r($vars);
echo "</pre>";

return $vars;

}

但我没有将$ segments ['word'] ... word3']传递给第二个函数。

我正在传递

$segments[0];
$segments[1];
etc...

如何将整个数组传递给第二个函数,键及其值

$segments['word'] = value...
$segments['word1']
$segments['word2']

由于查询可能并不总是包含word1或word2,我认为我需要按键指定我所指的值。

谢谢,

3 个答案:

答案 0 :(得分:1)

使用array_keys函数。它将返回给定关联数组中的键数组。

答案 1 :(得分:1)

我很难理解你想要的东西,但就我而言 这将是解决方案

 function A (){
  $segments = array();
  $segments['word']=$var1;
  $segments['word2']=$var2;
  $segments['word3']=$var3;

  return $segments;
}

function B ($segments){
  $someVar=$segments['word'];
  $someVar1=$segments['word'];
  $someVar2=$segments['word'];
  $vars['other_stuff']...

  return $vars;
}

   $arr = a();
   $barr = b($arr);

答案 2 :(得分:1)

我不太明白你要完成什么,因为你的问题有点不清楚,你提出的代码是不正确的,但我会尝试给出一些你可能正在寻找的例子,给出以下功能A:

function A(){
  $segments = array();
  $segments['word'] = 'var1';
  $segments['word2'] = 'var2';
  $segments['word3'] = 'var3';

  return $segments;
}

选项1)从函数 B 调用函数 A

function B(){
  $segments = A();
  // Do whatever you need with the $segments here
}

选项2)将返回的数组从函数 A 传递给函数 B

function B($segments){
  // Do whatever you need with the $segments here
}

$segments = A();

B($segments);

选项3)将从函数 A 返回的各个值传递给函数 B

function B($var1, $var2){
  // Do whatever you need with the $var1 and $var2
}

$segments = A();

B($segments['word'], $segments['word2']);

如果您能澄清您的问题并准确告诉我们您要完成的任务,那么我们可能会帮助您做得更好。