In MySQL I can SELECT a value from one column of a row WHERE one or more columns have a specified value:
SELECT name FROM table WHERE birthyear = '1965' AND birthplace = 'Vancouver'
Now I have a multidimensional PHP_array (not a mySQL database), and I would like to extract the name from the row for a given birthyear and birthplace in a similar manner. This is the array:
$example = Array
(
[0] => Array
(
[0] => 1965
[1] => Bob
[2] => Vancouver
)
[1] => Array
(
[0] => 1973
[1] => John
[2] => Vancouver
)
[2] => Array
(
[0] => 1965
[1] => Paul
[2] => Houston
)
)
It seems to me that for each WHERE-condition I would need one loop in PHP. If you assume that I need to match several columns (birthday, birthplace, sex, occupation etc.), you'll understand how this slows down the execution over an array with thousands of entries.
How can I do this with the least number of loops?
array_filter($example,function($v) { return $v[0] == 1982; });
This function will do what you are looking for:
function getNameByYear($year, array $array) {
$name = false;
foreach ($array as $a) {
if ($a[0] == $year) {
$name = $a[1];
break;
}
}
return $name;
}
Usage:
$example; // your array
$n = getNameByYear(1965, $array); // Bob
$n = getNameByYear(1973, $array); // John
$n = getNameByYear(1982, $array); // Paul
$n = getNameByYear('1965', $array); // Bob
$n = getNameByYear('1973', $array); // John
$n = getNameByYear('1982', $array); // Paul
$n = getNameByYear('wrong', $array); // false
$n = getNameByYear(2035, $array); // false
Related
I have a quiz where you can use text field or radio buttons for the answers. The answers are stored in a database. None of the answers are required so people can skip questions.
Each entry is stored in a row:
Array
(
[0] => Array
(
[0] => Dave
[1] => ok
[2] => Manchester
[3] => No
)
[1] => Array
(
[0] => James
[1] => Happy
[2] => London
[3] => Yes
)
[2] => Array
(
[0] => Victoia
[2] => Leeds
)
)
Currently the question number is represented by the key. So Victoria hasn't answered question 1 or 3. My aim is to add the unanswered into the array witht the correct key value being null.
Here is my code so far but I'm struggling to get the array key position correct:
$answersArr = (array) $answers;
$row = array();
$items = array();
$numberOfQuesitons = count($headers);
foreach ($answersArr as $key=>$result) {
$answer = json_decode(stripslashes($result->answers));
$row[$key] = (array) $answer;
$single = count($row[$key]);
$currentKey = key($row[$key]);
for ($i = $single ; $i < $numberOfQuesitons; $i++) {
if ($numberOfQuesitons - $i > 0) {
if ($currentKey > 0) {
array_unshift($row[$key], null);
} else {
array_push($row[$key], null);
}
}
}
}
print_r($row);
The out put I get:
Array
(
[0] => Array
(
[0] => Admin
[1] => ok
[2] => Manchester
[3] => No
)
[1] => Array
(
[0] => Rod
[1] => Happy
[2] => London
[3] => Yes
)
[2] => Array
(
[0] => Rozi
[2] => Leeds
[3] =>
[4] =>
)
)
The last row in the array needs to look like this:
[2] => Array
(
[0] => Victoia
[1] =>
[2] => Somewhere
[3] =>
)
A little stuck here any help would be much appreciated.
Thanks
Let me say that I read through your code and have to make two assumptions: $numberOfQuesitons = 4 and $single = 2 in the case of Victoia.
Assumption is based on the fact that the name of a person is included in the result arrays and otherwise could not be derived.
So in your code at the iteration of Victoia we will have the following array to work with:
$row[$key] = [
0 => 'Victoia',
2 => 'Leeds',
];
Then at the inner for loop the following will happen (as noted in comments):
for ($i = $single; $i < $numberOfQuesitons; $i++) {
// 4 - 2 = 2, 2 > 0 = true <-- first iteration
// 4 - 3 = 1, 1 > 0 = true <-- second (and final) iteration
if ($numberOfQuesitons - $i > 0) {
// $currentKey doesn't change in this process
// and since the key is taken from the array
// $row[$key] points to, the key function will return 0 each iteration.
if ($currentKey > 0) {
// Also note that unshift will only add elements to the front of the array.
array_unshift($row[$key], null);
} else {
// Hence we drop down in this branch of the if statemtent
// as 0 > 0 evaluates to false each evaluation.
// So we start with [0] => Rozi || Victoia
// [2] => Leeds || Somewhere
// Push null thus: [3] => null
// Push null thus: [4] => null
// Hence our final result.
// Also note that push only 'pushes' elements at the end of the array.
array_push($row[$key], null);
}
}
}
To solve this we could change the logic a little bit, but personally I would modify the nested loop to the following (or something similar):
$answersArr = (array) $answers;
$row = array();
$numberOfQuesitons = count($headers);
foreach ($answersArr as $key => $result) {
$answer = json_decode(stripslashes($result->answers));
$row[$key] = (array) $answer;
// We simply create a range from 0 up to the last question number.
$all = range(0, $numberOfQuesitons - 1);
// Taking the difference between all questions and the answered
// ones will give us the missing ones.
$unAnsweredQs = array_diff($all, array_keys($row[$key]));
// Add those missing questions to the array.
foreach ($unAnsweredQs as $unAnswered) {
// Or maybe a more appropriate default.
$row[$key][$unAnswered] = null;
}
// Sort the keys, such that we respect the wanted order
// Name (key 0), Q1, Q2, ..., QN
ksort($row[$key]);
}
print_r($row);
I have an array of elements which I queried from the mongoDB.
This array has the id of a device and value of this device's consumptions.
For example there are 3 different ids -> 18,5,3 and multiple mixed values.
// first record of 18 so get value.
$row[0]["id"] = 18;
$row[0]["value"] = 100;
// not first record of 18 so ignore and move to the next record
$row[1]["id"] = 18;
$row[1]["value"] = 40;
// first record of 5 so get value.
$row[2]["id"] = 5;
$row[2]["value"] = 20;
// not first record of 18 so ignore and move to the next record
$row[3]["id"] = 18;
$row[3]["value"] = 30;
// first record of 3 so get value.
$row[4]["id"] = 3;
$row[4]["value"] = 20;
//not first record of 5 so ignore and move to the next record**
$row[5]["id"] = 5;
$row[5]["value"] = 30;
// not first record of 18 so ignore and move to the next record
$row[6]["id"] = 18;
$row[6]["value"] = 10;
...
....
What I am trying to do is loop through this $row array and get the most recent value of the id.
For example in above example what I want to return is:
id value
18 100
5 20
3 20
How can I do that kind of logic?
It can be done in several ways. The easiest one is to use a foreach:
$result = array();
foreach ($row as $i) {
if (! array_key_exists($i['id'], $result)) {
$result[$i['id']] = $i['value'];
}
}
# Verify the result
print_r($result);
The output is:
Array
(
[18] => 100
[5] => 20
[3] => 20
)
The same processing, but using array_reduce():
$result = array_reduce(
$row,
function(array $c, array $i) {
if (! array_key_exists($i['id'], $c)) {
$c[$i['id']] = $i['value'];
}
return $c;
},
array()
);
If you want to keep only the first occurrence of each 'id' then just add the values to an aggregate array - but only if they haven't been added already. Then grab the values of the aggregate array.
https://tehplayground.com/NRvw9uJF615oeh6C - press Ctrl+Enter to run
$results = array();
foreach ($row as $r) {
$id = $r['id'];
if (! array_key_exists($id, $results)) {
$results[$id] = $r;
}
}
$results = array_values($results);
print_r($results);
Array
(
[0] => Array
(
[id] => 18
[value] => 100
)
[1] => Array
(
[id] => 5
[value] => 20
)
[2] => Array
(
[id] => 3
[value] => 20
)
)
Try this
$alreadyfound = []; // empty array
$result = [];
foreach ($row as $item)
{
if (in_array($item["id"], $alreadyfound))
continue;
$alreadyfound[] = $item["id"]; // add to array
$result[] = $item;
}
The result
Array
(
[0] => Array
(
[id] => 18
[value] => 100
)
[1] => Array
(
[id] => 5
[value] => 20
)
[2] => Array
(
[id] => 3
[value] => 20
)
)
The array_unique() function is exactly what you are looking at.
See the documentation here : array_unique() documentation
Using array_column with an index key will almost do it, but it will be in the reverse order, so you can reverse the input so that it works.
$result = array_column(array_reverse($row), 'value', 'id');
I need to show the email address, first and last names of users who are not registered in my new database.
I selected three columns of my old database and three columns of my new database. I created two arrays and each receives the result of the query.
But when the comparing is displaying all users, the comparison is not made.
Check my code:
while($array = $resultado->fetch_array(MYSQLI_NUM)){
$portal_old[] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_NUM)){
$portal_new[] = $array2;
}
foreach ($portal_old as $portal) {
if(!in_array($portal, $portal_new)){
$result[] = $portal;
}
}
Assuming email address should be able to uniquely identify a registered user, you can use email address as a key as you build your array of results from each database.
while($array = $resultado->fetch_array(MYSQLI_ASSOC)){
$portal_old[$array['email']] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_ASSOC)){
$portal_new[$array2['email']] = $array2;
}
In order for this to work, you will need to either use MYSQLI_ASSOC instead of MYSQLI_NUM when fetching, and specify the name of the email column when building your array, or if you are using MYSQLI_NUM, specify the numeric index of the email column.
Then you can use array_diff_key to easily find the result you are looking for.
$result = array_diff_key($portal_old, $portal_new);
This works, John Smith here gets in $result while Joe Black doesn't:
<?php
$name = array(0 => 'john', 1 => 'smith');
$portal_old[] = $name;
$name = array(0 => 'joe', 1 => 'black');
$portal_old[] = $name;
//print_r($portal_old);
//shows: Array ( [0] => Array ( [0] => john [1] => smith ) [1] => Array ( [0] => joe [1] => black ) )
$name = array(0 => 'jason', 1 => 'mill');
$portal_new[] = $name;
$name = array(0 => 'joe', 1 => 'black');
$portal_new[] = $name;
//print_r($portal_new);
//shows: Array ( [0] => Array ( [0] => jason [1] => mill ) [1] => Array ( [0] => joe [1] => black ) )
foreach($portal_old as $key=>$value) {
if(!in_array($value[0], $portal_new[0]) && !in_array($value[1], $portal_new[1])) {
$result[] = $value;
}
}
print_r($result);
//shows: Array ( [0] => Array ( [0] => john [1] => smith ) )
?>
It's because the results of $portal_old and $portal_new are multidimensional arrays, you need to compare by looking inside the arrays of the arrays.
This will work for you :
$i = $j = 0;
while($array = $resultado->fetch_array(MYSQLI_NUM)){
$portal_old[$i][$array['0'].' | '.$array['1'].' | '.$array['2']] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_NUM)){
$portal_new[$j][$array2['0'].' | '.$array2['1'].' | '.$array2['2']] = $array2;
}
// Get only array keys
$old_arr_key = array_keys($portal_old[0]);
$new_arr_key = array_keys($portal_new[0]);
// Result
$result = array_diff($new_arr_key,$old_arr_key);
print_r($result);
I'm creating a search function for a csv. With the result of my search I then want to remove duplicates and then move those that had duplicates to the beginning of the array.
Array
(
[0] => Array
(
[0] => COD 21
[1] => 4567
[2] => Robert Wahl
)
[1] => Array
(
[0] => RT 5
[1] => 1234
[2] => Robert Merrill
)
[2] => Array
(
[0] => XD 62
[1] => 1653
[2] => Robert Hill
)
[3] => Array
(
[0] => RT 5
[1] => 1234
[2] => Robert Merrill
)
)
I have a function that removes duplicates, but I'm not sure how to get it to move the values that have duplicates to the beginning of the array, and order them according to those with the most duplicates:
function arrayUnique($array) {
$aux_ini=array();
$result=array();
for($n=0;$n<count($array);$n++)
{
$aux_ini[]=serialize($array[$n]);
}
$mat=array_unique($aux_ini);
for($n=0;$n<count($array);$n++)
{
$result[]=unserialize($mat[$n]);
}
return $result;
}
Any and all help is appreciated.
function arrayUnique($array) {
// count values
$arr = array_count_values (array_map('serialize',$array));
// sort in desc order
arsort($arr);
// take keys and restore array
return array_map('unserialize',array_keys($arr));
}
function array_unique_ordered_by_duplicates($array)
{
//serialize inner arrays
$serialized = array_map('serialize', $array);
//create an array indexed by the serialized values, with all keys having a value of 0
$flipped = array_map(function($el){
return 0;
},array_flip($serialized));
//increment the count each time an element is encountered, thereby gicing duplicates a higher value
foreach($serialized as $v){
$flipped[$v]+=1;
}
//sort descending on the calculated value, putting the most duplicated items first
arsort($flipped);
//return unserialized, deduped, orded array
return array_map('unserialize',array_keys($flipped));
}
Note that splash58's is a more efficient version of the above, using php's array_count_values, and should be used instead
A little bit funky but I think the code is relatively easy to follow.
$csv = array(array('COD 21','4567','Robert Wahl')
,array('RT 5','1234','Robert Merrill')
,array('XD 62','1653','Robert Hill')
,array('RT 5','1234','Robert Merrill')
);
echo "Begin...<br>\n";
$serials = array();
$dupeqty = array();
foreach ($csv as $csvkey => $csvdata)
{
$dupekey = serialize($csvdata);
if ( !isset($serials[$dupekey]) )
$serials[$dupekey] = $csvkey;
if ( !isset($dupeqty[$dupekey]) )
$dupeqty[$dupekey] = 0;
$dupeqty[$dupekey]++;
}
arsort($dupeqty);
foreach ($dupeqty as $dupekey => $times)
echo $dupekey . " occurs $times times<br>\n";
flush();
echo "<br>\n";
echo "Accessing the original array...<br>\n";
foreach ($dupeqty as $dupekey => $times)
{
$data = $csv[$serials[$dupekey]];
echo '... data ' . $data[0] . ' ' . $data[1] . ' ' . $data[2] . "<br>\n";
}
Running it generates:
Begin...
a:3:{i:0;s:4:"RT 5";i:1;s:4:"1234";i:2;s:14:"Robert Merrill";} occurs 2 times
a:3:{i:0;s:5:"XD 62";i:1;s:4:"1653";i:2;s:11:"Robert Hill";} occurs 1 times
a:3:{i:0;s:6:"COD 21";i:1;s:4:"4567";i:2;s:11:"Robert Wahl";} occurs 1 times
Accessing the original array...
... data RT 5 1234 Robert Merrill
... data XD 62 1653 Robert Hill
... data COD 21 4567 Robert Wahl
try this,
function array_multidimensional($array)
{
$serialized = array_map('serialize', $array);
$array = array_unique($serialized);
return array_intersect_key($array, $unique);
}
Well this has been a headache.
I have two arrays;
$array_1 = Array
(
[0] => Array
(
[id] => 1
[name] => 'john'
[age] => 30
)
[1] => Array
(
[id] => 2
[name] => 'Amma'
[age] => 28
)
[2] => Array
(
[id] => 3
[name] => 'Francis'
[age] => 29
)
)
And another array
array_2 = = Array
(
[0] => Array
(
[id] => 2
[name] => 'Amma'
)
)
How can I tell that the id and name of $array_2 are the same as the id and name of $array_1[1] and return $array_1[1]['age']?
Thanks
foreach($array_1 as $id=>$arr)
{
if($arr["id"]==$array_2[0]["id"] AND $arr["name"]==$array_2[0]["name"])
{
//Do your stuff here
}
}
Well you can do it in a straightforward loop. I am going to write a function that takes the FIRST element in $array_2 that matches something in $array_1 and returns the 'age':
function getField($array_1, $array_2, $field)
{
foreach ($array_2 as $a2) {
foreach ($array_1 as $a1) {
$match = true;
foreach ($a2 as $k => $v) {
if (!isset($a1[$k]) || $a1[$k] != $a2[$k]) {
$match = false;
break;
}
}
if ($match) {
return $a1[$field];
}
}
}
return null;
}
Use array_diff().
In my opinion, using array_diff() is a more generic solution than simply comparing the specific keys.
Array_diff() returns a new array that represents all entries that exists in the first array and DO NOT exist in the second array.
Since your first array contains 3 keys and the seconds array contains 2 keys, when there's 2 matches, array_diff() will return an array containing the extra key (age).
foreach ($array_1 as $arr) {
if (count(array_diff($arr, $array_2[1])) === 1) {//meaning 2 out of 3 were a match
echo $arr['age'];//prints the age
}
}
Hope this helps!
I assume you want to find the age of somebody that has a known id and name.
This will work :
foreach ($array_1 as $val){
if($val['id']==$array_2[0]['id'] && $val['name']==$array_1[0]['name']){
$age = $val['age'];
}
}
echo $age;
Try looking into this.
http://www.w3schools.com/php/func_array_diff.asp
And
comparing two arrays in php
-Best