Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267038-array_push-with-2d-array/
Share on other sites

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']);
}

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.

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());

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.