Conditionally showing values in a PHP array [duplicate]












0















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,
]









share|improve this question













marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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




    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
















0















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,
]









share|improve this question













marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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




    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














0












0








0








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,
]









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:47









Patweb

716




716




marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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




    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


















  • 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
















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












2 Answers
2






active

oldest

votes


















0














Try this



$b = array_filter($a, function($k) { return $k!=null; });


Working example here.






share|improve this answer





























    1














    You can use array_filter to only keep the values that are not Null.
    Try like this.



    array_filter($array);





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try this



      $b = array_filter($a, function($k) { return $k!=null; });


      Working example here.






      share|improve this answer


























        0














        Try this



        $b = array_filter($a, function($k) { return $k!=null; });


        Working example here.






        share|improve this answer
























          0












          0








          0






          Try this



          $b = array_filter($a, function($k) { return $k!=null; });


          Working example here.






          share|improve this answer












          Try this



          $b = array_filter($a, function($k) { return $k!=null; });


          Working example here.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:53









          Kamil Kiełczewski

          8,89585788




          8,89585788

























              1














              You can use array_filter to only keep the values that are not Null.
              Try like this.



              array_filter($array);





              share|improve this answer


























                1














                You can use array_filter to only keep the values that are not Null.
                Try like this.



                array_filter($array);





                share|improve this answer
























                  1












                  1








                  1






                  You can use array_filter to only keep the values that are not Null.
                  Try like this.



                  array_filter($array);





                  share|improve this answer












                  You can use array_filter to only keep the values that are not Null.
                  Try like this.



                  array_filter($array);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 14:52









                  Igor Carvalho

                  234213




                  234213















                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'