我有一个带有空键值的多维数组。如何在多维数组php中将null替换为空字符串?我的数组是
"result": [
{
"id": "1",
"first_name": "Kishan",
"last_name": "Patel",
"profile_picture": null,
"email": "imkishanpatel107@gmail.com",
"birthdate": "1992-07-10",
"gender": "male",
"nickname": "KK",
"town": null,
"state": "58",
"bio": "123",
"zipcode": "395006",
"radius": "12356",
"favroiteteam": "2",
"latitude": "1.25456",
"longitude": "4.5658787",
"message_enable": "0",
"ratting_enable": "0",
"add_friend_enable": "0",
"interested_in": null,
"user_sport": [
{
"sport": {
"id": "3",
"name": "Basketball",
"image": "",
"no_of_player": "9",
"is_active": "1"
},
"sportPosition": {
"id": "1",
"sport_id": "3",
"position_name": "Point Guard"
},
"skill_level": {
"id": "1",
"name": "Novice"
}
},
{
"sport": {
"id": "2",
"name": "Baseball",
"image": "",
"no_of_player": "10",
"is_active": "1"
},
"sportPosition": {
"id": "6",
"sport_id": "2",
"position_name": "Pitcher"
},
"skill_level": {
"id": "3",
"name": "Advanced"
}
},
{
"sport": {
"id": "8",
"name": "Roller Hockey",
"image": "",
"no_of_player": "0",
"is_active": "1"
},
"sportPosition": {
"id": "15",
"sport_id": "8",
"position_name": "Forward"
},
"skill_level": {
"id": "1",
"name": "Novice"
}
}
]
}
]
答案 0 :(得分:0)
@Kishan Kikani你不能将null替换为'',因为两者都认为是空值,在数组的输出中,在null的位置会有空值或''你需要指定一些东西来替换你的值如上所示,你可以做到两种方式(你和我的):
<?php
$result = array("result"=>
array(
"id"=> "1",
"first_name"=> "Kishan",
"last_name"=> "Patel",
"profile_picture"=> null,
"email"=> "imkishanpatel107@gmail.com",
"birthdate"=> "1992-07-10",
"gender"=> "male",
"nickname"=> "KK",
"town"=> null,
"state"=> "58",
"bio"=> "123",
"zipcode"=> "395006",
"radius"=> "12356",
"favroiteteam"=> "2",
"latitude"=> "1.25456",
"longitude"=> "4.5658787",
"message_enable"=> "0",
"ratting_enable"=> "0",
"add_friend_enable"=> "0",
"interested_in"=> null,
"user_sport"=>
array(
"sport"=> array(
"id"=> "3",
"name"=> "Basketball",
"image"=> "",
"no_of_player"=> "9",
"is_active"=> "1"
),
"sportPosition"=> array(
"id"=> "1",
"sport_id"=> "3",
"position_name"=> "Point Guard"
),
"skill_level"=> array(
"id"=> "1",
"name"=> "Novice"
)
),
array(
"sport"=> array(
"id"=> "2",
"name"=> "Baseball",
"image"=> "",
"no_of_player"=> "10",
"is_active"=> "1"
),
"sportPosition"=> array(
"id"=> "6",
"sport_id"=> "2",
"position_name"=> "Pitcher"
),
"skill_level"=> array(
"id"=> "3",
"name"=> "Advanced"
)
),
array(
"sport"=> array(
"id"=> "8",
"name"=> "Roller Hockey",
"image"=> "",
"no_of_player"=> "0",
"is_active"=> "1"
),
"sportPosition"=> array(
"id"=> "15",
"sport_id"=> "8",
"position_name"=> "Forward"
),
"skill_level"=> array(
"id"=> "1",
"name"=> "Novice"
)
)
)
);
echo "<pre>";
print_r($result); // BEFORE
foreach($result as $rsKey => $rs){
foreach($rs as $key => $value){
if(is_null($value)){
$result[$rsKey][$key] = "";
}
}
}
echo "<pre>";
print_r($result); // after replace only null to "" but there will be a empty space in the values
foreach($result as $rsKey => $rs){
foreach($rs as $key => $value){
if(is_array($value)){
foreach ($value as $k => $v) {
foreach ($v as $k1 => $v1) {
if(empty($v1)){
$result[$rsKey][$key][$k][$k1] = "level2";
}
}
}
}
else{
if(is_null($value)){
$result[$rsKey][$key] = "level1";
}
}
}
}
echo "<pre>";
print_r($result); // after replace whole array empty values
尝试任何你想要的(y)
答案 1 :(得分:0)
$arr = array(
"key1"=>"value1",
"key2"=>null,
"key3"=>array(
"subkey1"=>null,
"subkey2"=>"subvalue2"),
"key4"=>null);
echo json_encode(replace_null_with_empty_string($arr));
function replace_null_with_empty_string($array)
{
foreach ($array as $key => $value)
{
if(is_array($value))
$array[$key] = replace_null_with_empty_string($value);
else
{
if (is_null($value))
$array[$key] = "";
}
}
return $array;
}
答案 2 :(得分:0)
Pi带@Atish的答案...我清理了它并简化了它。这将遍历数组并更改值引用(&),然后使用递归遍历数组的每个下一维。 @Daidon的答案只循环到第一个维度。
新功能看起来像这样...
function null2empty($array){
foreach ($array as &$value) {
$value = (is_array($value)) ? null2empty($value) : $value;
$value = (is_null($value)) ? "" : $value;
}
return $array;
}
要使用它,您会这样称呼它...
$profile_array = null2empty($profile_array);
答案 3 :(得分:-1)
只需放置一个循环并检查是否为foreach
foreach ($array as $key => $value) {
if (is_null($value)) $array[$key] = "";
}