0

I'm working with an search indexer and get my search result from the indexer as an array A that contains multiple objects. These objects were special objects generated from the indexer and cannot be used for output purposes. Is there a way to convert it into an array $B that contains standard PHP ojbects? Thanks!

Array A:

Array ( [0] => XSDocument Object ( [_data:XSDocument:private] => Array ( [id] => 65 [message] => cool book awesome [username] => lost guy [book] => my love....
[1] => XSDocument Object ( [_data:XSDocument:private] => Array ( [id] => 78 [message] => cool book awesome [username] => lost guy [book] => my love....)

Thanks a lot!

user3820799
  • 13
  • 1
  • 4
  • There is missing line in foreach appending to array B, isn't it? – marian0 Sep 14 '14 at 00:38
  • You mean 'endforeach'? I added it in earlier and it didn't make a difference somehow either, so I just left it out fornow – user3820799 Sep 14 '14 at 00:42
  • 1
    What is `$data_temp`? And where does the data get from there to `$B`? Post the full code regarding this. – Charlotte Dunois Sep 14 '14 at 00:43
  • No, I mean that you read array A and put data to variable `$data_temp` and you do nothing with it after. – marian0 Sep 14 '14 at 00:45
  • That was a typo. It whould have been $B not $data_temp. Updated – user3820799 Sep 14 '14 at 00:48
  • `foreach ($A as $row) { $B[] = array('username' => $row->username, 'book' => $row->book, 'message' => $row->message); }` – Sean Sep 14 '14 at 00:50
  • @Sean thx and I guess that would make $B an array that contains arrays? A way to make $B an array that only contains strings? – user3820799 Sep 14 '14 at 01:03
  • So you want `$B` to an array of strings. And what would the format look like? For example -> `$B[] = "'username' => $row->username, book => $row->book, 'message' => $row->message";` – Sean Sep 14 '14 at 01:07
  • I think that was very close to what I was looking for. At the end I need to be able to display the content from $B with foreach loop – user3820799 Sep 14 '14 at 01:10
  • After further investigation, it turned out I would actually need to convert it to an array B that contains multiple standard PHP objects. Apologies both for the confusion. – user3820799 Sep 14 '14 at 01:27
  • So you want something more like `$B = (object)$A`? see http://stackoverflow.com/a/6384474/689579 or http://php.net/manual/en/language.types.object.php#language.types.object.casting – Sean Sep 14 '14 at 05:41

1 Answers1

0

You overwrite data in each loop, so you have to write to $B like to array.

$B = array();

foreach ($A as $row) {
    $tmp = array();
    $tmp['username'] =  $row->username;
    $tmp['book'] =  $row->book;
    $tmp['message'] =  $row->message;

    $B[] = $tmp;
}
marian0
  • 3,336
  • 3
  • 27
  • 37
  • Um with it I was able to convert $A from an array that contains multiple objects to $B as an array that contains multiple arrays. My goal, however, was to see if it's possible to make $B an array that contains strings. Any advice? Thx! – user3820799 Sep 14 '14 at 01:02
  • Could you provide an example of your goal? Because this one from question looks exactly like an array. – marian0 Sep 14 '14 at 01:05
  • I'm looking for an example right now. At the end I just need to be able to display the content from $B with a foreach loop – user3820799 Sep 14 '14 at 01:11
  • After further investigation, it turned out I would actually need to convert it to an array B that contains multiple standard PHP objects. Apologies @marian0 – user3820799 Sep 14 '14 at 01:27