-2

I am getting following array on submit of the form

[_wpcf7] => 3
[_wpcf7_version] => 4.9.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f3-p2-o1
[_wpcf7_container_post] => 2
[your-name] => sad
[your-subject] => sad
[your-message] => sdsa

However I need it to be trimmed and moved to another array like

[your-name] => sad
[your-subject] => sad
[your-message] => sdsa

Actually I need to remove the contents of array

[_wpcf7] => 3
[_wpcf7_version] => 4.9.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f3-p2-o1
[_wpcf7_container_post] => 2

So please guide me how can I do it using PHP script

4 Answers4

1

Save only desired keys in array

$array2 = array(
 'your-name',
 'your-subject',
 'your-message',
);

$res = array_intersect_key($array1, array_flip($array2));

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
0

Try this code:

$array2 = array(
 'your-name' => $array1['your-name'],
 'your-subject' => $array1['your-subject'],
 'your-message' => $array1['your-message'],
);
Vladimir
  • 1,373
  • 8
  • 12
  • Thanks for your answer. Is there a way I can remove the above fields as the fields you have mentioned is coming dynamically However the above fields are constant. – Pratik Bhatt Jan 05 '18 at 07:19
0

if it's always the last three items (as you confirmed in comments) you can use this code.
It will first get all key names and then loop the keys backwards three times to get the last three items and save them to $namearr

$arr = ["_wpcf7" => 3,
"_wpcf7_version" => "4.9.2",
"_wpcf7_locale" => "en_US",
"_wpcf7_unit_tag" => "wpcf7-f3-p2-o1",
"_wpcf7_container_post" => 2,
"your-name" => "sad",
"your-subject" => "sad",
"your-message" => "sdsa"];

$keys = array_keys($arr);
$count = count($keys)-1;
$namearr=[];
for($i = $count; $i >$count-3;$i-- ){
    $namearr[$keys[$i]] = $arr[$keys[$i]];
}
var_dump($namearr);

output:

array(3) {
  "your-message" => "sdsa"
  "your-subject" => "sad"
  "your-name" => "sad"
}

https://3v4l.org/L9Cmp

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Simple and easy :- "Remove the keys starting from _wpcf7 and Store the remaining ones in another array"

<?php
    $oldArray = array(('_wpcf7') => '3',
        ('_wpcf7_version') => '4.9.2',
        ('_wpcf7_locale') => 'en_US',
        ('_wpcf7_unit_tag') => 'wpcf7-f3-p2-o1',
        ('_wpcf7_container_post') => '2',
        ('your-name') => 'sad',
        ('your-subject') => 'sad',
        ('your-message') => 'sdsa');
    $newArray = array();
    foreach($oldArray as $key => $value) {
        if (substr($key,0,6) !== '_wpcf7')
            $newArray[$key] = $value;
    }
?>

Check this code also :- Fiddle
Happy Coding :-)

Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
  • @Yash OP can't upvote. It's a privilege you earn. Why do you think your answer should be upvoted? As far as I know upvotes are used on good answers. This is not an answer, it's only code. If anything it should be downvoted – Andreas Jan 05 '18 at 09:27
  • @Andreas Actually you are right. This is not an answer but Its far **better** than your answer. Mind one thing, answer that matters most must be simple, easy to understand and minimal code. – Yash Parekh Jan 05 '18 at 09:53
  • in what way is my answer hard to understand? – Andreas Jan 05 '18 at 09:58
  • not able to find any comments with **hard to understand**. It depends on the questioner. – Yash Parekh Jan 05 '18 at 10:15
  • @YashParekh What are you talking about. I have comments to my code. It's written above the code. I'll copy it here for you: *It will first get all key names and then loop the keys backwards three times to get the last three items and save them to $namearr* – Andreas Jan 05 '18 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162592/discussion-between-yash-parekh-and-andreas). – Yash Parekh Jan 05 '18 at 11:32