Asgardien Posted June 3, 2012 Share Posted June 3, 2012 Hi guys I have been tasked by my c.o to finnish a project and need some help. I have an array that is returned from a CURL reqest to one of the Reg servers. all i need to do is be able to search through the array and return the data. The example below is the same formate as the array i recieve data is fictional. any help will be appreciated. array ( 0 => array ( 'id' => '4024', 'town_name' => 'BIRKENHEAD', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1', ), 1 => array ( 'id' => '4023', 'town_name' => 'CHESTER', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1', ), 2 => array ( 'id' => '3859', 'town_name' => 'ELLESMERE PORT', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1', ), 3 => array ( 'id' => '4065', 'town_name' => 'FLINT', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1', ), Quote Link to comment Share on other sites More sharing options...
cpd Posted June 3, 2012 Share Posted June 3, 2012 foreach($items as $item) var_dump($item); Quote Link to comment Share on other sites More sharing options...
Barand Posted June 3, 2012 Share Posted June 3, 2012 <?php $heads = array_keys($data[0]); echo "<table border='1' cellpadding='2'>"; echo '<tr><th>' . join('</th><th>', $heads) . '</th></tr>'; foreach ($data as $array) { echo '<tr><td>' . join('</td><td>', $array) . '</td></tr>'; } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 3, 2012 Author Share Posted June 3, 2012 thanks for the quick replys, but i fear i asked the question incorrectly (too long in the field what i actually need to do is search through the array for a given value then return the whole value. ie some one searches for a town name and gets all entrys returned with that town name value. Sorry for the misleading first post. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 3, 2012 Share Posted June 3, 2012 put an if() in the loop <?php $searchfor = 'FLINT'; $heads = array_keys($data[0]); echo "<table border='1' cellpadding='2'>"; echo '<tr><th>' . join('</th><th>', $heads) . '</th></tr>'; foreach ($data as $array) { if ($array['town_name'] == $searchfor) echo '<tr><td>' . join('</td><td>', $array) . '</td></tr>'; } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 3, 2012 Author Share Posted June 3, 2012 thanks for the reply Barand. used yor code and recieved a "Warning: array_keys() expects parameter 1 to be array, string given in D:\wamp\www\~test2.php on line 19 Call Stack: 0.0013 329976 1. {main}() D:\wamp\www\~test2.php:0 0.2389 336912 2. array_keys() D:\wamp\www\~test2.php:19 Warning: join(): Invalid arguments passed in D:\wamp\www\~test2.php on line 21 Call Stack: 0.0013 329976 1. {main}() D:\wamp\www\~test2.php:0 0.2390 337008 2. join() D:\wamp\www\~test2.php:21 Warning: Invalid argument supplied for foreach() in D:\wamp\www\~test2.php on line 22 Call Stack: 0.0013 329976 1. {main}() D:\wamp\www\~test2.php:0 Any ideas ? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 3, 2012 Share Posted June 3, 2012 it is assumed that the $data variable I used contains your data array as posted Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 4, 2012 Author Share Posted June 4, 2012 thanks again for the reply, will have to put this on hold for 24 hrs no pc were im going for a bit Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 5, 2012 Author Share Posted June 5, 2012 Still getting error;s when i run the code, the Data variable does hold the array data, this may be just me having a boild brain, in the 30'sº today and i get sunburn when it goes over 18º Quote Link to comment Share on other sites More sharing options...
Barand Posted June 5, 2012 Share Posted June 5, 2012 here's my full code, including the test array <?php $data = array ( 0 => array ( 'id' => '4024', 'town_name' => 'BIRKENHEAD', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1' ), 1 => array ( 'id' => '4023', 'town_name' => 'CHESTER', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1' ), 2 => array ( 'id' => '3859', 'town_name' => 'ELLESMERE PORT', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1' ), 3 => array ( 'id' => '4065', 'town_name' => 'FLINT', 'house_number' => '1', 'address1' => 'Woodford Road', 'address2' => '', 'town' => 'BIRKENHEAD', 'county' => '', 'postcode' => 'CH62 1AZ', 'reference' => 'A7', 'active' => '1' ) ); $searchfor = 'FLINT'; $heads = array_keys($data[0]); echo "<table border='1' cellpadding='2'>"; echo '<tr><th>' . join('</th><th>', $heads) . '</th></tr>'; foreach ($data as $array) { if ($array['town_name'] == $searchfor) echo '<tr><td>' . join('</td><td>', $array) . '</td></tr>'; } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 6, 2012 Author Share Posted June 6, 2012 still getting the error below, have 2 days RR now so can crack on with this hopefully ( ! ) Warning: array_keys() expects parameter 1 to be array, string given in D:\wamp\www\php2B65.tmp on line 19 Call Stack # Time Memory Function Location 1 0.0007 367376 {main}( ) ..\php2B65.tmp:0 2 0.3063 374368 array_keys ( ) ..\php2B65.tmp:19 ( ! ) Warning: join() [function.join]: Invalid arguments passed in D:\wamp\www\php2B65.tmp on line 21 Call Stack # Time Memory Function Location 1 0.0007 367376 {main}( ) ..\php2B65.tmp:0 2 0.3065 374624 join ( ) ..\php2B65.tmp:21 ( ! ) Warning: Invalid argument supplied for foreach() in D:\wamp\www\php2B65.tmp on line 22 Call Stack # Time Memory Function Location 1 0.0007 367376 {main}( ) ..\php2B65.tmp:0 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 6, 2012 Share Posted June 6, 2012 check your array structure still matches that in your original post Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 6, 2012 Author Share Posted June 6, 2012 Barand thanks for all your help with this, the array has tha structur that i gave but there is more data ,"Fields"in the array than i posted there could be upto 27 fields in each array. just cant post them. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 6, 2012 Share Posted June 6, 2012 try replacing this line $heads = array_keys($data[0]); with this one (just in case your array doesn't start at zero) $heads = array_keys(current($data)); Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 6, 2012 Author Share Posted June 6, 2012 still brings back the same error. The array comes from a CURL call, would this make a difference ? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 6, 2012 Share Posted June 6, 2012 Having made the cURL call, how do you pass the returned array to my code? Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 7, 2012 Author Share Posted June 7, 2012 I ran the code you posted and it worked returning the required search value, So i am thinking it must be something to do with the way it is brought back via the Curl call thats is causing it to fail Quote Link to comment Share on other sites More sharing options...
Barand Posted June 7, 2012 Share Posted June 7, 2012 That's why I asked my last question Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 7, 2012 Author Share Posted June 7, 2012 sorry about that, i pass the curl request so : $data = curl_exec ($ch); Quote Link to comment Share on other sites More sharing options...
Barand Posted June 7, 2012 Share Posted June 7, 2012 after $data = curl_exec ($ch); put var_export($data); and see what results. Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 7, 2012 Author Share Posted June 7, 2012 error below, just to clarify , just a point i close the curl call after all the code 'array ( 0 => array ( \'id\' => \'4024\', \'town_name\' => \'BIRKENHEAD\', \'active\' => \'1\', ), 1 => array ( \'id\' => \'4023\', \'town_name\' => \'CHESTER\', \'active\' => \'1\', ), 2 => array ( \'id\' => \'3859\', \'town_name\' => \'ELLESMERE PORT\', \'active\' => \'1\', ), 3 => array ( \'id\' => \'4065\', \'town_name\' => \'FLINT\', \'active\' => \'1\', ), 4 => array ( \'id\' => \'3961\', \'town_name\' => \'LIVERPOOL\', \'active\' => \'1\', ), 5 => array ( \'id\' => \'3962\', \'town_name\' => \'MOLD\', \'active\' => \'1\', ), 6 => array ( \'id\' => \'3958\', \'town_name\' => \'ST HELENS\', \'active\' => \'1\', ), )' ( ! ) Warning: array_keys() expects parameter 1 to be array, string given in D:\wamp\www\php8462.tmp on line 34 Call Stack # Time Memory Function Location 1 0.0005 366168 {main}( ) ..\php8462.tmp:0 2 0.1254 373096 array_keys ( ) ..\php8462.tmp:34 ( ! ) Warning: join() [function.join]: Invalid arguments passed in D:\wamp\www\php8462.tmp on line 36 Call Stack # Time Memory Function Location 1 0.0005 366168 {main}( ) ..\php8462.tmp:0 2 0.1255 373400 join ( ) ..\php8462.tmp:36 ( ! ) Warning: Invalid argument supplied for foreach() in D:\wamp\www\php8462.tmp on line 37 Call Stack # Time Memory Function Location 1 0.0005 366168 {main}( ) ..\php8462.tmp:0 PHP Warning: array_keys() expects parameter 1 to be array, string given in D:\wamp\www\php8462.tmp on line 34 PHP Stack trace: PHP 1. {main}() D:\wamp\www\php8462.tmp:0 PHP 2. array_keys() D:\wamp\www\php8462.tmp:34 PHP Warning: join() [function.join]: Invalid arguments passed in D:\wamp\www\php8462.tmp on line 36 PHP Stack trace: PHP 1. {main}() D:\wamp\www\php8462.tmp:0 PHP 2. join() D:\wamp\www\php8462.tmp:36 PHP Warning: Invalid argument supplied for foreach() in D:\wamp\www\php8462.tmp on line 37 PHP Stack trace: PHP 1. {main}() D:\wamp\www\php8462.tmp:0 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 7, 2012 Share Posted June 7, 2012 is the array definition returned as a string? try $d = curl_exec ($ch); $d = "\$data=".$d.';' ; eval($d); followed by my code Quote Link to comment Share on other sites More sharing options...
Asgardien Posted June 7, 2012 Author Share Posted June 7, 2012 Your a Star Barand that worked. Thanks a lot for all the help and time. Quote Link to comment Share on other sites More sharing options...
alvin567 Posted June 8, 2012 Share Posted June 8, 2012 say I have a 5x5 array, how do i slot in a 4x4 array into position 5x5? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 I fail to see how that post makes any contribution whatsoever to the above thread. 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.