Jump to content
Larry Ullman's Book Forums

Recommended Posts

$array2=array('name'=>array('laith','raad','hamid'),'age'=>array(20,30,40),'gender'=>'male');

print_r($array2);

echo $c2=count($array2);

foreach($array2 as $k=>$v)


if($array2 < $c2)

{

foreach($array2 as $k=>$v)

{ echo $k . ' ' . $v . "<br/>"; }

}

echo $k . ' ' . $v . "<br/>"; }

 

// i have a problem 

//NOTICE: array to string conversion on the last line

 

// i need solution ????????????????????????????????? :wub: 
Link to comment
Share on other sites

A bigger problem is that your code kind of doesn't make any sense. Initial thoughts...

 

- Your $array2 design isn't appropriate. You should create each person as a separate subarray, with "name", "age", and "gender" indexes for each.

- You echo the assignment of count() to $c2. This is not the same as printing the value of $c2.

- count() only gives you the number of items in the main array, so it'll be 3 in your situation. If you add two more names and ages (the way you have it), the count will still be 3.

- Your IF conditional checks of a multidimensional array is less than the count of that array. This doesn't work or make sense.

- You do two foreach loops on the same array, which is unnecessary and buggy.

- You print out $k and $v twice, which is unnecessary and buggy.

 

I gather you're just learning, but the NOTICE is the least of the problems. You need to rethink the logic of what you're trying to do here.

Link to comment
Share on other sites

I think this would be a better start, wouldn't you, Antonio?

 

$persons = array(
      array("name" => "Laith", "age" => 20, "gender" => "male"), // person 1
      array("name" => "Raad", "age" => 30, "gender" => "male"), // person 2
      array("name" => "Hamid", "age" => 40, "gender" => "male"), // person 3
); 

 

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...