简体   繁体   English

打印嵌套数组

[英]Printing nested array

I am a newbie to this array thing as still learning. 我仍然在学习这个数组的东西。 Need to print this nested array. 需要打印此嵌套数组。 Pls guide me how to do it as when trying to print it is not recognized. 请指导我如何做,因为尝试打印时无法识别。

$args = array(
        'pUserId'=>"veenu",
        "pPassword" => "somePass",
    "pCode" => 381,
    "pCity" => "DELHI",
        $pIn = array("TypeCode" => 22,"Subtype" => "21"));

The way I am trying to print is below 我尝试打印的方式如下

print_r($args[pIn] -> TypeCode);

Error when trying to print is "Undefined index: pIn" and "Trying to get property of non-object" 尝试打印时的错误是“ Undefined index:pIn”和“试图获取非对象的属性”

1.From what I see, you want to have a key pLn pointing to the nested array. 1.据我所见,您想要一个指向嵌套数组的键pLn。 This is how you do it: 这是您的操作方式:

"pIn" => array("TypeCode" => 22,"Subtype" => "21")

2.It's a good idea when using keys to enclose them in single quotes: 2.使用键将它们括在单引号中是一个好主意:

 print_r($args['pIn']['TypeCode']);

Cheers! 干杯!

It should be 它应该是

echo $args[$pIn]['TypeCode'];

This is a multi-dimensional array. 这是一个多维数组。

It has a parent array and an array with key $pIn . 它有一个父数组和一个键$pIn的数组。

You are putting $pIn as a key, but it is not defined. 您将$pIn作为键,但未定义。 But if you want to put just pIn (as string), you don't need to express it in a variable. 但是,如果您只想将pIn (作为字符串)放入,则无需在变量中表示它。

What you need is simply 您需要的只是

$args = array(
        "pUserId" => "veenu",
        "pPassword" => "somePass",
        "pCode" => 381,
        "pCity" => "DELHI",
        "pIn" => array(
                "TypeCode" => 22,
                "Subtype" => "21")
         );

As $args is now a multidimensional array, you will be able to get the TypeCode like this: 由于$args现在是一个多维数组,因此您将可以像下面这样获取TypeCode:

echo $args["pIn"]["TypeCode"];

As you have it written, you are assigning an array to the variable $pIn and including it in $args but not as an index. 如您所写,您正在为变量$ pIn分配一个数组,并将其包括在$ args中,但不作为索引。 Your array as defined looks like: 定义的数组如下所示:

array(5) {
  ["pUserId"]=>
  string(5) "veenu"
  ["pPassword"]=>
  string(8) "somePass"
  ["pCode"]=>
  int(381)
  ["pCity"]=>
  string(5) "DELHI"
  [0]=>
  array(2) {
    ["TypeCode"]=>
    int(22)
    ["Subtype"]=>
    string(2) "21"
  }
}

You probably want: 您可能想要:

$args = array(
        'pUserId'=>"veenu",
        "pPassword" => "somePass",
    "pCode" => 381,
    "pCity" => "DELHI",
    "pIn" => array("TypeCode" => 22,"Subtype" => "21"));

    print_r($args["pIn"]["TypeCode"]);  

Notice how the print_r accesses the element you want. 注意print_r如何访问所需的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM