0

I have the following code:

    <?php
    $attr_data = array();

foreach ($content->search as $attributeSelected)
    {

    $attr_data[] = $attributeSelected['id'];
    }
    ?>

This saves the data as:

Array ( 
        [0] => SimpleXMLElement Object ( [0] => 23914175_laptop )
        [1] => SimpleXMLElement Object ( [0] => 23914175_laptop )
        [2] => SimpleXMLElement Object ( [0] => price_range_10_50004 ) 
) 

However I just want the array data and not to include the "SimpleXMLElement Object". So I added the following code:

    <?php
    $attr_data = array();

foreach ($content->search as $attributeSelected)
    {    
    $attr_data[] = json_decode(json_encode($attributeSelected['id']), true);
    }
    ?>

Which now gives me the data as:

Array (
        [0] => Array ( [0] => 23914175_laptop ) 
        [1] => Array ( [0] => 23914175_laptop )
        [2] => Array ( [0] => price_range_10_50004 ) 
) 

I'm not sure what I'm doing wrong. I just want to save the data to an array and then use an If statement to check if the array contains specific data.

  • you are saving an array into array – Gopal Dec 31 '13 at 13:09
  • 1
    `$attributeSelected['id']->0` – revo Dec 31 '13 at 13:09
  • Just do `$attr_data = json_decode(json_encode($attributeSelected['id']), true);` (you don't need the `[]` here). – Amal Murali Dec 31 '13 at 13:11
  • If that code did not turn the results to what you want, why did you use it then in the first place? Perhaps the problem is not clearly formulated in your question? – hakre Dec 31 '13 at 13:11
  • @AmalMurali I have tried that however this code is within a foreach statement and only saves the last piece of data to the array. – user3134976 Dec 31 '13 at 13:13
  • 2
    @user3134976: You didn't state that in your question. Please include all the **relevant** code in the question. – Amal Murali Dec 31 '13 at 13:14
  • Also recommended reading: [Forcing a SimpleXML Object to a string, regardless of context](http://stackoverflow.com/q/416548/367456) - This is most likely what you wonder about. Next to that, checkout SimpleXML, it offers all you need to select your data with xpath much easier than by array: http://php.net/simplexml.examples-basic – hakre Dec 31 '13 at 13:15
  • 2
    @user3134976: One way to prevent a not-so-clear question is to create a small, self-containing example from scratch that demonstrates your issue. That will not only make you to reduce the problem to the bare minimum (already helping to understand an issue your own), but also will provide you much clearer answers as everybody can easily reproduce it. – hakre Dec 31 '13 at 13:17
  • @AmalMurali added now. – user3134976 Dec 31 '13 at 13:20
  • 1
    And do not forget: Format your code properly. Good code starts with proper indenting. Give what you do some love, it helps :) -- Also still: You write in your question you don't want to have the `SimpleXMLElement Object` but then you showed that you already found your solution to have it not in there. So a question with your question remains: What is your question? What you wrote is solved in its own, so not really a question. – hakre Dec 31 '13 at 13:23

4 Answers4

2

How about:

$attr_data[] = $attributeSelected['id'][0];
user14764
  • 748
  • 1
  • 9
  • 19
0

You are trying to store an object, if you need the first value of object you should change the third line to:

$attr_data[] = $attributeSelected['id']->0;
revo
  • 47,783
  • 14
  • 74
  • 117
0

Please add a snippet of your XML, it makes things a lot clearer.

typecast the values to string:

$xml = simplexml_load_string($x); // assume XML in $x
$att = array();

foreach ($xml->search as $a) {
    $att[] = (string)$a['id'];
}

see it working: http://codepad.viper-7.com/P0DI0S

michi
  • 6,565
  • 4
  • 33
  • 56
0

The class SimpleXMLElement has a __toString method, so you could call it directly (which is kinda ugly IMO) or just typecast it to a string:

<?php
$attr_data = array();

foreach ($content->search as $attributeSelected)
{
    $attr_data[] = (string) $attributeSelected['id'];
    // Or
    $attr_data[] = $attributeSelected['id']->__toString();
}
?>

Read more at http://php.net/manual/en/simplexmlelement.tostring.php

Daniel Pereira
  • 327
  • 2
  • 8