1

I have some values that grab off an API and store them into an array $data for insertion into my tables. However, I seem to have objects within the array $data, which seems kind of messy. Is it possible to convert all the contents of array $data into array elements?

PHP Code

$data['title'] = $item->ItemAttributes->Title;
$data['brand'] = $item->ItemAttributes->Brand;
$data['color'] = $item->ItemAttributes->Color;
echo "<pre>";
print_r($data);

Output

stdClass Object
(
    [title] => stdClass Object
        (
            [0] => La Martina Polo Shirt Scotland Polo, Color: Black, Size: L
        )

    [brand] => stdClass Object
        (
            [0] => La Martina
        )

    [color] => stdClass Object
        (
            [0] => Black
        )

)
8
  • And what is $data ? Was it initialized as an array ? Can you recreate the problem on codepad.viper-7.com ? Commented Jul 14, 2012 at 14:10
  • $data is just an empty array. Commented Jul 14, 2012 at 14:11
  • As you might notices in this example, what you have there is not an array. Commented Jul 14, 2012 at 14:15
  • Somehow it became an array. I did not initialize it as an array though, maybe thats why when I pushed an object into $data using $data[] = $obj, $data becomes an array Commented Jul 14, 2012 at 14:24
  • 1
    No, $item is not an array, because you are calling $item->ItemAttributes->Title;. Commented Jul 14, 2012 at 14:46

3 Answers 3

1
array get_object_vars ( object $object )

for more : http://php.net/manual/en/function.get-object-vars.php

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

Comments

1
$array = get_object_vars($object);

If the members are protected or private, do it from inside the class.

If you aren't going to define a specific class, use reflection.

Comments

0

I dont get you properly but if you do something like this

$data['title'] =(string)$item->ItemAttributes->Title;
$data['brand'] =(string)$item->ItemAttributes->Brand;
$data['color'] = (string)$item->ItemAttributes->Color;

.. might lead you to something like below:-

{'title'=>'some title','brand'=>,'color'=>'something'}

But I am not sure if that is what you want

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.