1

How can I set array value to null if no data exist?

Following is my array from PHP and I am json encoding -

{  
   "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"
      }
   }
}
3
  • Can you post more of your code for creating the array. It seems like you probably need to put the = null bit before the while loop. You should have the business id stored somewhere. $business_details_array[$thisBusinessId]['milestone'][] = null; Commented Apr 28, 2016 at 6:06
  • I've added code in which I am fetching all business list with business_id and it is further filtering details according to it. Commented Apr 28, 2016 at 6:10
  • Topically related: Can I add a PHP array key without an assigned value in a class variable? Commented Feb 3, 2023 at 6:49

2 Answers 2

1

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;
        }      
    }   
Sign up to request clarification or add additional context in comments.

Comments

0

you could set a default initial array for a 'business' and set its values to 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
    );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.