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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...