Thursday, April 19, 2012

Is there a better way to iterate over this multi-dimensional array in PHP?

I am using the Solr search engine. It returns my search results like so:



array(2) {
["responseHeader"]=>
array(3) {
["status"]=>
int(0)
["QTime"]=>
int(1)
["params"]=>
array(5) {
["wt"]=>
string(3) "php"
["start"]=>
string(1) "0"
["q"]=>
string(7) "monitor"
["version"]=>
string(3) "2.2"
["rows"]=>
string(2) "10"
}
}
["response"]=>
array(3) {
["numFound"]=>
int(2)
["start"]=>
int(0)
["docs"]=>
array(2) {
[0]=>
array(11) {
["id"]=>
string(6) "VA902B"
["name"]=>
string(49) "ViewSonic VA902B - flat panel display - TFT - 19""
["manu"]=>
string(15) "ViewSonic Corp."
["weight"]=>
float(190.4)
["price"]=>
float(279.95)
["price_c"]=>
string(10) "279.95,USD"
["popularity"]=>
int(6)
["inStock"]=>
bool(true)
["store"]=>
string(18) "45.17614,-93.87341"
["cat"]=>
array(2) {
[0]=>
string(11) "electronics"
[1]=>
string(7) "monitor"
}
["features"]=>
array(1) {
[0]=>
string(75) "19" TFT active matrix LCD, 8ms response time, 1280 x 1024 native resolution"
}
}
[1]=>
array(12) {
["id"]=>
string(7) "3007WFP"
["name"]=>
string(34) "Dell Widescreen UltraSharp 3007WFP"
["manu"]=>
string(10) "Dell, Inc."
["includes"]=>
string(9) "USB cable"
["weight"]=>
float(401.6)
["price"]=>
float(2199)
["price_c"]=>
string(8) "2199,USD"
["popularity"]=>
int(6)
["inStock"]=>
bool(true)
["store"]=>
string(18) "43.17614,-90.57341"
["cat"]=>
array(2) {
[0]=>
string(11) "electronics"
[1]=>
string(7) "monitor"
}
["features"]=>
array(1) {
[0]=>
string(71) "30" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast"
}
}
}
}
}


I am capturing this response inside a variable called $results. On my page I am doing a vardump to get each result set like so:



foreach($results['response'] as $result) {
if(is_array($result)) {
foreach($result as $v) {
var_dump($v);
}
}
}


This is the output of that:



array(11) {
["id"]=>
string(6) "VA902B"
["name"]=>
string(49) "ViewSonic VA902B - flat panel display - TFT - 19""
["manu"]=>
string(15) "ViewSonic Corp."
["weight"]=>
float(190.4)
["price"]=>
float(279.95)
["price_c"]=>
string(10) "279.95,USD"
["popularity"]=>
int(6)
["inStock"]=>
bool(true)
["store"]=>
string(18) "45.17614,-93.87341"
["cat"]=>
array(2) {
[0]=>
string(11) "electronics"
[1]=>
string(7) "monitor"
}
["features"]=>
array(1) {
[0]=>
string(75) "19" TFT active matrix LCD, 8ms response time, 1280 x 1024 native resolution"
}
}

array(12) {
["id"]=>
string(7) "3007WFP"
["name"]=>
string(34) "Dell Widescreen UltraSharp 3007WFP"
["manu"]=>
string(10) "Dell, Inc."
["includes"]=>
string(9) "USB cable"
["weight"]=>
float(401.6)
["price"]=>
float(2199)
["price_c"]=>
string(8) "2199,USD"
["popularity"]=>
int(6)
["inStock"]=>
bool(true)
["store"]=>
string(18) "43.17614,-90.57341"
["cat"]=>
array(2) {
[0]=>
string(11) "electronics"
[1]=>
string(7) "monitor"
}
["features"]=>
array(1) {
[0]=>
string(71) "30" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast"
}
}


This is exactly what I need but I was wondering if there is a more elegent way to get it than my "nested foreach loop and is array check" approach.





Matching numbers

I'm trying to finish a homework assignment and I can't find this anywhere. I'm trying to match input numbers with randomly generated numbers and displaying how many of the 5 are correct(also with a win/lose message) This is what I have so far and any help would be appreciated. :)



#include <iostream>
#include <cstdlib>

using namespace std;



void main()
{
//variables
int lottery[5], user[5];
int count = 0;
int num1, num2, num3, num4, num5;
int winnum;
//generating numbers
for (int i=0; i <5; i++)
{
lottery[i] =1+rand()%9;
}
//input
cout << "Enter a digit between 0-9: ";
cin >> num1;
cout << "Enter a digit between 0-9: ";
cin >> num2;
cout << "Enter a digit between 0-9: ";
cin >> num3;
cout << "Enter a digit between 0-9: ";
cin >> num4;
cout << "Enter a digit between 0-9: ";
cin >> num5;


winnum = rand();

//for (int i=0; i<5; i++)
// cin >> user[i];

//display


cout << "Winning Lottery Numbers: " << winnum << endl;

cout << "Your ticket Numbers: " << num1 << num2 << num3 << num4 << num5 << endl;




//matching the numbers

//HELP!
system("pause");

}




Buffered versus unbuffered output for Python in Windows

I use NppExec/Notepad++ to run Python scripts, and flush my output buffers continuously during runtime to update my console window with print statements (The default buffered output only shows all print statements after the script finishes execution).



This link shows that all you need to do is use the command python -u to get unbuffered output. Is there a downside to using this execution mode for all my Python scripts, regardless of the editor used? I'm not clear about the difference between a buffered an unbuffered output.





Tcl Tk treeview with checkbuttons

Is is possible to add check button to ttk::treeview column.



Specifically I am trying to create a checklist for the user to hide or show items on the canvas when the checkbox is selected or deselected. Since there are a lot of canvas items with specific type and sub type I need a list box kind of mechanism.





Final Local Variable may not have been initialized in anonymous inner class

Here is my code:



final Foo myFoo = new Foo(new Inner() {
@Override
callback(){
myFoo.bar();
}
});


Java is giving me an error about how myFoo might not have been initialized. Is there any way to fix this? I can potentially set the callback to null when I construct the object and then change it afterwards, but I'd hope for there to be a cleaner way. Any ideas? (Also that wouldn't work if Foo wasn't written by me and didn't expose an interface to change the callback later)



In case anyone is curious, in my specific scenario, Foo is an ArrayAdapter, and the callback is notifyDataSetChanged(). What is displayed by the adapter depends on the values of the items in the array, and those values change when they are clicked on. The callback is the clickListener.





How to update for each records without looping in SQL Server

I have a table and contained data show below, it's called TABLE_A




+++++++++++++++++++++++++++++++++++++++++++++
PrimaryID | Col2 | SecondaryID
+++++++++++++++++++++++++++++++++++++++++++++
1 | Description 1 | 0
2 | Description 2 | 0
3 | Description 3 | 0
4 | Description 4 | 0
. | ... | .
. |
.


please see SecondaryID, above. its has zero value as an initial value



and I have another table, it's called TABLE_B, below




+++++++++++++++++++++++++++++++++++++++++++++
PrimaryID | Col2 | ForeignKeyID
+++++++++++++++++++++++++++++++++++++++++++++
1 | Description 1 | 123
2 | Description 2 | 320
3 | Description 3 | 111
4 | Description 4 | 999
. | ... | .
. |
.


I have trouble in SQL Server,
How to update SecondaryID column on TABLE_A with ForeignKeyID value on TABLE_B for each row in TABLE_A's PrimaryID is equal TABLES_B's PrimaryID.
But, I don't want to solve this problem using LOOPING CURSORS or another else.



Are there a simple way??



I need urgent, and thank you in advanced.





How to get jQuery dialog to wait before displaying?

I'd like my jQuery dialog to display exactly 3 seconds after a user hovers over an image. Currently I have:



$(".imgLi").live('hover', function() {
showDialog();
});

function showDialog()
{
$('#imageDialogDiv').dialog({
modal:true
});
}

<div id="imageDialogDiv" title="Blah">...</div>


Not sure where too put the time code or how to implement jQuery's timer object here. If the use "mouses away" (moves the mouse away from the image) at any point during that 3 second timeframe, I do not want the dialog to display. Thanks in advance for any help here.