I have an array of data that is being created from a MySQL query which I am then using to create more data which I then need back in the array (or a new one) but I haven't been able to get this working at all.
Here is the code I have been trying to use:
$vars['items'] = $this->_query('SELECT customers.*, sites.site_name as `default_location`
FROM `customers`
JOIN `sites` ON customers.site_id=sites.id');
$a=array();
foreach($vars['items'] as $customer)
{
if($customer['record_to_use'] == '2')
{
$results = dns_get_record($customer['cust_mx'], DNS_MX);
$mx = min(array_column($results, "pri"));
$highest = array_filter(
$results,
function($item) use($mx) {return $item["pri"] === $mx;}
);
foreach ($highest as $mx)
{
$results = dns_get_record($mx["target"], DNS_A);
foreach ($results as $a)
{
$vars['items']['ip'] .= $a['ip'];
}
}
}
}
So I need the IP that gets created from each item in the array to get added into that array as $vars['items']['ip']
Thanks in advance!
foreach ($results as $a => $value)
{
$vars['items'][$a]['ip'] = $value['ip'];
}
That's what I needed :)
$vars['items']['ip'] .= $a['ip'];
.= is concatenation if that was a typo in your code.
Try $vars['items']['ip'] = $a['ip'];
Related
I have an array data where I am storing the result of an SQL query as below :
$stmt = sqlsrv_query($db,$sql);
$data = [];
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
if(!empty($row)) { $data[] = $row; } }
then I want to create a group key which is the concatenation of specific keys of my array data as below :
foreach ($data as $d) {
$group_key = $d['id'].'_'.$d['Country'].'_'.$d['Order Numer'];
//rest of my code
}
it works fine but I want to choose the keys dynamically instead of setting up manually id, Country and Order Number...
let's say I have an array $PostKeys = ["id","Country","Order Number"]; this array will vary depending on the values selected by user...
What I did is :
$PostKeys = ["id","Country","Order Number"];
foreach ($data as $d) {
foreach($PostKeys as $value)
{ $array_group_key[] = $d[$value] ; }
$group_key = implode("_",$array_group_key);
// rest of my code
}
I am supposed to get the same result but there is always mismatch. I didn't figure out where is the issue exactly. Any suggestions please ? Thank you very much.
You need to empty $array_group_key each time through the loop. Otherwise, you're appending to the results from all the previous rows.
foreach ($data as $d) {
$array_group_key = [];
foreach($PostKeys as $value)
{
$array_group_key[] = $d[$value] ;
}
}
Using below array, i'm trying to use foreach loop to iterate through each item. Then i need to apply if condition to check if the given number is even and odd. I also need to create two arrays one for even and one for odd and push each number in their respective category.
So i have done this so far:
These are the two arrays i created to push through the values to.
}
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if (value %2 == 0)
{
$array_even = $value;
echo $array_even;
}
else
{
$array_odd = $value;
echo $array_odd;
}
I'd like to know if i'm using the correct solution or are there major errors im committing?
It will surely work like charms.
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if ($value %2 == 0)
{
$array_even[] = $value;
}
else
{
$array_odd[] = $value;
}
}
echo "<pre>";
print_r($array_odd);
echo "<pre>";
print_r($array_even);
so i have this function
public function GetNearAirport($adr)
{
$acftrange = $this->GetAcftAdr($adr);
$too = array();
foreach ($acftrange as $key) {
$result = $this->GetNearRange($adr,$key->range);
}
The $acftrange give me some rows, and i need to get a new result for each row of $acftrange. But i need all results in one array So I can insert into the html table. Sorry for my english. In foreach the $key->range is a condition for the query to be made.
I assume getNearRange() returns an array. Use array_merge to append $result to $too
foreach ($acftrange as $key) {
$result = $this->GetNearRange($adr,$key->range);
$too = array_merge($too, $result);
}
I am pulling data from an api and as such i have a loop that stores some ids into an array.
What i need to do is select all ids from my database and then remove any ids that have been found in the database from the initial array. so i can continue to query the api for ids that i do not have currently.
To make more sense please look below:
$matches = $database->get_results('SELECT match_id FROM `matches` WHERE `order_id`='.$order_id);
if ($matchlist->totalGames !== 0) {
foreach ($matchlist as $key) {
$gameIds[] = $key->matchId;
}
}
I need to remove the ids from $gameIds if they already are stored in the $matches.
Any ideas?
Thanks
I have tried:
$matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
if ($matchlist->totalGames !== 0) {
foreach ($matchlist as $key) {
$gameIds[] = $key->matchId;
}
$arr_matches = object2array($matches);
$new_array = array_diff($arr_matches, $gameIds);
var_dump($new_array);
}
error:
Catchable fatal error: Object of class stdClass could not be converted to string
Step 1: Change object to array
function object2array($object)
{
if (is_object($object)):
foreach ($object as $key => $value):
$array[$key] = $value;
endforeach;
else:
$array = $object;
endif;
return $array;
}
Step 2:
$arr_matches = object2array($matches)
$new_array = array_diff($arr_matches , $gameIds);
// Will remove all elements contained in $gameIds from $arr_matches array.
I'm relatively new to PHP and I hope you can help me solve my problem. I am selecting out data from a database into an array for timekeeping. Ultimately, I would like to calculate the total number of hours spent on a project for a given customer.
Here is the code to populate a multi-dimensional array:
...
foreach ($record as $data) {
$mArray = array();
$name = $data['user'];
$customer = $data['customer'];
$project = $data['project'];
$hours = $data['hours'];
$mArray[$name][$customer][$project] += $hours;
}
...
I would now like to iterate over $mArray to generate an xml file like this:
...
foreach ($mArray as $username) {
foreach ($mArray[$username] as $customerName) {
foreach ($mArray[$username][$customerName] as $project ) {
echo '<'.$username.'><'.$customerName.'><'.$project.'><hours>'.
$mArray[$username][$customerName][$project].'</hours></'.$project.'>
</'.$customerName.'></'.$username.'>';
}
}
}
This nested foreach doesn't work. Can someone give me a couple of tips on how to traverse this structure? Thank you for reading!
UPDATE:
Based on the comments I've received so far (and THANK YOU TO ALL), I have:
foreach ($mArray as $userKey => $username) {
foreach ($mArray[$userKey] as $customerKey => $customerName) {
foreach ($mArray[$userKey][$customerKey] as $projectKey => $projectName) {
echo '<name>'.$userKey.'</name>';
echo "\n";
echo '<customerName>'.$customerKey.'</customerName>';
echo "\n";
echo '<projectName>'.$projectKey.'</projectName>';
echo "\n";
echo '<hours>'.$mArray[$userKey][$customerKey][$projectKey].'</hours>';
echo "\n";
}
}
}
This is now only providing a single iteration (one row of data).
Foreach syntax is foreach($array as $value). You're trying to use those values as array keys, but they're not values - they're the child arrays. What you want is either:
foreach($mArray as $username) {
foreach($username as ...)
or
foreach($mArray as $key => $user) {
foreach($mArray[$key] as ...)