Jump to content



Photo

PHP Multidimensional Arrays


  • Please log in to reply
8 replies to this topic

#1 Craig Hopson

Craig Hopson

    Neowinian

  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 20 February 2013 - 20:15

hi guys i have a simple one ui cant solve i want an array to be like this

Array (
[0] => Array (
[id] => 2
[lat] => 40.8824996948
[long] => -74.381942749
)
[1] => Array (
[id] => 6
[lat] => 40.8824996948
[long] => -74.381942749
)
[2] => Array (
[id] => 4
[lat] => 51.5001525879
[long] => -0.126236006618 )
)
)

This is the code i'm using which gives me multipuls of arrays (as it's looping)
include('modules/fab/ip2location.class.php');
  $ip = new ip2location;
  $ip->open('modules/fab/databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-SAMPLE.BIN');
  $uid = $client['id'];
  $result = sql_query("SELECT * FROM `jcow_accounts` where ipaddress != '' order by lastlogin desc ");
  $i = 0;
  while($row = mysql_fetch_array($result)) {
  
   $record  = $ip->getAll($row['ipaddress']);
   $id  = $row['id'];
   $lat  = $record->latitude;
    $long  = $record->longitude;
 
   $fab[] = array(
	   'id'=>$id,
	   'lat'=>$lat,
	   'long'=>$long,
	 );
  }

Thanks


#2 +Audien

Audien

    Software Eng.

  • Joined: 30-December 03
  • Location: Seattle, WA
  • OS: Windows 8.1/Mac OSX
  • Phone: iPhone 5S

Posted 20 February 2013 - 20:23

What's the current output? You also should initialize $fab = array().

#3 OP Craig Hopson

Craig Hopson

    Neowinian

  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 20 February 2013 - 20:25

forgive me for not formatting it but this is wat i get

Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) [3] => Array ( [id] => 5 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) [3] => Array ( [id] => 5 [lat] => 40.8824996948 [long] => -74.381942749 ) [4] => Array ( [id] => 3 [lat] => 51.5001525879 [long] => -0.126236006618 ) )

#4 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: Samsung Galaxy SIII, iPhone 4s

Posted 20 February 2013 - 20:40

Formatted example,

#
# ITERATION 1
#

Array (
[0] => Array (
[id] => 2
[lat] => 40.8824996948
[long] => -74.381942749
)
)

#
# ITERATION 2 (which includes iteration 1)
#

Array (
[0] => Array (
[id] => 2
[lat] => 40.8824996948
[long] => -74.381942749
)
[1] => Array (
[id] => 6
[lat] => 40.8824996948
[long] => -74.381942749
)
)


#
# QUESTION
#

Are you printing the contents of the array while within the while loop?

#5 Jose_49

Jose_49

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 30-July 09

Posted 20 February 2013 - 20:45

forgive me for not formatting it but this is wat i get

Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) [3] => Array ( [id] => 5 [lat] => 40.8824996948 [long] => -74.381942749 ) ) Array ( [0] => Array ( [id] => 2 [lat] => 40.8824996948 [long] => -74.381942749 ) [1] => Array ( [id] => 6 [lat] => 40.8824996948 [long] => -74.381942749 ) [2] => Array ( [id] => 4 [lat] => 51.5001525879 [long] => -0.126236006618 ) [3] => Array ( [id] => 5 [lat] => 40.8824996948 [long] => -74.381942749 ) [4] => Array ( [id] => 3 [lat] => 51.5001525879 [long] => -0.126236006618 ) )

I want to share with you an amazing trick I've learned for printing out the array:
Add this:
print('<pre>')
print_r($put_the_variable_name_you_want_to_display);
print('</pre>')

#6 OP Craig Hopson

Craig Hopson

    Neowinian

  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 20 February 2013 - 20:46

my whole script is above i've just started the project basicly i will have about 100 entrys in the array like this

Array (
[0] => Array (
[id] => 2
[lat] => 40.8824996948
[long] => -74.381942749
)
[1] => Array (
[id] => 6
[lat] => 40.8824996948
[long] => -74.381942749
)
[2] => Array (
[id] => 4
[lat] => 51.5001525879
[long] => -0.126236006618 )
)
)

#7 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: Samsung Galaxy SIII, iPhone 4s

Posted 20 February 2013 - 20:48

my whole script is above i've just started the project basicly i will have about 100 entrys in the array like this

Array (
[0] => Array (
[id] => 2
[lat] => 40.8824996948
[long] => -74.381942749
)
[1] => Array (
[id] => 6
[lat] => 40.8824996948
[long] => -74.381942749
)
[2] => Array (
[id] => 4
[lat] => 51.5001525879
[long] => -0.126236006618 )
)
)


Ok! But the code appears to be doing this outside of the fact with each iteration it is including results from the previous, which is what would happen if you are dumping the contents of the array WHILE within the `while` loop.

#8 OP Craig Hopson

Craig Hopson

    Neowinian

  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 20 February 2013 - 20:50

sorry my bad i had the

echo '<pre>';
echo print_r($fab);
echo '</pre>';

in the while {}

SORRY :s

#9 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: Samsung Galaxy SIII, iPhone 4s

Posted 20 February 2013 - 20:54

sorry my bad i had the

echo '<pre>';
echo print_r($fab);
echo '</pre>';

in the while {}

SORRY :s


Glad to have been of help ;)