PHP Two Arrays - If they are different then insert - Otherwise Update - php

I have some data, one array from CSV File, another array contains a value of a field in a post.
With this data, i would like to check if the value of the field is in the CSV then update the field, if not and there is a difference to insert the field into a post.
$csv_data array has lots more fields than $vrm array. I think I need a function that says, if element is in array then, however I am unsure how to do this.
Here is what I have so far:
$vrm = [];
foreach($vehicles as $vehicle) {
$vrm[] = $vehicle->REGISTRATION;
}
$difference = array_diff($csv_data, $vrm);
if(empty($difference)) {
echo "Need to Update";
} else {
echo "Need to insert";
}
Is anyone able to assist me with this or point me in the right direction please?
** EDIT **
Array $vrm is:
Array ( [0] => CV56IPG,
[1] => RT56KLP,
[2] => AB12HNJ)
Array $csv_data is:
Array ( [0] => Array (
[REGISTRATION] => CV56IPG
[MAKE] => Volkswagen
[MODEL] => Polo
[DERIVATIVE] => 1.2 Match 5dr
)
[1] => Array (
[REGISTRATION] => AB12HNJ
[MAKE] => Volkswagen
[MODEL] => Polo
[DERIVATIVE] => 1.2 Match 5dr
)
)

I don't exactly understand your word.
I guess, If you have $vrm unique id, If you use haspMap, you can do this.
{
vrm_id1: vrm_data1,
vrm_id2: vrm_data2
}
if you insert or update new data.
when vrm_id is not exist => insert.
when vrm_id is exist => update.

Related

Extract multidimensional array values

I'm dealing with this array, but the key [row_204] changes each time (ie sometimes it is [row_79] or [row_109]) but all other key names stay the same in this exact structure. I need to get the value of UUID and userID but can't find a solution to get the value by key in that [row_] array.
I need to be able to extract the values and place them in strings, for example,
$uuid =
and so on.
I can't seem to find a similar query and have tried so many variations. Many thanks in advance.
Array
(
[action] => edit
[data] => Array
(
[row_204] => Array
(
[UUID] => 148367FF-FBEB-413D-8495-6B1539BDC5DC
[userID] => 7
[maxPoints] => 7
[awardedPoints] => 6
[Date] => 2017-06-08
)
)
)
If you know there is always only one row_* item in that array, you can just pull the first item (i.e., the only one in your case) off the front of the list with array_shift():
$data = array_shift($array['data']);
print_r($data);
Will give you:
Array (
[UUID] => 148367FF-FBEB-413D-8495-6B1539BDC5DC
[userID] => 7
[maxPoints] => 7
[awardedPoints] => 6
[Date] => 2017-06-08
)
Then you can just deference the keys you want:
$uuid = $data['UUID'];
The easiest would probably be:
$data = current($_POST['data']);
Then just echo $data['UUID'];.
If you need the key for whatever reason:
list($key, $data) = each($_POST['data']);

Extract data from php array

I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}

PHP add/remove values from array created by json

I have an array that looks like this after using json_decode() on a response from a Json webservice:
[11] => Array
(
[0] => 80B37803-6278-5351-BC7A-D3A2FBFF8AA7
[1] => test
[2] =>
)
[12] => Array
(
[0] => 70B37803-6278-5351-BC7A-D3A2FBFF8AA8
[1] => test 2
[2] =>
)
[13] => Array
(
[0] => 90B37803-6278-5351-BC7A-D3A2FBFF8AA9
[1] => test 3
[2] =>
)
To print the array I use the following code:
echo '<pre>'; print_r($responseArticle); echo '</pre>';
How can I edit this kind of array in order, for example, to add a 3rd row or delete one of the already exsisting row?
$newArray = json_decode($json_data);
add inner data
adding a column for 11 number of row
$newArray[11][4] = 'this is the 4th column';
for delete
unset($newArray[11][4]);
for adding row
$newArray[lastindex] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test4','');
for delete row
unset($newArray[index]);
to add another value of an array is to use array_push and to delete it you can use unset. See the example below.
<?php
$json_array[11] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7','test','');
$json_array[12] = array('70B37803-6278-5351-BC7A-D3A2FBFF8AA8','test 2','');
$json_array[13] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 3','');
array_push($json_array, array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 4',''));
echo '<pre>';print_r($json_array);echo '</pre>';
unset($json_array[14]);
echo '<pre>';print_r($json_array);echo '</pre>';
?>
To add in some info for N key:
$responseArticle[N] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7', 'testN', '');
To delete info stored in N key:
if(isset($responseArticle[N]) {
unset($responseArticle[N]);
}
Here N can be any number, say 3 as you have asked in the question.

How to merge sub-arrays based on first value php

I have an array that is put together. It's basically 24 data points across 7 stations. Each station has a data point each hour and a new sub0array is created for each data point. The problem with this, is I now want to split the data up based on station so I can sort it. When I go to do that array_filter doesn't work for me. I wanted to basically combine all of the values that have the same row 0 result in each sub-array. but keep each data point based on them. Row 0 is the station name. Here is an example of what the array looks like. There are 160 sub arrays. All I need to do is combine them together based on Station name. I was going to do this based on Array value, but the problem is if a data-point is missing, it breaks that logic.
Array
(
[0] => Array
(
[0] => STATIONONE
[1] => 02/22/15 04:00:00 PM
[2] => SW
[3] => Array
(
[0] => 4.51
)
[4] => MPH
[5] => Array
(
[0] => 16.1
)
[6] => MPH
)
[1] => Array
(
[0] => STATIONONE
[1] => 02/22/15 05:00:00 PM
[2] => S
[3] => Array
(
[0] => 2.7
)
[4] => MPH
[5] => Array
(
[0] => 9.61
)
[6] => MPH
)
And it just keeps repeating till I have 23 Values. Then the next station.
So 0-22 subarray value = station 1
23-45 subarray value = station 2
etc.. just stating to help show what I am trying to do.
It isn't entirely clear what you expect your output to be, but this might get you there:
$entriesByStation = []
foreach ($entries as $entry) {
$entriesByStation[$entry[0]] = $entry;
}
This will group all the the entries by station name in the $entriesByStation array
The accepted answer got me close into figuring this out. I had to do this by station though. So I just created 7 foreach statements and created an array for each station. This way I can also merge the array together if needed.
# Parse our All Data and set station
foreach ($array as $entry) {
if ($entry[0] === "STATIONONE"){
$S1pkwsite = ($entry[0]);
$S1pkwvalue = ($entry[5][0]);
$S1pkwdate = ($entry[1]);
$S1pkwunit = ($entry[6]);
$S1wspvalue = ($entry[3][0]);
$S1wspunit = ($entry[4]);
$S1wdrvalue = ($entry[2]);
$S1pkwdataarray[] = array('SiteName'=>$S1pkwsite,'DATE'=>$S1pkwdate,'WDR'=>$S1wdrvalue,'VALUE'=>$S1pkwvalue,'UNIT'=>$S1pkwunit);
}
}
array_push ($Stationone_data, $pkwdataarray);
$Stationone_data = array_shift($Stationone_data);

Selecting value from array results

Being new to learning PHP I am having trouble with understanding how to select/echo/extract a value from array result a API script returns.
Using the standard:
echo "<pre>";
print_r($ups_rates->rates);
echo "</pre>";
The results returned look like this:
Array
(
[0] => Array
(
[code] => 03
[cost] => 19.58
[desc] => UPS Ground
)
[1] => Array
(
[code] => 12
[cost] => 41.69
[desc] => UPS 3 Day Select
)
[2] => Array
(
[code] => 02
[cost] => 59.90
[desc] => UPS 2nd Day Air
)
)
If I am only needing to work with the values of the first array result: Code 3, 19.58, UPS Ground --- what is the correct way to echo one or more of those values?
I thought:
$test = $ups_rates[0][cost];
echo $test;
This is obviously wrong and my lack of understanding the array results isn't improving, can someone please show me how I would echo an individual value of the returned array and/or assign it to a variable to echo the normal way?
echo $ups_rates->rates[0]["cost"];
See Arrays
More:
To iterate over the array
foreach ($ups_rates->rates as $rate) {
echo $rate["cost"];
// ...
}
$array = $ups_rates->rates;
$cost = $array[0]['cost'];
echo $ups_rates->rates[0]['code'];
echo $ups_rates->rates[0]['cost'];
echo $ups_rates->rates[0]['desc'];
should print out all 3
rates[0] is the first element of your array and you can index into that array by appending a ['key'] index onto the end
the only thing you forgot is ' marks

Categories