Jump to content

exploded array count issus


PHPGeek80

Recommended Posts

Hi,

 

I have the following php code and when i try to count the items of the expolded array it gives me a value of "1" even though the data im getting from the database is empty.

 

Does anyone know why this is?

 

$row = mysql_fetch_array($sql_result);

$field_description = $row["field_description"];
$field_cost = $row["field_cost"];
$field_setup = $row["field_setup"];

$A_field_description = array();
$A_field_cost = array();
$A_field_setup = array();

$A_field_description = explode(',', $field_description);
$A_field_cost = explode(',', $field_cost);
$A_field_setup = explode(',', $field_setup);

$A_field_count = count($A_field_description);

print $A_field_count;

Link to comment
Share on other sites

<?php

$row = mysql_fetch_assoc($sql_result);

$field_description = $row["field_description"];
$field_cost = $row["field_cost"];
$field_setup = $row["field_setup"];

$A_field_description = array();
$A_field_cost = array();
$A_field_setup = array();

$A_field_description = explode(' ', $field_description);
$A_field_cost = explode(' ', $field_cost);
$A_field_setup = explode(' ', $field_setup);

$A_field_count = count($A_field_description);

print $A_field_count;
?>

Link to comment
Share on other sites

Ummm, have you ever thought that he might be exploding comma delimited strings redarrow?

 

It is returning 1 because if explode() can't find the delimited in the string, it returns the original string (in an array). So explode() is returning an array with one key and an empty value. Hence the count of 1.

Link to comment
Share on other sites

A note: Since PHP is loosely typed, you don't need to define the objects/vars before you use them...

 

If your data is empty, $a = explode(',', $b); will return NULL (0) or the original string, then count($a) would infact count:

$a = array([0] => NULL [or original string]);

Which of course is 1 :P

Link to comment
Share on other sites

$row = mysql_fetch_assoc($sql_result);

$field_description = $row["field_description"];
$field_cost = $row["field_cost"];
$field_setup = $row["field_setup"];


$A_field_description = explode(' ', $field_description);
$A_field_cost = explode(' ', $field_cost);
$A_field_setup = explode(' ', $field_setup);

$A_field_count = count($A_field_description);

print $A_field_count;
?>

 

Use that instead. Should work.

 

 

 

When you do this:

$A_field_description = array();
$A_field_cost = array();
$A_field_setup = array();

 

You are converting those variables to empty arrays.

and then by exploding the empty array, you're giving it a key, but no value.

 

 

 

Link to comment
Share on other sites

func1

<?php
function func1($param){
  $var = Array(); // Init var
  $var = $param; // set var
  return $var;
}
?>

Above, there is no reason to init $var since the very first use of it is an assignment.

 

func2

<?php
function func2(){
  $var = explode(' ', $some_var);
  return $var;
}
?>

Above, func2 is taking for granted that $some_var has been set elsewhere in the code.  Who knows where it's been set and what it's been set to.  func2 is a good way to introduce an obscure bug into your code.

 

func2 - fixed

<?php
function func2(){
  $some_var = some_func();
  $var = explode(' ', $some_var);
  return $var;
?>

Now we know what $some_var is before using it which makes contamination less likely.  Better programming practice.

 

General guideline:  You don't need to initialize variables if the first use of them is assignment.  If the first use is anything other than assignment, you'll save yourself a headache somewhere down the line by taking the extra 1.5 seconds to type whichever of these fits your need:

$var = NULL;

$var = Array();

$var = '';

$var = "";

$var = false;

$var = true;

 

I have nothing more to say on this matter.  ;^]

Link to comment
Share on other sites

Hi,

 

Yes that is correct emehrkay.

 

I have tried it with the mysql_fetch_assoc feature but get the same result.

 

I used print_r on the exploded array and got:

 

Array ( [0] => )

 

If there is no data in the array why is it still giving a key?

 

I have also tried this without defining the arrays with the same result.

Link to comment
Share on other sites

It is returning 1 because if explode() can't find the delimited in the string, it returns the original string (in an array). So explode() is returning an array with one key and an empty value. Hence the count of 1.

 

Before you use explode, consider checking the length of the string, or if it even contains any ','

Link to comment
Share on other sites

Hi all,

 

Issue resolved.

 

<?php

$row = mysql_fetch_assoc($sql_result);

$field_description = $row["field_description"];
$field_cost = $row["field_cost"];
$field_setup = $row["field_setup"];

$A_field_count = 0;

if (!empty($field_description))
{
$A_field_description = array();
$A_field_cost = array();
$A_field_setup = array();

$A_field_description = explode(' ', $field_description);
$A_field_cost = explode(' ', $field_cost);
$A_field_setup = explode(' ', $field_setup);

$A_field_count = count($A_field_description);
}

print $A_field_count;
?>

 

Thanks for all you help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.