How can I merge this array or remove a key - php

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); }

Related

Serialize/unserialize an encapsulated Array of SimpleXML-Elements

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

PHP array to take out array value only one field

I have an array where i want only the one Field text from all array which is text only. i want all text from there.
stdClass Object
(
[language] => en
[textAngle] => 0
[orientation] => Up
[regions] => Array
(
[0] => stdClass Object
(
[boundingBox] => 81,63,1340,1055
[lines] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,855,117
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,174,94
[text] => Set
)
[1] => stdClass Object
(
[boundingBox] => 529,87,126,69
[text] => an
)
[2] => stdClass Object
(
[boundingBox] => 693,65,483,115
[text] => example.
)
)
)
[1] => stdClass Object
(
[boundingBox] => 218,182,1059,116
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 218,182,271,92
[text] => Treat
)
[1] => stdClass Object
(
[boundingBox] => 521,203,504,95
[text] => everyOne
)
[2] => stdClass Object
(
[boundingBox] => 1054,182,223,91
[text] => With
)
)
)
I want take out from here like [text]=>Set,[text]=>an,[text]=>example.
eg set an example.
Output should be eg. only like set an example
Given your example class above, I would try something like this.
$text = '';
foreach ($class->regions[0]->lines as $line){
foreach ($line->words as $word){
$text = $text." ".$word->text;
}
}
print $text;

How to get the name and values from xml array using php

Here is my Xml code:
SimpleXMLElement Object
(
[resultsRows] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => date
[value] => 20140102
[label] => Date
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => browserType
[value] => Chrome
[label] => Browser Type
)
)
)
[metric] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visitDuration
[value] => 1242
[label] => Avg. Visit Duration
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => bounces
[value] => 3
[label] => Bounces
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => repeatVisitors
[value] => 0
[label] => Repeat Visitors
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => newVisitors
[value] => 5
[label] => New Visitors
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visits
[value] => 10
[label] => Visits
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => pageViews
[value] => 66
[label] => Page Views
)
)
)
)
)
)
)
Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside #attributes nested array like key and value.
Thanks.
Found in the comments on php SimpleXml documentation:
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
Check the docs here: http://cl1.php.net/ref.simplexml

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;
}

Get value from JSON object

stdClass Object
(
[form] => stdClass Object
(
[fieldsets] => Array
(
[0] => stdClass Object
(
[fieldset_name] => name_1
[datapoints] => Array
(
[0] => stdClass Object
(
[dp_id] => 4
)
[1] => stdClass Object
(
[dp_id] => 5
)
)
)
[1] => stdClass Object
(
[fieldset_name] => name_2
[datapoints] => Array
(
[0] => stdClass Object
(
[dp_id] => 1
)
[1] => stdClass Object
(
[dp_id] => 3
)
)
)
)
)
)
Now i want the values of dp_id seperately into two arrays ie.,array(4,5) and array(1,3)
foreach($object->form->fieldsets as $fieldset) {
$dp_ids = array();
foreach($fieldset->datapoints as $datapoint)
$dp_ids[] = $datapoint->dp_id;
var_dump($dp_ids);
}
try this.
$object->form['fieldsets'][1]->datapoints[1]

Categories