PHP Json数组未设置无法正常工作 [英] Php Json Array Unset Not Working

查看:100
本文介绍了PHP Json数组未设置无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  array(1) { 
  ["value"] => array(1000) { 
    [0]=> array(9) { 
      ["PartitionKey"]=> string(11)"AWT-GPI.com" 
      ["RowKey"]=> string(36) "0024a6ac-6cf1-454a-91b2-15bfec3a3d86" 
      ["Timestamp"]=> string(28) "2016-09-09T20:16:26.8674483Z" 
      ["Email"]=> string(20) "ginyoung30@gmail.com" 
      ["First_Name"]=> string(8) "Jennifer" 
      ["Hash"]=> string(32) "d656d0c21b8f3c14fe03232bb68d1b53" 
      ["IP_1"]=> string(0) "" 
      ["Last_Name"]=> string(5) "Young" 
      ["Username"]=> string(9) "flacobell" 
    } 
    [1]=> array(9) { 
      ["PartitionKey"]=> string(11) "AWT-GPI.com" 
      ["RowKey"]=> string(36) "002c00c4-e064-43e8-9dd8-319c8d6663bd" 
      ["Timestamp"]=> string(28) "2016-09-09T20:19:54.5500874Z" 
      ["Email"]=> string(22) "Glenn@flavorleague.com" 
      ["First_Name"]=> string(1) "G" 
      ["Hash"]=> string(32) "1444a7b2c86506158013d1175137eede" 
      ["IP_1"]=> string(0) "" ["Last_Name"]=> string(6) "Wilson" 
      ["Username"]=> string(13) "misterspeed76" 
    } 
  } 
}

这是使用此代码的数组

$count = count($null_check);
for ($i = 0; $data < $count; $i++) {
    foreach ($null_check['value'][$i] as $key => $data) {
        $parsed_key = str_replace('_', ' ', $key);
        echo $parsed_key.': '.$data.'<br>';
    }
    echo '<br><br>';
}

我能够得到这个输出

PartitionKey: AWT-GPI.com
RowKey: 0024a6ac-6cf1-454a-91b2-15bfec3a3d86
Timestamp: 2016-09-09T20:16:26.8674483Z
Email: ginyoung30@gmail.com
First Name: Jennifer
Hash: d656d0c21b8f3c14fe03232bb68d1b53
IP 1: 
Last Name: Young
Username: flacobell


PartitionKey: AWT-GPI.com
RowKey: 002c00c4-e064-43e8-9dd8-319c8d6663bd
Timestamp: 2016-09-09T20:19:54.5500874Z
Email: Glenn@flavorleague.com
First Name: G
Hash: 1444a7b2c86506158013d1175137eede
IP 1: 
Last Name: Wilson
Username: misterspeed76

现在,我想取消设置RowKeyTimestamp,但是当我在foreach语句中进行操作

Now I want to unset RowKey and Timestamp, however when I do in the foreach statement

unset($null_check['RowKey'];

这是行不通的,我为每个外部或内部都创建了一个单独的行,这行不通,我使用在foreach中分配的值行不通.从字面上看是行不通的.我只剩下大约30个这样的部分.所有格式相同,我只想删除RowKeyTimestamp键,我该怎么做?

It doesn't work, I create a separate for each outside or inside, doesn't work, I use the value assigned in the foreach doesn't work. Literally nothing works. This is only a part I have about 30 more just like this. All same format, I just want to remove the RowKey and Timestamp Key, how would I do this?

推荐答案

您可以使用unset()array_diff_key(),方法如下:(只需将\n更改为<br>)

You can use unset() or array_diff_key(), here's how: (just change the \n's to <br>'s)

(演示)

输入:

$array=[
    "value"=>[
        ["PartitionKey"=>"AWT-GPI.com",
         "RowKey"=>"0024a6ac-6cf1-454a-91b2-15bfec3a3d86",
         "Timestamp"=>"2016-09-09T20:16:26.8674483Z",
         "Email"=>"ginyoung30@gmail.com",
         "First_Name"=>"Jennifer",
         "Hash"=>"d656d0c21b8f3c14fe03232bb68d1b53",
         "IP_1"=>"",
         "Last_Name"=>"Young",
         "Username"=>"flacobell"
        ],
        ["PartitionKey"=>"AWT-GPI.com",
         "RowKey"=>"002c00c4-e064-43e8-9dd8-319c8d6663bd",
         "Timestamp"=>"2016-09-09T20:19:54.5500874Z",
         "Email"=>"Glenn@flavorleague.com",
         "First_Name"=>"G",
         "Hash"=>"1444a7b2c86506158013d1175137eede",
         "IP_1"=>"",
         "Last_Name"=>"Wilson",
         "Username"=>"misterspeed76"
        ]
    ]
];

方法1:

foreach($array['value'] as $subarray){
    foreach(array_diff_key($subarray,['RowKey'=>'','Timestamp'=>'']) as $k=>$v){  // does not modify $array
        echo str_replace('_',' ',$k)," : $v\n";
    }
    echo "\n";
}

方法2:

echo "\n---\n";

foreach($array['value'] as $subarray){
    unset($subarray['RowKey'],$subarray['Timestamp']);  // does not modify $array
    foreach($subarray as $k=>$v){
        echo str_replace('_',' ',$k)," : $v\n";
    }
    echo "\n";
}

输出:

PartitionKey : AWT-GPI.com
Email : ginyoung30@gmail.com
First Name : Jennifer
Hash : d656d0c21b8f3c14fe03232bb68d1b53
IP 1 : 
Last Name : Young
Username : flacobell

PartitionKey : AWT-GPI.com
Email : Glenn@flavorleague.com
First Name : G
Hash : 1444a7b2c86506158013d1175137eede
IP 1 : 
Last Name : Wilson
Username : misterspeed76


---
PartitionKey : AWT-GPI.com
Email : ginyoung30@gmail.com
First Name : Jennifer
Hash : d656d0c21b8f3c14fe03232bb68d1b53
IP 1 : 
Last Name : Young
Username : flacobell

PartitionKey : AWT-GPI.com
Email : Glenn@flavorleague.com
First Name : G
Hash : 1444a7b2c86506158013d1175137eede
IP 1 : 
Last Name : Wilson
Username : misterspeed76

这篇关于PHP Json数组未设置无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆