简体   繁体   English

PHP数组设置空值

[英]PHP array set null value

How can I set array value to null if no data exist?如果不存在数据,如何将数组值设置为null

Following is my array from PHP and I am json encoding -以下是我来自 PHP 的数组,我是 json 编码 -

{  
   "title":"Impalz-Marketing",
   "type":"Business Details",
   "version":"1.0",
   "login":[  ],
   "business":{  
      "1":{  
         "details":{  },
         "messages":[  ],
         "offers":[  ],
         "events":[  ],
         "milestone":[  ],
         "products":[  ],
         "brand_exp":[  ],
         "reviews":[  ],
         "agg_reviews":{  }
      },
      "168":{  
         "details":{  },
         "messages":[  ],
         "products":[  ]
      }
   }
}

Number of rows are uneven in both business.两个业务的行数不均匀。 How can I set data to null if no row exist?如果不存在行,如何将数据设置为空?

$data = array(
        'title' => 'Impalz-Marketing',
        'type' => 'Business Details',
        'version' => '1.0',
        'login' => $login_array,
        'business' => $business_details_array
);

I've tried this:我试过这个:

$business_details_array = array();
while($row = mysql_fetch_assoc($biz_list))
{
    $business_details_array[$row['id']]['details'] = $row;
}

    while($row = mysql_fetch_assoc($biz_milestone))
    {
        if(!empty($row['id'])){
            $temp= explode(',', $row['path']);
            if(count($temp) > 1) {
                $row['path']= $temp;
            }
            $business_details_array[$row['business_id']]['milestone'][] = $row;
        }else{  
            $business_details_array[]['milestone'][] = null; // since no data exist their wont be any business_id
        }       
    }    

I want something like this -我想要这样的东西 -

{  
   "title":"Impalz-Marketing",
   "type":"Business Details",
   "version":"1.0",
   "login":[  ],
   "business":{  
      "1":{  
         "details":{  },
         "messages":[  ],
         "offers":[  ],
         "events":[  ],
         "milestone":[  ],
         "products":[  ],
         "brand_exp":[  ],
         "reviews":[  ],
         "agg_reviews":{  }
      },
      "168":{  
         "details":{  },
         "messages":[  ],
         "offers": "null",
         "events": "null",
         "milestone": "null",
         "products":[  ],
         "brand_exp": "null",
         "reviews": "null",
         "agg_reviews": "null"
      }
   }
}

Add nulls to your initial array creation将空值添加到您的初始数组创建中

$business_details_array = array();
while($row = mysql_fetch_assoc($biz_list))
{
    $business_details_array[$row['id']]['details'] = $row;
    $business_details_array[$row['id']]['milestone'] = null;
    $business_details_array[$row['id']]['products'] = null;
    $business_details_array[$row['id']]['messages'] = null;
}

    while($row = mysql_fetch_assoc($biz_milestone))
    {
        if(!empty($row['id'])){
            $temp= explode(',', $row['path']);
            if(count($temp) > 1) {
                $row['path']= $temp;
            }
            $business_details_array[$row['business_id']]['milestone'][] = $row;
        }      
    }   

you could set a default initial array for a 'business' and set its values to NULL.您可以为“业务”设置默认初始数组并将其值设置为 NULL。 and then merge the array from the DB into the default one.然后将数据库中的数组合并到默认数组中。

    $defaultBusinessVals = array(
        "details" => NULL,
        "messages" => NULL,
        "offers" => NULL,
        "events" => NULL,
        "milestone" => NULL,
        "products" => NULL,
        "brand_exp" => NULL,
        "reviews" => NULL,
        "agg_reviews" => NULL
    );

If the DB array is this:如果数据库数组是这样的:

    $dbArr = array(
        "details" => "no details",
        "messages" => "my msg",
        "offers" => 3,
        );

and then merge the data from the DB然后合并数据库中的数据

    $res = $dbArr + $defaultBusinessVals;

or或者

    $res = array_merge($defaultBusinessVals, $dbArr);

The result will be结果将是

    $res = array(
        "details" => "no details",
        "messages" => "my msg",
        "offers" => 3,
        "events" => NULL,
        "milestone" => NULL,
        "products" => NULL,
        "brand_exp" => NULL,
        "reviews" => NULL,
        "agg_reviews" => NULL
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM