Jump to content

multidimensional Array help


Asgardien

Recommended Posts

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',

  ),

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
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.