rohitbanerjee Posted August 13, 2012 Share Posted August 13, 2012 Hello, I am having some issues with 2d arrays in PHP. $1d_arr = array('a', 'b', 'c'); foreach($1d_arr as $elem){ $elem = array(); } $sql = 'SELECT letter, num FROM table'; $result = $db->query($sql); while($row=mysql_fetch_array($result)){ array_push($1d_arr[$row['letter']], $row['num']); } This seems to be working but each array_push call is throwing a warning that the first argument has to be an array but null is being passed. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 What do want to end up with? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 13, 2012 Share Posted August 13, 2012 Did you see the result before to pass it to the array_push ? You need to iterate it in the loop : while($row=mysql_fetch_array($result)){ echo '<pre'.print_r($1d_arr, true).'</pre>'; exit; array_push($1d_arr[$row['letter']], $row['num']); } Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 Iteration will be very short with that exit statement in there. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 13, 2012 Share Posted August 13, 2012 Iteration will be very short with that exit statement in there. I just want to show him the current result of $1d_arr. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 13, 2012 Share Posted August 13, 2012 This bit does not do what you think it does: foreach($1d_arr as $elem){ $elem = array(); } Try a var_dump ($1d_arr) afterwards, and you'll see what I mean. Now, what I gather you're trying to do is to get a 2-d array where the letters are the keys and the value the index. For that, something like the following code is all you need: $sql = 'SELECT letter, num FROM table'; $result = $db->query ($sql); $combos = array (); while ($row = mysql_fetch_array ($result)) { $combos[$row['letter']] = $row['num']; } This will automatically create the array using the data from the database, creating a one-dimensional array with the correct key->value pairs. If you want to restrain it to a few letters, on the other hand, then use the WHERE IN() function in the MySQL query. You can read more about how to use it in the MySQL manual. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 14, 2012 Share Posted August 14, 2012 As ChristianF was saying that foreach loop is not doing what you think it is. It is only creating/resetting a variable called $elem on each ioteration which has nothing to do with the $1d_arr array. But, you are making it more difficult than it needs to be. Can the query result have values that are not in the list 'a', 'b', 'c' AND must you have a result in $1d_arr for each of those? If not, just create the output array dynamically when processing the results. Second, since you are only adding one value at a time, using $array[] = $value is more efficient: $sql = 'SELECT letter, num FROM table'; $result = $db->query($sql); $1d_arr = array(); while($row=mysql_fetch_array($result)) { $1d_arr[$row['letter']][] = $row['num']; } However, if you must have a dimension in the array for all those primary keys and the result from the query may not have values for each one, then you could define each element as an array to begin with: $1d_arr = array('a'=>array(), 'b'=>array(), 'c'=>array()); Or if you just want to define the keys as values in an array and then create the two-dimensional array programmatically you can do this $keys = array('a', 'b', 'c'); $1d_arr = array_fill_keys ($keys, array()); Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 This part should read "1-d array", sorry about the typo. ...get a 2-d array where the letters... Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 14, 2012 Share Posted August 14, 2012 Yes guys, that's exactly I wanted to say to him, but..... my expression in English is not like yours Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.