2017-02-17 8 views
-4

nullキー値を持つ多次元配列が1つあります。多次元配列PHPでnullを空の文字列に置き換えるには?私の配列は多次元配列のnullを空の文字列値に置き換えます。

"result": [ 
    { 
     "id": "1", 
     "first_name": "Kishan", 
     "last_name": "Patel", 
     "profile_picture": null, 
     "email": "[email protected]", 
     "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

あなただけの場合の条件を記述し、この配列を使用している間... – Sona

答えて

-1

単に両方があなたの出力であることとして空の値を考慮しているため、あなたが「」にヌルを置き換えることはできませんループを入れて、foreachの

foreach ($array as $key => $value) { 
    if (is_null($value)) $array[$key] = ""; 
} 
0

@Kishan Kikaniとヌルをかどうかを確認です配列あなたはあなたが行うことができます両方の方法(あなたと私)は以下のように上記の配列にあなたの価値を置き換えるために何かを割り当てる必要があり「」がヌルの代わりに空の値であるかになります。

<?php 
$result = array("result"=> 
       array(
        "id"=> "1", 
        "first_name"=> "Kishan", 
        "last_name"=> "Patel", 
        "profile_picture"=> null, 
        "email"=> "[email protected]", 
        "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 

試してみますあなたが望む任意の1(Y)

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; 
    } 
+0

だけ答えとしてコードをダンプ回避し、それがないと、なぜかを説明しようとしてみてください。関連するコーディング経験を持っていない人にとっては、あなたのコードは明らかではないかもしれません。 [解明、文脈を含めるようにあなたの答えを編集して、あなたの答えに何らかの制限、前提条件、簡素化について言及してください。](https://stackoverflow.com/help/how-to-answer) –

関連する問題