Conditionally showing values in a PHP array [duplicate]
This question already has an answer here:
Remove empty array elements
25 answers
I have a PHP array which has some values that am storing to an API,, I want to show the field that has a value and omit the others but aint sure how to write the logic in PHP,,
~ Kindly assist?
array:5 [
"child1Dob" => "2018-11-07",
"child2Dob" => null,
"child3Dob" => null,
"child4Dob" => null,
"child5Dob" => null,
]
php arrays
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 14:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Remove empty array elements
25 answers
I have a PHP array which has some values that am storing to an API,, I want to show the field that has a value and omit the others but aint sure how to write the logic in PHP,,
~ Kindly assist?
array:5 [
"child1Dob" => "2018-11-07",
"child2Dob" => null,
"child3Dob" => null,
"child4Dob" => null,
"child5Dob" => null,
]
php arrays
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 14:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
1
Orarray_filter
, for that matter.
– Federico klez Culloca
Nov 21 '18 at 14:49
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
1
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53
|
show 2 more comments
This question already has an answer here:
Remove empty array elements
25 answers
I have a PHP array which has some values that am storing to an API,, I want to show the field that has a value and omit the others but aint sure how to write the logic in PHP,,
~ Kindly assist?
array:5 [
"child1Dob" => "2018-11-07",
"child2Dob" => null,
"child3Dob" => null,
"child4Dob" => null,
"child5Dob" => null,
]
php arrays
This question already has an answer here:
Remove empty array elements
25 answers
I have a PHP array which has some values that am storing to an API,, I want to show the field that has a value and omit the others but aint sure how to write the logic in PHP,,
~ Kindly assist?
array:5 [
"child1Dob" => "2018-11-07",
"child2Dob" => null,
"child3Dob" => null,
"child4Dob" => null,
"child5Dob" => null,
]
This question already has an answer here:
Remove empty array elements
25 answers
php arrays
php arrays
asked Nov 21 '18 at 14:47
Patweb
716
716
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 14:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 14:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
1
Orarray_filter
, for that matter.
– Federico klez Culloca
Nov 21 '18 at 14:49
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
1
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53
|
show 2 more comments
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
1
Orarray_filter
, for that matter.
– Federico klez Culloca
Nov 21 '18 at 14:49
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
1
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
1
1
Or
array_filter
, for that matter.– Federico klez Culloca
Nov 21 '18 at 14:49
Or
array_filter
, for that matter.– Federico klez Culloca
Nov 21 '18 at 14:49
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
1
1
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53
|
show 2 more comments
2 Answers
2
active
oldest
votes
Try this
$b = array_filter($a, function($k) { return $k!=null; });
Working example here.
add a comment |
You can use array_filter to only keep the values that are not Null
.
Try like this.
array_filter($array);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this
$b = array_filter($a, function($k) { return $k!=null; });
Working example here.
add a comment |
Try this
$b = array_filter($a, function($k) { return $k!=null; });
Working example here.
add a comment |
Try this
$b = array_filter($a, function($k) { return $k!=null; });
Working example here.
Try this
$b = array_filter($a, function($k) { return $k!=null; });
Working example here.
answered Nov 21 '18 at 14:53
Kamil Kiełczewski
8,89585788
8,89585788
add a comment |
add a comment |
You can use array_filter to only keep the values that are not Null
.
Try like this.
array_filter($array);
add a comment |
You can use array_filter to only keep the values that are not Null
.
Try like this.
array_filter($array);
add a comment |
You can use array_filter to only keep the values that are not Null
.
Try like this.
array_filter($array);
You can use array_filter to only keep the values that are not Null
.
Try like this.
array_filter($array);
answered Nov 21 '18 at 14:52
Igor Carvalho
234213
234213
add a comment |
add a comment |
You want to do so in a loop or simply remove null from the array and loop the clean array?
– Diogo Santo
Nov 21 '18 at 14:48
1
Or
array_filter
, for that matter.– Federico klez Culloca
Nov 21 '18 at 14:49
@DiogoSanto I want to pass only the field that has a value
– Patweb
Nov 21 '18 at 14:51
That why I asked the above question
– Diogo Santo
Nov 21 '18 at 14:51
1
Is it just me, or does it seem like this question is just someone's course work?
– Difster
Nov 21 '18 at 14:53