Serialize/unserialize an encapsulated Array of SimpleXML-Elements - php

I have from an API-request an encapsulated Array like this:
Array
(
[1] => SimpleXMLElement Object
(
[id] => 1
[link_rewrite] => fashion-supplier
[name] => Fashion Supplier
[active] => 1
[date_add] => 2018-01-18 13:47:30
[date_upd] => 2018-01-18 13:47:30
[description] => SimpleXMLElement Object
(
[language] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
[meta_title] => SimpleXMLElement Object
....
This data I want to store in the Laravel-Cache. Because of the Simple-XML-Elements inside it is not possible to store the raw data ("Serialization of 'SimpleXMLElement' is not allowed").
Is there a way to convert this complex array-simplexml-object-combination in a storable form? The problem is, I need the XML-Attributes inside, that's why I can't use json_decode(json_encode($array));
Thank you for your help!
Alex

Related

How can I merge this array or remove a key

Below is an example of the array I have compiled so far. I am generating the array from a facebook graph api call and want to remove the Array wrapping each object so I just have one list under the data Array. Preferably I need a dynamic solution as their could be more than one [0] => Array, [1] => Array and so on.... in each API request.
stdClass Object
(
[data] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
)
I would like the array to look like this ultimately. What is the best approach here?
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
[1] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
Assuming your data is stored in a variable $data, you can do this:
foreach($data->data as &$el) {
$el = $el[0];
}
Now the wrapping arrays have been removed.
See it run on eval.in
You can use this:
if ($data && $data->data){ $new = array_values($data->data); }

Getting last child in SimpleXMLElement Object with PHP

again I think I'm missing something really basic here, but I really can't figure it out.
I'm parsing an xml file formatted as excel, using SimpleXML in PHP.
My xml file looks like the following:
SimpleXMLElement Object (
[DocumentProperties] => SimpleXMLElement Object (
[Author] => Surname Name
[LastAuthor] => Name Surname
[Created] => 2016-08-01T10:15:25Z
[Version] => 14.00 )
[OfficeDocumentSettings] => SimpleXMLElement Object ( [AllowPNG] => SimpleXMLElement Object ( ) )
[ExcelWorkbook] => SimpleXMLElement Object ( [WindowHeight] => 9375 [WindowWidth] => 9705 [WindowTopX] => 270 [WindowTopY] => 615 [ProtectStructure] => False [ProtectWindows] => False )
[Styles] => SimpleXMLElement Object ( [Style] => SimpleXMLElement Object ( [Alignment] => SimpleXMLElement Object ( ) [Font] => SimpleXMLElement Object ( ) ) )
[Worksheet] => Array (
[0] => SimpleXMLElement Object (
[Table] => SimpleXMLElement Object (
[Column] => Array (
[0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( ) )
[Row] => Array (
[0] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => Model )
[1] => SimpleXMLElement Object ( [Data] => Qty )
[2] => SimpleXMLElement Object ( [Data] => Description )
[3] => SimpleXMLElement Object ( [Data] => Tags ) ) )
[1] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => model_name )
[1] => SimpleXMLElement Object ( [Data] => 1 )
[2] => SimpleXMLElement Object ( [Data] => This is my item ) ) )
[....code repeats with other rows/worksheets....]
Now, if I want to display the Author name, I can simply do something like:
echo $xml->DocumentProperties->LastAuthor;
and I will get the expected result. How do I get to display the values in the various rows/cells (keeping in mind that the xml files will have a very diverse number of rows and rows are not always completely filled)?
I've tried something like the following:
echo $xml->Worksheet[0]->Table->Row[0]->Cell[0];
But doing so doesn't return any value. I would also like to be able to cycle through all the rows so that I can display them as an HTML table.
Any ideas?
Thanks a lot in advance for your help!

PHP Accessing And Searching a multidimensional array

I have the following array:
Array (
[0] => stdClass Object (
[#attributes] => stdClass Object (
[Id] => 0 [MapId] => 1 [Name] => [LocationId] => 0 [MapRelativeX] => 0.58813826735513464 [MapRelativeY] => 0.5223214285714286
)
)
[1] => stdClass Object ( [#attributes] => stdClass Object ( [Id] => 1 [MapId] => 1 [Name] => GL D [LocationId] => 1 [MapRelativeX] => 0.54053714859437729 [MapRelativeY] => 0.43601190476190477 ) ) [2] => stdClass Object ( [#attributes] => stdClass Object ( [Id] => 2 [MapId] => 1 [Name] => GL E [LocationId] => 1 [MapRelativeX] => 0.458028542742398 [MapRelativeY] => 0.5223214285714286 ) )
I am trying to access and search through this array. For instance I am trying to access the ID key/value pair but I can't. I have the following code.
$row->Id doesn't return anything where $row is $fullArray[0]
Also is there a way to search this array for the row with the ID=2?
Thanks
figured it out:
$row[0]->attributes()->Id
:)

Sort stdClass Object after an array

I have two arrays,
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => art
)
[1] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[2] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
Array
(
[2] => 2
[1] => 1
)
And I want to sort the first array after the second array. In the second array the key and the value is equal with the first array id. So the output have to be the following.
Array
(
[0] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[1] => stdClass Object
(
[id] => 1
[title] => art
)
[2] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
You can use array_multisort[Docs] for it:
array_multisort($arraySort, $arrayData);
Pass the array with the sort order as the first and your array to be sorted as the second parameter.
You might need to build the sort array prior to it, from your question it's not clear to me if you already have it or not.
In case not, if you want to get all of the data arrays entries ID values into the sort array:
$arraySort = array();
foreach($arrayData as $key => $obj)
{
$arraySort[$key] = $obj->id;
}

SimpleXml problems accessing xml nodes

i have serious problems accessing nodes of a specific xml file:
http://write.fm/cqsmrf5
with print_r i get the following result:
SimpleXMLElement Object
(
[RecordSet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[dataSource] => testdatabase
[totalRecordCount] => 3573
)
[Record] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[counter] => 1
)
[Fields] => SimpleXMLElement Object
(
[Field] => Array
(
[0] => Barcelona
[1] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[counter] => 2
)
[Fields] => SimpleXMLElement Object
(
[Field] => Array
(
[0] => Cádiz
[1] => 2
)
)
)
)
)
I tried to access through the command
print $xml->recordset->record[0]->fields->field[0];
But i only get the error:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/test.php on line 18
Could anyone help me out.
Thanks in advance.
XML is case-sensitive. Try
$xml->RecordSet->Record[0]->Fields->Field[0]; // Barcelona

Categories