Jump to content

Recommended Posts

What I'm trying to do is create a summarized array from an unsummarized result set from a mysql query. I have a MySQL query that searches for linecards that match a given serial number association. From that sql query I create an array of results. Then I create a new array for purposes of summarizing the results.

Once I get my SQL resultset, I can then create an array using mysql_fetch_array. I prefer my values be indexed numerically, so I'm not using Mysql_assoc on the array.

$link6_array2 = mysql_fetch_array($link6_result2);
$link6_rows2 = mysql_num_rows($link6_result2);

echo "link6 rows is $link6_rows2"; /* outputs 3 */
echo "count link6 array2 " . count($link6_array2) . " "; /* outputs 2 */

!! Curiously, these output different numbers. Is that ok? I think array numbering starts at 0 so they could be considered identical, but I didn't know count() was sensative to that.


Ok, here's a snippit.

for ($i = 0; $i < $link6_rows2; $i++) { /* Number of results from DB query */

$link6_array2 = mysql_fetch_array($link6_result2); /* Create array from DB query */

if (!$bladearray) { /* if first run through loop, create array bladearray */

$vals = $link6_array2[$i]; /* inserted for troubleshooting. This makes the array() */
/* create syntax below prettier. Extracts numeric index 0 here */
/* echo $vals outputs: ALM */

$bladearray = array (
"$vals" => array("qty" => "1") /* since array didn't exist before, should */
/* set $bladearray[ALM][qty] = 1 */
);
//echo $bladearray[ALM][qty]; /* outputs 1 as it should */

} else {

$vals2 = $link6_array2[$i];

// echo $vals2; /* !!! Results in blank output. Nothing regurned??? Why??? */

<cut here >

This is I think the root of my problem. Once the loop starts over again, something goes haywire. My SQL query returns 3 rows. Each row contains one column. Each row is identical. The result set looks pretty simple:

mysql> select shortname from ixiaeval.blade2chs where (chs_sn = '400T-1234');
+-----------+
| shortname |
+-----------+
| ALM |
| ALM |
| ALM |
+-----------+
3 rows in set (0.00 sec)


So why do I not have a value in my next itteration of the for loop? I had one in the first itteration. I should have one in the second and index $i which is now 1.

This is killing me! Thanks for any insight.





<remainder of the loop if interested in using it>


if (array_key_exists($vals2, $bladearray)) { /* must be 2nd + time through */
/*since first !$bladearray failed */
/*so check if $lin6_ar2[1] exists */
/* if so, increment qty for key at */
/*index $l6_arr[ALM][qty] */
$bladearray[$link6_array2][$i][qty]++;

} else { /* create new array since previous key did not exist, must have been something*/
/* other than ALM for $link6_array2[1] */

$vals = $link6_array2[$i];
$bladearray_update = array ( /* create new array of same structure as above */

"$vals" => array("qty" => "1")
);

array_merge($bladearray, $bladearray_update); /* Merge new array onto existing array */
}

} /* exit loop and start all over with next array index */

Link to comment
https://forums.phpfreaks.com/topic/13223-cant-create-and-merge-an-array/
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.