i have a array of results from a database query.
Now, i need the array in a little bit other structure as it comes from the database query.
I do this for the moment.
$this->db->where($where);
$all=$this->db->get('my_table')->result_array();
after that i reorganize in this way
foreach($all as $element)
{
foreach($element as $subkey=>$subelement)
{
$preset_templates[$element['emailtemplateid']][$subkey]=$subelement;
}
}
Is there a easier or shorter way to do the same?
May be you can try below given code.
foreach($all as $element=>$subelement)
{
$preset_templates[$subelement['emailtemplateid']]=$subelement;
}
I hope it helps.
Related
I'd like to fill an array with data from my database. I created a for loop for that but I think I'm doing something wrong (I'm still very new to programming so sorry for that!)
for($i=0; $i<count($uploadprofile->getAgencies()); $i++){
foreach($uploadprofile->getAgencies() as $agency) {
$users[$i] = $agency->getAgencyUser();
}
}
dump(count($users));
The dump statement only counts 1 user even though there are supposed to be 3 users in there.
I need this array for then using the data in an other for loop afterwards:
for($i=0; $i<count($users); $i++){
foreach($users as $user){
$manager->addNotification($user->toArray()[$i], $notif);
}
}
I am sure this is really bad coding. It looks like way too many lines for something that simple. So I would be really glad about any advice rather than just a "downvoting"!
If more information about the entities are needed, I'd be happy to provide them!
Unless I'm missing something, you have too many loops, and I would stick with foreach. To build the $users array:
foreach($uploadprofile->getAgencies() as $agency){
$users[] = $agency->getAgencyUser();
}
To use it:
foreach($users as $user){
$manager->addNotification($user->toArray(), $notif);
}
But, if you won't actually need the $users array after this then just combine:
foreach($uploadprofile->getAgencies() as $agency){
$user = $agency->getAgencyUser();
$manager->addNotification($user->toArray(), $notif);
}
I'm executing a cursor. I have cutted off the code how the procedure is called and executed. This part is efficient.
At last I have a a not big cursor. I'm calling the procedure, which returns this cursor many times on the page and I need to create a multidimensional array from it. This array should look like like the following:
$ret = oci_execute($outrefc) ;
while ($row = #oci_fetch_array($outrefc))
{
foreach (array_keys($row) as $key)
{
$res[$i][$key] = $row[$key];
}
$i++;
}
Is there any way to make the upper snippet faster?
The multidimensional array should stay as it is. I only wonder if I could create it in any more efficient way.
Thank you!
I am a beginner in PHP. I am trying to make an operation in this array. I want to insert this array in my database like on to many in a table.But before the insertion i have to modify the array values.
this is my array.
$services=[0=>('id_e'=>91701,'id_s'=03),
1=>('id_e'=>'','id_s'=>01),
2=>('id_e'=>'','id_s'=>02)
];
It has to become like as follow.
$services=[0=>('id_e'=>91701,'id_s'=>03),
1=>('id_e'=>'91701','id_s'=>01),
2=>('id_e'=>'91701','id_s'=>02)
];
And then i want insert into the database. Any idea please?
Try this:
$id_e = null;
foreach ($services as &$row) {
if ($row['id_e']) $id_e = $row['id_e'];
else $row['id_e'] = $id_e;
}
unset($row);
demo
I have an array like below --
input array
I want the array to be like ----
output
How I can loop through this array to get desired output ?
Kindly guide.
Thanks.
Since you won't put any research effort into the question yourself, I'll just give you the half answer ...
foreach($inputArray as $i) {
$newArray[$i[7]] = $i[8];
}
Now you just have to add them into the specific array. Tip: You need another foreach loop.
Do something like this --
foreach($new_array as $item) //// for looping outer array
{
foreach($item as $n)
{
$newArray[$n[7]] = $n[8]; /// for looping inner array
}
}
https://graph.facebook.com/search?q=tom&type=user&access_token=2227470867|2.AQD2FG3bzBMEiDV3.3600.1307905200.0-100001799728875|LowLfLcqSZ9YKujFEpIrlFNVZPQ
how to avoid repeat name in facebook people search? in the json code, there have 2 Thomas Lee. Thanks.
foreach ($status_list['data'] as $data) {
echo $data['name']; // not print the same name.
}
$names = Array();
foreach ($status_list['data'] as $data) {
$names[] = $data['name'];
}
$names = array_unique($names); // not print the same name.
foreach ($names as $name) {
echo $name;
}
Here's a fast mashup of how you remove duplicates:
<?php
function existsInArray($list, $key, $value){
foreach($list as $lkey => $lvalue){
if($lvalue[$key] == $value){
return true;
}
}
return false;
}
$sortedUsers = array();
foreach($status_list['data'] as $data){
if(!existsInArray($sortedUsers, "id", $data["id"])){
$sortedUsers[] = $data;
}
}
This will go through the array och users, check if each item exist with the same id in the sorted array. If it doesn't exist, it will be added to the sorted array. Then you have $sortedUsers which doesn't contain any duplicates.
Note: However, this is just proof of concept code. So there are probably a lot of performance optimization that could be done. Also, there are probably some built in functionality to which can do this with less user defined code. Why I showed this is to just explain the process.
Edit: Since this answer got accepted I feel obligated to show something which is a little more high quality than proof of concept code. Also because it got mentioned in the comments that it was inefficient.
So here's easy fix to make this much faster:
$sortedUsers = array();
foreach($status_list['data'] as $data){
$sortedUsers[$data["id"]] = $data;
}
This way it will just overwrite the duplicates and will take away the whole process of comparing each item. In worst case this will be O(n) where as the proof of concept code was O(n ^ (n / 2)) in worst case.