Jump to content


Photo

Appending Multidimensional Arrays


  • Please log in to reply
6 replies to this topic

#1 agravayne

agravayne
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 09 October 2006 - 02:09 PM

Hello,

I have searched for this exstensively but cannot work out what I am doing wrong here. I am trying to build a multidimensional array from the results of a query. Everything worlks fine except the array_push line - i cannot see what I am doing wrong or if indeed what I am doing is possible as I can only see examples for 2D arrays.

Here is my code.

$data=array();

$sql = "SELECT `dictionary_id`, `dictionary_value` FROM `helpline`.`dictionary_values` `dictionary_values` WHERE ( ( `dictionary_id` = 21 AND `deleted` = 0 ) ) ";

$rs=mysql_query($sql,$conn);

while( $row = mysql_fetch_array($rs) )

{
$sourcename=($row["dictionary_value"]);
array_push($data, $sourcename=>array("cases"=>"0","inds"=>"0"));

}

print_r($data);

The sql and everything works fine -  the array push line is just does not work.

Is this possible and if not this way how?

Many thanks
Scott

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 October 2006 - 02:15 PM

Change
array_push($data, $sourcename=>array("cases"=>"0","inds"=>"0"));
to
$sourcename = array_merge($source_name,array("cases"=>"0","inds"=>"0"));
array_push($data, $sourcename);


#3 agravayne

agravayne
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 09 October 2006 - 02:30 PM

Hello,

Thanks for such a quick reply - however this did not work. Perhaps I should explain what I am trying to acheive.

I have a list of Places that people are referred to us from. The SQL query gets the full list of these from the database. I then want to create a array with a lline for each like so...

Source 1, cases = 0, inds=0
Source 2,  cases = 0, inds=0

and so on. I have more code later on that will change the values of cases and inds for each line. The sql assigns the variable $sourcename for each result. I simply wnat to add a new line in the array for each result $sourcename, cases=0, inds-0.

Your replacement to code seem to override the sourcename variable anwyay.

The result of the print_r($data) is

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => )

I am still lost.

Scott

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 09 October 2006 - 02:37 PM

Try this:

while($row = mysql_fetch_array($rs)){
   $sourcename = $row['dictionary_value'];
   $data[$sourcename] = array('cases' => "0", 'inds' => "0"));
}

Note: Tested now, so should work I think.

You can test the desired result with
var_dump($data);


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 agravayne

agravayne
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 09 October 2006 - 02:51 PM

Hello HuggieBear,

This does work but quite how I want it this is the reulst of Print_r

Array ( [0] => Array ( [ASU Croydon] => Array ( [cases] => 1 [inds] => 1 ) ) [1] => Array ( [Benefits Agency] => Array ( [cases] => 2 [inds] => 2 ) ) [2] => Array ( [C.A.B] => Array ( [cases] => 3 [inds] => 3 ) ) [3] => Array ( [Felixstowe EU] => Array ( [cases] => 4 [inds] => 4 ) ) [4] => Array ( [Hampshire/Dorset] => Array ( [cases] => 5 [inds] => 5 ) ) [5] => Array ( [IMM Bedford] => Array ( [cases] => 6 [inds] => 6 ) ) [6] => Array ( [IMM Harwich] => Array ( [cases] => 7 [inds] => 7 ) ) [7] => Array ( [IMM KIA] => Array ( [cases] => 8 [inds] => 8 ) ) [8] => Array ( [IMM Norwich] => Array ( [cases] => 9 [inds] => 9 ) ) [9] => Array ( [Imm-Ashford] => Array ( [cases] => 10 [inds] => 10 ) ) [10] => Array ( [Imm-Dover East] => Array ( [cases] => 11 [inds] => 11 ) ) [11] => Array ( [Imm-Folkestone / Cheriton] => Array ( [cases] => 12 [inds] => 12 ) ) [12] => Array ( [IMM-Gatwick] => Array ( [cases] => 13 [inds] => 13 ) ) [13] => Array ( [IMM-Liverpool] => Array ( [cases] => 14 [inds] => 14 ) ) [14] => Array ( [Imm-Other] => Array ( [cases] => 15 [inds] => 15 ) ) [15] => Array ( [NASS Oakington] => Array ( [cases] => 16 [inds] => 16 ) ) [16] => Array ( [Newhaven] => Array ( [cases] => 17 [inds] => 17 ) ) [17] => Array ( [Other] => Array ( [cases] => 18 [inds] => 18 ) ) [18] => Array ( [Other Oss] => Array ( [cases] => 19 [inds] => 19 ) ) [19] => Array ( [Oxted] => Array ( [cases] => 20 [inds] => 20 ) ) [20] => Array ( [RAP] => Array ( [cases] => 21 [inds] => 21 ) ) [21] => Array ( [Refugee Council] => Array ( [cases] => 22 [inds] => 22 ) ) [22] => Array ( [S E P S T] => Array ( [cases] => 23 [inds] => 23 ) ) [23] => Array ( [Self] => Array ( [cases] => 24 [inds] => 24 ) ) [24] => Array ( [Social Services] => Array ( [cases] => 25 [inds] => 25 ) ) [25] => Array ( [ST Ives EU] => Array ( [cases] => 26 [inds] => 26 ) ) [26] => Array ( [Stanstead EU] => Array ( [cases] => 27 [inds] => 27 ) ) [27] => Array ( [Surrey] => Array ( [cases] => 28 [inds] => 28 ) ) [28] => Array ( [Sussex In-Country] => Array ( [cases] => 29 [inds] => 29 ) ) [29] => Array ( [Welsh Refugee Council] => Array ( [cases] => 30 [inds] => 30 ) ) [30] => Array ( [Y&H] => Array ( [cases] => 31 [inds] => 31 ) ) )

As you can see this create a numeric key for each of the elements so to get the number of cases for the first source I would have to refer to like so

$result=$data[0]["ASU Croydon"]["cases"];

but I need to be able to refer to it by the name - in this case ASU Croydon - in otherword the sourcename needs to be the key.

Scott



#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 09 October 2006 - 03:11 PM

You posted before I'd amended my code.  Try copying and pasting again.

I changed this:
$data[] = array($sourcename => array('cases' => "0", 'inds' => "0"));
To this:
$data[$sourcename] = array('cases' => "0", 'inds' => "0"));

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 agravayne

agravayne
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 09 October 2006 - 03:21 PM

Many many thanks

Obvious now!

This helps a lot.

Cheers





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users