resting
resting

Reputation: 17467

Store an array to an array key

I'm trying to store an array ($temp) into the $data array, where key is prices.

$data['prices'] = $temp;

However, PHP converts the array into string instead and is throwing me and error.
Notice: Array to string conversion

Is $data = array('prices' => $temp); the only solution?

edit:
I found my mistake. Yes, $data was used previously as a string that's why PHP is converting the input into string.
Problem 2, doing a print_r on $data['prices'] = $xml->result->Prices->Price, shows only 1 set of array. But I am able to retrive 2 sets of result by doing a foreach loop on $data['prices']. Why is that so?

Content of $temp http://pastebin.com/ZrmnKUPB

Let me be more clear..
The full xml object I'm trying to extract information from: http://pastebin.com/AuMJiyrw
I'm only interested in the price array (Price_strCode and Price_strDescription) and store them in $data['prices']. End result something like this:

Array(
    [0] => (
        [Price_strCode] => 0001
        [Price_strDescription] => Gold
    )
    [1] => (
        [Price_strCode] => 0002
        [Price_strDescription] => Silver
    )
)

Upvotes: 1

Views: 248

Answers (2)

foxy
foxy

Reputation: 7672

Unless you are doing some other array to string conversion elsewhere, the array is actually being stored as another array.

$data['prices'] will be an array, which can be accessed as $data['prices']['key'].

Upvotes: 2

Umair Jabbar
Umair Jabbar

Reputation: 3666

This is not possible, I have been doing this always and it works fine. You must be doing something else somewhere which is causing your array to convert to strong. share your code here

Upvotes: 0

Related Questions