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) "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" 
    } 
  } 
}

That is the array using this code

$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>';
}

I am able to get this output

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

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

unset($null_check['RowKey'];

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?

Shri Suresh
  • 463
  • 1
  • 5
  • 20
Alex Rieker
  • 93
  • 2
  • 8
  • `unset($null_check['RowKey'];` there is `)` missing at the end, if thats a typo, then `unset($null_check['value'][$i]['RowKey']);` only will work as its a inner array. or use `unset($data['RowKey']);` to unset – Sasikumar Sep 17 '16 at 03:21
  • Have you turned on error reporting? Add [error reporting](http://php.net/manual/en/function.error-reporting.php) at the top of your file(s): `ini_set("display_errors", 1); error_reporting(E_ALL);` and you probably get some errors. But first off I'm not sure if you know what you are doing with your for and foreach loop. First you count your array, which will just return 1, since in the first dimension you just have the value `value => Array(...)`. – Rizier123 Sep 17 '16 at 03:28
  • So that means your for loop does just 1 iteration and in your foreach loop you just loop over the first subArray, basically `foreach($null_check["value"][0] as $key => $value)`. And so in that foreach loop you then probably try to unset the element, which won't work, since you are working on a copy of the element. So you either want to use the full key from the original array or loop through the elements by reference. – Rizier123 Sep 17 '16 at 03:28
  • Yes that was a typo, I did use `unset($data['RowKey']);` it didn't work. `unset($null_check['value'][$i]['RowKey']);` didn't work either. What do you mean by reference? So I should unset outside the foreach? That doesn't work either. Then rather than deleting can I skip certain keys or will deleting be easier? – Alex Rieker Sep 17 '16 at 03:31
  • Your output does not match up with your code nor with your array shown. Just give us a [mcve]. – Rizier123 Sep 17 '16 at 03:47

2 Answers2

0

Use this code

$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>';
Shri Suresh
  • 463
  • 1
  • 5
  • 20
  • Code: `$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.'
    '; if(in_array($key,array('RowKey','Timestamp'))){ unset($null_check['value'][$i][$key]); } } echo '

    '; }` Output is the same
    – Alex Rieker Sep 17 '16 at 14:45
  • Output is same,but now your array doesn't contain `RowKey` and `Timestamp ` . check `foreach` in that I've unset keys – Shri Suresh Sep 18 '16 at 04:58
0

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

(Demo)

Input:

$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"
        ]
    ]
];

Method #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";
}

Method #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";
}

Output:

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
mickmackusa
  • 43,625
  • 12
  • 83
  • 136