Getting first Array in variable data (a number of arrays) - php

The output of an object I am getting is:
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)
... and so on repeated.
I just want first array from these all arrays. Please note this is not a multidimentional array. .
I want only first array from the output :
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)

Related

i have an array with 8 values in it i want to print 4 in each row dynamically

this is the array
Array (
[0] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[1] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[2] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[3] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[4] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[5] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[6] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[7] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
)
Since you haven't given very much information about your problem, it's not easy to give a 'correct' answer. But i think you can go with this:
x = your array
foreach(array_chunk($x, 4) AS $chunk){
foreach($chunk AS $value){
echo $value->catcount.' - ';
}
echo "<br />";
}
thx to #smartpal for the array_chunk idea

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

XML xpath attribute value result to contain other results

I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/#attr|Parent/x
It grab the parent attr's value and all of its x's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first

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