Bill Tudor Bill Tudor - 4 months ago 7
PHP Question

Creating a 2D array moves data to key position

I declare the following array

$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);


I expect it to be created like

array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),

[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")


But when I print the array it looks like

Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )

[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )

[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )


I would like the array to be created in the same format as the second section of code.

Answer

All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this

$job_scope = array( "proposal_id" => array(),
                "will_provide" => array("0","Supervision","Labor","Material","Equpment"),          
                "general_scope" => array(),          
                "per_bid" => array("Yes","No","Omit"),               
                "job_type"  => array("Painting","Sandblasting","Scappling")
                );

It will produce this array

Array ( 
   [proposal_id] => Array ( ) 
   [will_provide] => Array ( [0] => 0 
                             [1] => Supervision 
                             [2] => Labor 
                             [3] => Material 
                             [4] => Equpment 
                           )
   [general_scope] => Array ( ) 
   [per_bid] => Array ( [0] => Yes 
                        [1] => No 
                        [2] => Omit 
                      ) 
   [job_type] => Array ( [0] => Painting 
                         [1] => Sandblasting 
                         [2] => Scappling 
                       ))

If you want to callback the value, (ex : call supervision value).

All you need to do is

print_r($job_scope['will_provide'][1])

and that will print the supervision value