2016-09-17 20 views
0
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) "[email protected]" 
     ["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) "[email protected]" 
     ["First_Name"]=> string(1) "G" 
     ["Hash"]=> string(32) "1444a7b2c86506158013d1175137eede" 
     ["IP_1"]=> string(0) "" ["Last_Name"]=> string(6) "Wilson" 
     ["Username"]=> string(13) "misterspeed76" 
    } 
    } 
} 

正在使用此代碼PHP的JSON數組未設置不工作

$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: [email protected] 
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: [email protected] 
First Name: G 
Hash: 1444a7b2c86506158013d1175137eede 
IP 1: 
Last Name: Wilson 
Username: misterspeed76 

現在我想取消設置RowKeyTimestamp陣列,但是當我在foreach聲明

unset($null_check['RowKey']; 

它不起作用,我爲每個外部或內部創建一個單獨的,不起作用,我使用foreach中指定的值不起作用。毫不起作用這只是我有這個約30更多的一部分。所有相同的格式,我只是想刪除RowKeyTimestamp鍵,我會怎麼做?

+0

'未設置($ null_check [ 'RowKey'];'有')'在缺少最後,如果這是一個錯字,那麼'unset($ null_check ['value'] [$ i] ['RowKey']);'只能作爲它的內部數組。或者使用'unset($ data ['RowKey']);'取消設置 – Sasikumar

+0

你打開了錯誤報告嗎?在文件頂部添加[錯誤報告](http://php.net/manual/en/function.error-reporting.php):'ini_set(「display_errors」,1); error_reporting(E_ALL);'你可能會遇到一些錯誤。但首先我不確定你是否知道你在做什麼和foreach循環。首先你要計算你的數組,它將返回1,因爲在第一維中,你只有值'value => Array(...)'。 – Rizier123

+0

這意味着您的for循環只執行1次迭代,並且在您的foreach循環中,您只需循環遍歷第一個子數組,基本上是'foreach($ null_check [「value」] [0] as $ key => $ value)''。因此,在那個foreach循環中,你可能會嘗試去掉元素,這是行不通的,因爲你正在處理一個元素的副本。所以你要麼使用原始數組的完整鍵,要麼通過引用循環元素。 – Rizier123

回答

0

使用此代碼

$count = count($null_check); 
for ($i = 0; $i <= $count; $i++) { 
    foreach ($null_check['value'][$i] as $key => $data) { 
     $parsed_key = str_replace('_', ' ', $key); 
     echo $parsed_key.': '.$data.'<br>'; 
     if(in_array($key,array('RowKey','Timestamp'))){ 
      unset($null_check['value'][$i][$key]); 
     } 
    } 
    echo '<br><br>'; 
} 
echo '<pre>'; 
print_r($null_check); 
echo '</pre>'; 
+0

代碼:'$ count = count($ null_check); \t \t爲($ I = 0; $數據<$計數; $ I ++){ \t \t \t的foreach($ null_check [ '值'] [$ i]於作爲$鍵=> $數據){ \t \t \t \t $ parsed_key = str_replace('_','',$ key); \t \t \t \t echo $ parsed_key。':'。$ data。'
'; \t \t \t \t如果(in_array($鍵,陣列( 'RowKey', '時間戳'))){ \t \t \t \t \t未設置($ null_check [ '值'] [$ i]於[$鍵]); \t \t \t \t} \t \t \t} \t \t \t回聲

'; \t \t}'輸出是一樣的 –

+0

輸出是一樣的,但是現在你的數組不包含'RowKey'和'Timestamp'。檢查'foreach',因爲我沒有設置密鑰 –

0

您可以使用unset()array_diff_key(),方法如下:(只是改變\n年代到<br>的)

Demo

輸入:

$array=[ 
    "value"=>[ 
     ["PartitionKey"=>"AWT-GPI.com", 
     "RowKey"=>"0024a6ac-6cf1-454a-91b2-15bfec3a3d86", 
     "Timestamp"=>"2016-09-09T20:16:26.8674483Z", 
     "Email"=>"[email protected]", 
     "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"=>"[email protected]", 
     "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 : [email protected] 
First Name : Jennifer 
Hash : d656d0c21b8f3c14fe03232bb68d1b53 
IP 1 : 
Last Name : Young 
Username : flacobell 

PartitionKey : AWT-GPI.com 
Email : [email protected] 
First Name : G 
Hash : 1444a7b2c86506158013d1175137eede 
IP 1 : 
Last Name : Wilson 
Username : misterspeed76 


--- 
PartitionKey : AWT-GPI.com 
Email : [email protected] 
First Name : Jennifer 
Hash : d656d0c21b8f3c14fe03232bb68d1b53 
IP 1 : 
Last Name : Young 
Username : flacobell 

PartitionKey : AWT-GPI.com 
Email : [email protected] 
First Name : G 
Hash : 1444a7b2c86506158013d1175137eede 
IP 1 : 
Last Name : Wilson 
Username : misterspeed76