Array in array for array_shift
I have several arrays from $ _REQUEST. For example: $ _REQUEST ['name'] and $ _REQUEST ['email']. That is, inside these arrays there is also an array. It turns out an array in an array.
Assigned them value
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
now I need to remove the first key with the value. For this I used array_shift
array_shift($name);
array_shift($email);
so i got rid of the first value. But, besides the name and email, there are others. It would not be desirable for everyone to write array_shift. How can I apply to all with one function?
thanx
UPD
For example $_REQUEST array:
Array (
[name] => Array (
[0] =>
[1] => myName
)
[email] => Array (
[0] =>
[1] => myEmail
)
[other] => Array (
[0] =>
[1] => otherDatas
)
)
I must get rid of these empty elements
php arrays
add a comment |
I have several arrays from $ _REQUEST. For example: $ _REQUEST ['name'] and $ _REQUEST ['email']. That is, inside these arrays there is also an array. It turns out an array in an array.
Assigned them value
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
now I need to remove the first key with the value. For this I used array_shift
array_shift($name);
array_shift($email);
so i got rid of the first value. But, besides the name and email, there are others. It would not be desirable for everyone to write array_shift. How can I apply to all with one function?
thanx
UPD
For example $_REQUEST array:
Array (
[name] => Array (
[0] =>
[1] => myName
)
[email] => Array (
[0] =>
[1] => myEmail
)
[other] => Array (
[0] =>
[1] => otherDatas
)
)
I must get rid of these empty elements
php arrays
1
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Do you want to shift all arrays in$_REQUEST
? Then you could just loop over them:foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?
– Karsten Koop
Nov 16 '18 at 9:20
add a comment |
I have several arrays from $ _REQUEST. For example: $ _REQUEST ['name'] and $ _REQUEST ['email']. That is, inside these arrays there is also an array. It turns out an array in an array.
Assigned them value
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
now I need to remove the first key with the value. For this I used array_shift
array_shift($name);
array_shift($email);
so i got rid of the first value. But, besides the name and email, there are others. It would not be desirable for everyone to write array_shift. How can I apply to all with one function?
thanx
UPD
For example $_REQUEST array:
Array (
[name] => Array (
[0] =>
[1] => myName
)
[email] => Array (
[0] =>
[1] => myEmail
)
[other] => Array (
[0] =>
[1] => otherDatas
)
)
I must get rid of these empty elements
php arrays
I have several arrays from $ _REQUEST. For example: $ _REQUEST ['name'] and $ _REQUEST ['email']. That is, inside these arrays there is also an array. It turns out an array in an array.
Assigned them value
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
now I need to remove the first key with the value. For this I used array_shift
array_shift($name);
array_shift($email);
so i got rid of the first value. But, besides the name and email, there are others. It would not be desirable for everyone to write array_shift. How can I apply to all with one function?
thanx
UPD
For example $_REQUEST array:
Array (
[name] => Array (
[0] =>
[1] => myName
)
[email] => Array (
[0] =>
[1] => myEmail
)
[other] => Array (
[0] =>
[1] => otherDatas
)
)
I must get rid of these empty elements
php arrays
php arrays
edited Nov 16 '18 at 9:22
Гани Шахмурат
asked Nov 16 '18 at 9:14
Гани ШахмуратГани Шахмурат
74
74
1
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Do you want to shift all arrays in$_REQUEST
? Then you could just loop over them:foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?
– Karsten Koop
Nov 16 '18 at 9:20
add a comment |
1
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Do you want to shift all arrays in$_REQUEST
? Then you could just loop over them:foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?
– Karsten Koop
Nov 16 '18 at 9:20
1
1
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Do you want to shift all arrays in
$_REQUEST
? Then you could just loop over them: foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?– Karsten Koop
Nov 16 '18 at 9:20
Do you want to shift all arrays in
$_REQUEST
? Then you could just loop over them: foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?– Karsten Koop
Nov 16 '18 at 9:20
add a comment |
3 Answers
3
active
oldest
votes
If I understand correctly, you have multiple arrays in your request and you want to do an array_shift
on all of them?
You could loop through your $_REQUEST
and apply that function to all the arrays. Maybe like this:
foreach ($_REQUEST as &$value) {
if (is_array($value) && empty($value[0])) {
array_shift($value);
}
}
That will shift all arrays in your request and leave any other variables alone.
EDIT: Updated the example to only shift arrays where the first element is empty.
EDIT2: Added &
to $value
so that you can change the $_REQUEST
variable directly.
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the$_REQUEST
again at the end. My code doesn't change the$_REQUEST
variable. If you want to do that then you can use&$value
. See my updated answer as well.
– Dirk Scholten
Nov 16 '18 at 10:37
add a comment |
array_filter will clear empty values even if they are at any index.
$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
$cleanArray[$key] = array_filter($value);
}
Output
Array (
[name] => Array (
[1] => myName
)
[email] => Array (
[1] => myEmail
)
[other] => Array (
[1] => otherDatas
)
)
Please note, it will also clear values like null
, (blank)
and false
. Example Reference
add a comment |
What you might do is create an array with the keys that you want from $_REQUEST
and use array_intersect_key to get your subset.
Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:
$keys = [
"name",
"email"
];
$result = array_map(function ($x) {
if (is_array($x)) {
return array_filter($x, function($y){
return null !== $y && "" !== trim($y);
});
}
return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));
print_r($result);
Demo
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334697%2farray-in-array-for-array-shift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand correctly, you have multiple arrays in your request and you want to do an array_shift
on all of them?
You could loop through your $_REQUEST
and apply that function to all the arrays. Maybe like this:
foreach ($_REQUEST as &$value) {
if (is_array($value) && empty($value[0])) {
array_shift($value);
}
}
That will shift all arrays in your request and leave any other variables alone.
EDIT: Updated the example to only shift arrays where the first element is empty.
EDIT2: Added &
to $value
so that you can change the $_REQUEST
variable directly.
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the$_REQUEST
again at the end. My code doesn't change the$_REQUEST
variable. If you want to do that then you can use&$value
. See my updated answer as well.
– Dirk Scholten
Nov 16 '18 at 10:37
add a comment |
If I understand correctly, you have multiple arrays in your request and you want to do an array_shift
on all of them?
You could loop through your $_REQUEST
and apply that function to all the arrays. Maybe like this:
foreach ($_REQUEST as &$value) {
if (is_array($value) && empty($value[0])) {
array_shift($value);
}
}
That will shift all arrays in your request and leave any other variables alone.
EDIT: Updated the example to only shift arrays where the first element is empty.
EDIT2: Added &
to $value
so that you can change the $_REQUEST
variable directly.
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the$_REQUEST
again at the end. My code doesn't change the$_REQUEST
variable. If you want to do that then you can use&$value
. See my updated answer as well.
– Dirk Scholten
Nov 16 '18 at 10:37
add a comment |
If I understand correctly, you have multiple arrays in your request and you want to do an array_shift
on all of them?
You could loop through your $_REQUEST
and apply that function to all the arrays. Maybe like this:
foreach ($_REQUEST as &$value) {
if (is_array($value) && empty($value[0])) {
array_shift($value);
}
}
That will shift all arrays in your request and leave any other variables alone.
EDIT: Updated the example to only shift arrays where the first element is empty.
EDIT2: Added &
to $value
so that you can change the $_REQUEST
variable directly.
If I understand correctly, you have multiple arrays in your request and you want to do an array_shift
on all of them?
You could loop through your $_REQUEST
and apply that function to all the arrays. Maybe like this:
foreach ($_REQUEST as &$value) {
if (is_array($value) && empty($value[0])) {
array_shift($value);
}
}
That will shift all arrays in your request and leave any other variables alone.
EDIT: Updated the example to only shift arrays where the first element is empty.
EDIT2: Added &
to $value
so that you can change the $_REQUEST
variable directly.
edited Nov 16 '18 at 10:37
answered Nov 16 '18 at 9:20
Dirk ScholtenDirk Scholten
572318
572318
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the$_REQUEST
again at the end. My code doesn't change the$_REQUEST
variable. If you want to do that then you can use&$value
. See my updated answer as well.
– Dirk Scholten
Nov 16 '18 at 10:37
add a comment |
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the$_REQUEST
again at the end. My code doesn't change the$_REQUEST
variable. If you want to do that then you can use&$value
. See my updated answer as well.
– Dirk Scholten
Nov 16 '18 at 10:37
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
not all requests, just a few
– Гани Шахмурат
Nov 16 '18 at 9:26
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
– Dirk Scholten
Nov 16 '18 at 9:33
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
@ГаниШахмурат this answer you should use
– Jbadminton
Nov 16 '18 at 9:36
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
– Гани Шахмурат
Nov 16 '18 at 10:21
That's because in that sandbox you print out the
$_REQUEST
again at the end. My code doesn't change the $_REQUEST
variable. If you want to do that then you can use &$value
. See my updated answer as well.– Dirk Scholten
Nov 16 '18 at 10:37
That's because in that sandbox you print out the
$_REQUEST
again at the end. My code doesn't change the $_REQUEST
variable. If you want to do that then you can use &$value
. See my updated answer as well.– Dirk Scholten
Nov 16 '18 at 10:37
add a comment |
array_filter will clear empty values even if they are at any index.
$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
$cleanArray[$key] = array_filter($value);
}
Output
Array (
[name] => Array (
[1] => myName
)
[email] => Array (
[1] => myEmail
)
[other] => Array (
[1] => otherDatas
)
)
Please note, it will also clear values like null
, (blank)
and false
. Example Reference
add a comment |
array_filter will clear empty values even if they are at any index.
$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
$cleanArray[$key] = array_filter($value);
}
Output
Array (
[name] => Array (
[1] => myName
)
[email] => Array (
[1] => myEmail
)
[other] => Array (
[1] => otherDatas
)
)
Please note, it will also clear values like null
, (blank)
and false
. Example Reference
add a comment |
array_filter will clear empty values even if they are at any index.
$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
$cleanArray[$key] = array_filter($value);
}
Output
Array (
[name] => Array (
[1] => myName
)
[email] => Array (
[1] => myEmail
)
[other] => Array (
[1] => otherDatas
)
)
Please note, it will also clear values like null
, (blank)
and false
. Example Reference
array_filter will clear empty values even if they are at any index.
$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
$cleanArray[$key] = array_filter($value);
}
Output
Array (
[name] => Array (
[1] => myName
)
[email] => Array (
[1] => myEmail
)
[other] => Array (
[1] => otherDatas
)
)
Please note, it will also clear values like null
, (blank)
and false
. Example Reference
edited Nov 16 '18 at 9:31
answered Nov 16 '18 at 9:24
SamirSamir
5,3692628
5,3692628
add a comment |
add a comment |
What you might do is create an array with the keys that you want from $_REQUEST
and use array_intersect_key to get your subset.
Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:
$keys = [
"name",
"email"
];
$result = array_map(function ($x) {
if (is_array($x)) {
return array_filter($x, function($y){
return null !== $y && "" !== trim($y);
});
}
return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));
print_r($result);
Demo
add a comment |
What you might do is create an array with the keys that you want from $_REQUEST
and use array_intersect_key to get your subset.
Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:
$keys = [
"name",
"email"
];
$result = array_map(function ($x) {
if (is_array($x)) {
return array_filter($x, function($y){
return null !== $y && "" !== trim($y);
});
}
return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));
print_r($result);
Demo
add a comment |
What you might do is create an array with the keys that you want from $_REQUEST
and use array_intersect_key to get your subset.
Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:
$keys = [
"name",
"email"
];
$result = array_map(function ($x) {
if (is_array($x)) {
return array_filter($x, function($y){
return null !== $y && "" !== trim($y);
});
}
return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));
print_r($result);
Demo
What you might do is create an array with the keys that you want from $_REQUEST
and use array_intersect_key to get your subset.
Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:
$keys = [
"name",
"email"
];
$result = array_map(function ($x) {
if (is_array($x)) {
return array_filter($x, function($y){
return null !== $y && "" !== trim($y);
});
}
return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));
print_r($result);
Demo
edited Nov 16 '18 at 10:06
answered Nov 16 '18 at 9:32
The fourth birdThe fourth bird
24.5k81629
24.5k81629
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334697%2farray-in-array-for-array-shift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Provide array structure and expected output for clarity.
– Samir
Nov 16 '18 at 9:15
Its better to have a example array for us to play :)
– j3thamz
Nov 16 '18 at 9:17
Do you want to shift all arrays in
$_REQUEST
? Then you could just loop over them:foreach($_REQUEST as $r) array_shift($r);
Or do you have a list/array of all variables that should be processed?– Karsten Koop
Nov 16 '18 at 9:20