Jump to content

Recommended Posts

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"]);
[color=red]array_push($data, $sourcename=>array("cases"=>"0","inds"=>"0"));[/color]

}

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
Link to comment
https://forums.phpfreaks.com/topic/23429-appending-multidimensional-arrays/
Share on other sites

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
Try this:

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

[size=8pt][color=red][b]Note:[/b][/color] Tested now, so should work I think.[/size]

You can test the desired result with [code=php:0]var_dump($data);[/code]


Regards
Huggie
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

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

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

Regards
Huggie
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.