2

Hey folks, please lend a hand to a PHP beginner. I'm trying to put a load of dynamically created variabled into an array to re-read later, reason is a SOAP message sent is a mess and im trying to create a less complicated array:

$placea = "place a";
$placeb = "place b";

$myarray = array();

echo "<pre>";
print_r($myarray);
echo "</pre>";

what i want to be able to do:

Array
(
    [0] => [Place A] => Array
        (
            [0] => [Accommodation] => Array
                (
                    [RoomId] => 001
                    [RoomAvail] => true
                    [Date] => 12.04.2011

                )
            [1] => [Accommodation] => Array
                (
                    [RoomId] => 002
                    [RoomAvail] => true
                    [Date] => 12.04.2011

                )

        )    Array
(
    [1] => [Place B] => Array
        (
            [0] => [Accommodation] => Array
                (
                    [RoomId] => 101
                    [RoomAvail] => true
                    [Date] => 12.04.2011

                )
            [1] => [Accommodation] => Array
                (
                    [RoomId] => 102
                    [RoomAvail] => true
                    [Date] => 12.04.2011

                )

        )


)

how would i write that out in php? sorry if its bleek and/or the array structure is incorrect

3
  • what's the data look like that you want to go in the array? Commented Jan 23, 2011 at 3:53
  • do a var_dump(soap_response); to see the data Commented Jan 23, 2011 at 3:55
  • would love to, but its over 15000 lines. would grab a snippet. but an answer is given below. :) thanks Commented Jan 23, 2011 at 4:05

3 Answers 3

3

So you just need to use the array initializer repetitively.

If you want to initialize an array in PHP with some values, say 1 through 4, you make a call like:

$foo = array(1, 2, 3, 4);

And if you want to make an associative array, where you store some key/value pairs, you make a call like:

$foo = array('key' => 'value', 'other key' => 'other value');

But you can of course nest calls, and mix and match layers of associative and non associative arrays to achieve something like your example, e.g.:

$foo = array( 
    'Place A' => array( 
        // note use of first array on the next line is
        // to generate structure like [0] => 'Accomodation' => ...
        array('Accomodation' => array( 
            'RoomId' => '001', 
            'RoomAvail' => true, 
            'Date' => '12.04.2011')
        )), 
        array('Accomodation' => array( 
            'RoomId' => '002', 
            'RoomAvail' => true, 
            'Date' => '12.04.2011')
        ))
    ),
    'Place B' => array( 
        array('Accomodation' => array( 
            'RoomId' => '101', 
            'RoomAvail' => true, 
            'Date' => '12.04.2011')
        )), 
        array('Accomodation' => array( 
            'RoomId' => '102', 
            'RoomAvail' => true, 
            'Date' => '12.04.2011')
        ))
    )
);

This will very nearly produce what you're looking for, to make it replicate exactly what you have you would wrap each 'Place A' with an array and each "place" would get its own assignment to some variable $foo (I assumed this wasn't actually what you wanted and wrote something maybe slightly more intuitive).

Sign up to request clarification or add additional context in comments.

1 Comment

this is what im after. i just didn't know how to repeatidly add stuff to an array. I kept getting illegal offset and other offset errors when trying to give each place an numeric array id.
0

If you want to have a 'less complicated' array, you have a two arrays, one fore place a and one for place b and then merge them using array_merger() http://www.php.net/manual/en/function.array-merge.php.

Study up on the array functions control structures in the manual. Many different ways of achieving bloated arrays uglying up your code.

1 Comment

agreed. the first array is mahoosive and data is all over the shop. im trying to set it into one array that i can keep in memory and get rid of the big one.
0

This would dynamically create an array.

foreach($soapResponse as $key1 => $value1){
    foreach($value as $key2 => $value2){
        // $key1 = Place A or B
        // value1 = array of values
        $arrayResponse[$key1][$key2] =  $value2;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.