Looping through array and create a new array of query results PHP - php

I have a PHP array object that can contain zero or more values like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
)
)
I want to loop through each value in this array and use the id in a query that returns an object that looks like this:
Array
(
[0] => stdClass Object
(
[id] => taWPlKGXHR5Y03cc
[title] => Test Document Title
[filename] => test.docx
)
)
On each iteration of the loop the query returns with one result in the form of an array object. I want to add the object to an array object that in this case would look something like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
[title] => Test Document Title 0
[filename] => test0.docx
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
[title] => Test Document Title 1
[filename] => test1.docx
)
)
The query is written and working and I know I need to use a foreach loop to iterate over the array of IDs, but I don't quite get how to set it up so that the end result is an array object as listed just above. I'm using PHP & Codeigniter to do all of this.
The code of the foreach I have so far is something like this:
$child = array();
foreach ($id as $row) {
$child = $this->users_model->get_docnfo($id);
}
Thanks for reading!

You should try it with
$child = array();
foreach ($id as $row) {
$child[] = $this->users_model->get_docnfo($row->id);
}
Note the $row->id instead of $id and also the brackets after $child.

Related

simplexml_load_file - foreach shows one time loop

I have a table structure like this, I need to swing her foreach loop to extract all 848 elements. The problem is that the loop displays only one item instead of the actual amount sitting there. With the way with regular forem and idid substitution works, although writing the same thing is in my opinion longer and uglier.
Array structure:
SimpleXMLElement Object
(
[#attributes] => Array
(
[language] => pol
[currency] => PLN
)
[product] => Array
(
[0] => SimpleXMLElement Object
[#attributes] => Array
(
[id] => 8
)
[1] => SimpleXMLElement Object
[#attributes] => Array
(
[id] => 4
)
[etc...]
)
I try this way
$chairXML = simplexml_load_file('toparser.xml');
foreach ($chairXML->children() as $children) {
echo $children->product['id'];
}
But this doesn't work, loop return one record.
$xml = '
<xml>
<product>
<cherry>1</cherry>
<apple>3</apple>
<orange>4</orange>
</product>
</xml>';
$simpleXmlObj = simplexml_load_string($xml);
foreach ($simpleXmlObj->product->children() as $children) {
print_r($children);
}
result
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 3
)
SimpleXMLElement Object
(
[0] => 4
)
I think your problem in $simpleXmlObj->children()
Try $simpleXmlObj->product->children()

Getting data with Facebook Graph Api

I am trying to get all the facebook group members ids with facebook php sdk, I am getting an array, and can't get out of the array with only ids.
Here is my array:
Array
(
[data] => Array
(
[0] => stdClass Object
(
[name] => Name Surname
[administrator] =>
[id] => 655330041265756
)
[1] => stdClass Object
(
[name] => Name Surname2
[administrator] =>
[id] => 10206563909924840
)
[2] => stdClass Object
(
[name] => Name Surname3
[administrator] =>
[id] => 1035931373098451
)
I am trying to export only [id], how is this possible?
Just a simple foreach can be used to iterate through the array and extract the id from each object.
foreach ($array as $key=>$value) {
echo $value->id;
}
Edit: If you want to assign the ids to an array, use the following code:
foreach ($array as $key=>$value) {
$idArray[] = $value->id;
}
Where $idArray is your array of ids.

Getting content from two stdObject array

[responseheader] => Object (
Array (
[0] => Object (
[id] => id_1
[name] => abc
)
[1] => Object (
[id] => id_2
[name] => xyz
)
)
)
[response] => Object (
[id_1] => Object (
[content] => Array (
[0] => content_1
)
)
[id_2] => Object (
[content] => Array (
[0] => content_2
)
)
)
Both the above 2 objects, responseheader and response are under one object (header).
In the above structure, the order is for response will be same as it is in responseheader.(ie, id_2 will always come after id_1)
I want to get the content from response for each id present in responseheader. I will be iterating responseheader object.
I could loop through response and progressively add another property (say dummy) inside responseheader storing the content, but is there any better, faster approach?
This should do the trick.
$result = array();
//loop through responseheader array
foreach($data['responseheader'] as $row)
{
//if the id exists in the response array add it to the result array
if(array_key_exists($row['id'], $data['response'])) {
$result[] = $data['response'][$row['id']]['content'][0];
}
}
print_r($result);
If the content in response could contain more than one content you have to loop throught it to:
foreach($data['response'][$row['id']]['content'] as $content) {
$result[] = $content;
}

Manually creating an associative array

I'm trying to create an associative array with dynamic data, and having some trouble.
I'd like to produce an array that looks like the following while fetching rows from a MySQL query.
Array
(
[0] = Array
(
[name] => First
)
[1] = Array
(
[name] => Second
)
[2] = Array
(
[name] => Third
)
[3] = Array
(
[name] => Fourth
)
[4] = Array
(
[name] => Fifth
)
)
I've been trying to use array_merge, but it's not giving me the result I want. Array_merge apparently doesn't operate the same inside a foreach as it does outside (I ran the same code with and without the loop, without worked the way I need).
Basically, this is what I'm doing currently (which doesn't work):
foreach($idList as $id)
{
$arr[] = array_merge(array(), array('name' => $id));
}
This gives me output like this:
Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[2] = Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[name] => third
)
)
You've got a few issues here.
Mainly, you can't have the same index twice. 'name' can be the index once and only once, so you're 'desired' output is impossible.
Also, this statement is pretty problematic
foreach($idList as $id)
{
$arr[] = array_merge(array(), array('name' => $id));
}
The use of $arr[] = $x is like a push. It adds a new element to the back of the array, numerically indexed.
Your use of array_merge is unnecessary. array_merge returns the second argument merged over the first argument. You are just trying to add a single new element. Also, is that exactly the line you used or did you use array_merge($arr, array('name' => $id)); ???
Try:
foreach($idList as $id)
{
$arr[] = array('name' => $id);
}
And you will get:
Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[name] => second
}
....
And so on. I'm not sure if this is exactly what you want, but what you proposed in the first place isn't possible.

Convert indexed array of objects to an associative array of objects using a column as the new first-level keys

$var is an array:
Array (
[0] => stdClass Object ( [ID] => 113 [title] => text )
[1] => stdClass Object ( [ID] => 114 [title] => text text text )
[2] => stdClass Object ( [ID] => 115 [title] => text text )
[3] => stdClass Object ( [ID] => 116 [title] => text )
)
Want to update it in two steps:
Get [ID] of each object and throw its value to position counter (I mean [0], [1], [2], [3])
Remove [ID] after throwing
Finally, updated array ($new_var) should look like:
Array (
[113] => stdClass Object ( [title] => text )
[114] => stdClass Object ( [title] => text text text )
[115] => stdClass Object ( [title] => text text )
[116] => stdClass Object ( [title] => text )
)
How to do this?
Thanks.
$new_array = array();
foreach ($var as $object)
{
$temp_object = clone $object;
unset($temp_object->id);
$new_array[$object->id] = $temp_object;
}
I'm making the assumption that there is more in your objects and you just want to remove ID. If you just want the title, you don't need to clone to the object and can just set $new_array[$object->id] = $object->title.
Iterate the array of object and populate a new array of objects with the desired structure using the columnar data. I am manually casting the reduced payloads (containing a single property) as objects.
Code: (Demo)
$result = [];
foreach ($array as $obj) {
$result[$obj->ID] = (object) ['title' => $obj->title];
}
var_export($result);
Or preserve the id column value, unset that property from the object, then push the mutated version of the object.
BUT be careful with this option because objects modify the original values in a foreach()! (Demo)
$result = [];
foreach ($array as $obj) {
$id = $obj->ID;
unset($obj->ID);
$result[$id] = $obj;
}
var_export($result);
echo "\n---\n";
var_export($array); // <-- notice how the original array has lost its id properties
#DanielVandersluis's answer does not suffer this mutation of the original array. Proof: https://3v4l.org/1ggB6

Categories