php php解析数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10451306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:10:44  来源:igfitidea点击:

php parse array values

phparraysparsing

提问by bikey77

I'm not very good when it comes to arrays so here's probably something very simple but not for me! I'm getting an array of values via POST and I need to parse them and store the values in a table. How should I use the classic parsing such as:

我在数组方面不是很好,所以这可能是一些非常简单但不适合我的东西!我通过 POST 获取一组值,我需要解析它们并将值存储在表中。我应该如何使用经典解析,例如:

foreach($array as $a) {
  $text = $a->text;
  $name = $a->user->name;
}

etc to parse an array looking like this:

等来解析一个看起来像这样的数组:

[item] => Array
        (
            [tags] => Array
                (
                    [0] => Bluetooth
                    [1] => WiFi
                    [2] => USB
                )

        )

This is the entire POST array:

Array
(
    [prodid] => 
    [Submit] => Save
    [productcode] => 797987
    [cat_id] => 66
    [brand] => Fysiomed
    [name] =>  asdc asdc asd c
    [productnew] => yes
    [item] => Array
        (
            [tags] => Array
                (
                    [0] => Bluetooth
                    [1] => WiFi
                    [2] => USB
                )

        )

    [size] => 1
    [barcode] => 7979871
    [price] => 233.00
    [priceoffer] => 0.00
    [stock] => 50
    [weight] => 0.30
    [orderby] => 1
)

回答by

if(isset($_POST) && !empty($_POST)) {
  foreach($_POST as $key => $value) {
    if($key == 'item') {
      echo $value[$key]['tag'][0]. '<br>';
      echo $value[$key]['tag'][1]. '<br>';
      echo $value[$key]['tag'][2]. '<br>';
    } 
  }
}

回答by Pete

if( isset($_POST['item']) && isset($_POST['item']['tags']) ){
  foreach($_POST['item']['tags'] as $tag){
    //do stuff...e.g.
    echo $tag;
  }
}

回答by Sarvesh Kumar Singh

Looks like your array is shaped like this, check this

看起来你的阵列形状像这样,检查这个

$array = array( "item" => array( "tags" => array("Bluetooth", "Wifi", "USB" ) ) );
var_dump($array);

You will see something like this

你会看到这样的东西

array(1) {
  ["item"]=>
  array(1) {
    ["tags"]=>
    array(3) {
      [0]=>
      string(9) "Bluetooth"
      [1]=>
      string(4) "Wifi"
      [2]=>
      string(3) "USB"
    }
  }
}

Now for parsing this array,

现在解析这个数组,

foreach($array as $in => $val) {
    // as $array has key=>value pairs, only one key value pair
    // here $in will have the key and $val will have the value
    // $in will be "item"
    print $in; // this will print "item"
    foreach($val as $in2 => $val2 ){
        // only one key : "tags"
        print $in; // this will print "tags"
        print $val2[0];  // this will print "Bluetooth"
        print $val2[1];  // this will print "Wifi"
    } 
}

I hope this clears you doubt regarding arrays.

我希望这可以消除您对数组的疑虑。

回答by jarchuleta

Are you just trying to get the text out? try this.

你只是想把文字弄出来吗?尝试这个。

foreach($array['item']['tags'] as $tag) {
   $text = $tag;
}