AndroidPlayer2
AndroidPlayer2

Reputation: 346

If element is null, it would not be added to the array with the keyword array(), how?

In PHP, I have the following function:

 public function GenerateJSONOutOfArray_M4($mRecord) 
        {
            try
            {
                $mRecordLength = sizeof($mRecord);            

                $R = array();
                for($i = 0; $i < $mRecordLength; $i++)
                {
                    $R[$i] = 
                    array (
                    'a' => $mRecord[$i]['M4_M2'],
                    'b' => $mRecord[$i]['M4_M3'],
                    'c' => $mRecord[$i]['M4_Barcode'],
                    );
                }

                $result =
                array (
                  'J' => 
                        $R
                );

                $json = json_encode($result);

                return $json;                
            }
            catch (Exception $e)
            {
                return FALSE;
            }
        }                          

I need if $mRecord[$i]['M4_M2'] is null then 'a' would not be added to the array
In other word, I mean array() does not include 'a' if the value for 'a' is null
How?

Upvotes: 0

Views: 46

Answers (3)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38542

There are many ways to do that, One way to do it this way with multiple if along with isset condition to check null,

$array = [];
for($i = 0; $i < $mRecordLength; $i++)
{
    if(isset($mRecord[$i]['M4_M2'])){
        $array['a'] = $mRecord[$i]['M4_M2'];
    }
    if(isset($mRecord[$i]['M4_M3'])){
        $array['b'] = $mRecord[$i]['M4_M3'];
    }
    if(isset($mRecord[$i]['M4_Barcode'])){
        $array['c'] = $mRecord[$i]['M4_Barcode'];
    }
    $R[$i] =$array;
}

Upvotes: 2

Umair Khan
Umair Khan

Reputation: 1752

Something like this. You need to use isset if you want to check if property / variable / index exists and not null. You can use simple if but it'll evaluate on truthy / falsy values.

public function GenerateJSONOutOfArray_M4($mRecord)
{
    try {
        $mRecordLength = sizeof($mRecord);

        $R = array();
        for ($i = 0; $i < $mRecordLength; $i++) {
            $R[$i] = [];
            if (isset($mRecord[$i]['M4_M2']))
                $R[$i]['a'] = $mRecord[$i]['M4_M2'];
            if (isset($mRecord[$i]['M4_M3']))
                $R[$i]['b'] = $mRecord[$i]['M4_M3'];
            if (isset($mRecord[$i]['M4_Barcode']))
                $R[$i]['c'] = $mRecord[$i]['M4_Barcode'];
        }

        $result = array('J' => $R);

        $json = json_encode($result);

        return $json;
    } catch (Exception $e) {
        return FALSE;
    }
}

Upvotes: 1

Michel
Michel

Reputation: 4157

Some possibilities:

delete afterwards:

{
$R[$i] = 
array (
  'a' => $mRecord[$i]['M4_M2'],
  'b' => $mRecord[$i]['M4_M3'],
  'c' => $mRecord[$i]['M4_Barcode'],
  );
//unset if NULL
if($R[$i]['a'] === NULL) unset($R[$i]['a']);
}

or

add afterwards:

{
$R[$i] = 
array (
  'b' => $mRecord[$i]['M4_M3'],
  'c' => $mRecord[$i]['M4_Barcode'],
  );
//add if not NULL
if($mRecord[$i]['M4_M2'] !== NULL) $R[$i]['a'] = $mRecord[$i]['M4_M2'];
}

Upvotes: 1

Related Questions