Show only ones that do not match PHP - php

I'm trying to use a multiple foreach and if statements to give me a list of list of people that have not been matched. I have the below code, I am able to get this to successfully give me a list of people it does match.
What I want to do is it echo each the ID from the $tenant_id foreach that have not been found in the $value2 foreach, am I doing something wrong? It will only output nothing?
foreach($array_93 as $value) {
$tenant_id = $value['id'];
$limit = 0;
foreach($obj->response->entries as $value2) {
if($limit==1) break;
if ($value2->{100} == 'true' && $value2->{114} == $tenant_id)
{echo $value['id']; // This should echo ID's that have not been found.}
$limit++;
}
}
};
UPDATE >>
After continuing to try and get this working I have got to this point, I am able to to use this to show which ID's are all 'n' as per screenshot after. The first one is all n's so this has not matched, how can I now make just the ones with all n's ID show?
foreach($array_93 as $value) {
echo '<b>'.$value['id'].'</b>';
echo '<br />';
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
echo '<i>n</i>';
} else {
echo '<b>Y</b>';
}
}
echo '<br />';
};

Use a flag with Y-found state:
foreach($array_93 as $value) {
$Yfound = false;
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) !== false) {
$Yfound = true;
}
}
if(!$Yfound) {
echo $value['id'] . ' has n`s only<br>';
}
}

Hi you should not echo right away :
foreach($array_93 as $value) {
//echo '<b>'.$value['id'].'</b>';
//echo '<br />';
//don't print yet
$output = ""; //this to store your n and Y strings.
$n = 0; //Here you store the number of times Y appears
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
$output .= '<i>n</i>';//concatenating
} else {
$output .= '<b>Y</b>';
$n++;
}
}
//then test if there is a y and echo output.
if($n == 0){
echo '<b>'.$value['id'].'</b>';
echo '<br />';
echo $output;
echo '<br />';
}
};

Related

Moving empty values of an array to the last position

I'm pulling an array from an external API, And I've got some help with usort on here, And It pushes it to the bottom, and empty values are on top. I tried using unset but that just emptied out my entire realArrival values.
e.g.
$fromThis = array(
"realArrival": "",
"realArrival": "Confirm. 06:18",
"realArrival": "Confirm. 06:19"
);
Into:
$intoThis = array(
"realArrival": "Confirm. 06:18",
"realArrival": "Confirm. 06:19",
"realArrival": ""
);
Here is my code on how I tried to do it. I'm sure you are shaking your head at this but here it goes.
$url = 'http://apis.is/flight?language=en&type=arrivals';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
$results = $results['results'];
function cmp($a, $b) {
return strcmp($a['realArrival'], $b['realArrival']);
}
//Sort Time
usort($results, "compare_time");
function compare_time($a,$b) {
$splitA= count(explode(' ',$a['realArrival']));
$splitB= count(explode(' ',$a['realArrival']));
if($splitA==2 && $splitB==2)
{
$first_time = strtotime(explode(' ',$a['realArrival'])[1]);
$second_time = strtotime(explode(' ',$b['realArrival'])[1]);
if($first_time==$second_time)
{
return strcmp($b['realArrival'], $a['realArrival']);
}
return ($second_time < $first_time) ? 1: 0;
}
return strcmp($a['realArrival'], $b['realArrival']);
}
echo '<table class="highlight bordered responsive-table grey darken-2 col s6">';
echo "<tr>";
echo '<th>Date</th>';
echo '<th>Flight Number</th>';
echo '<th>Airline</th>';
echo '<th>From</th>';
echo '<th>Schedule. Time</th>';
echo '<th>Status</th>';
echo "</tr>";
foreach ($results as $item => $val) {
echo "<tr>";
echo '<td>'.$val['date'].'</td>';
echo '<td>'.$val['flightNumber'].'</td>';
echo '<td>'.$val['airline'].'</td>';
echo '<td>'.$val['from'].'</td>';
echo '<td>'.$val['plannedArrival'].'</td>';
// HERE I TRIED TO TARGET THE EMPTY VALUES
if ($val === "") {
unset($results[$item]);
$results[] = $val;
echo '<td style="font-weight: bold;">'.$val['realArrival'].'</td>';
}
echo '</tr>';
}
I know this might seem clunky, but you could always just do something like...
$items_with_real_arrivals = array();
$items_without_real_arrivals = array();
foreach ($results as $item => $val) {
if ($item[‘realArrival’] === "") { $items_without_real_arrivals.push($item); }
else $items_with_real_arrivals.push($item);
}
$reordered_items = array();
for ($i=0; $i<count($items_with_real_arrivals); $i++) {
$reordered_items.push($item);
}
for ($i=0; $i<count($items_without_real_arrivals); $i++) {
$reordered_items.push($item);
}
foreach ($reordered_items as $item => $val) {
echo "<tr>";
echo ' <td>'.$val['date'].'</td>';
echo ' <td>'.$val['flightNumber'].'</td>';
echo ' <td>'.$val['airline'].'</td>';
echo ' <td>'.$val['from'].'</td>';
echo ' <td>'.$val['plannedArrival'].'</td>';
echo ' <td style="font-weight:bold;">' . $val['realArrival'].'</td>';
echo '</tr>';
}

Displaying SQL-data in a table

A few months ago, I made a system where people could upload a picture and then those pictures would be displayed in a table with 4 columns.
Now I am working on a project (to practice my php, I am still learning it) and I would like to use a similar system as below but it should display data from a database.
The result I was trying to achieve is the data being displayed in 2 columns.
if ($handle = opendir('images/')) {
$bool = true;
while ($bool) {
$td = 1;
echo '<tr>';
while ($td < 4) {
$file = readdir($handle);
if ($file != '.' && $file != '..' && $file != '.DS_Store') {
echo '<td>';
if ($file) {
echo '<img src="images/'.$file.
'">';
} else {
$bool = false;
}
echo '</td>';
$td++;
}
}
echo '</tr>';
}
}
closedir($handle);
For anyone looking for the answer:
$query = "SELECT * FROM webshop";
if($query_run = mysql_query($query)) {
$bool = true;
echo '<table>';
while($bool) {
$td = 0;
echo '<tr>';
while($td < 2) {
if($fetch = mysql_fetch_assoc($query_run)) {
echo '<td>';
$title = $fetch['title'];
$info = $fetch['info'];
/* Commented out for now, will be implemented later
$image = $fetch['image'];
$price = $fetch['price'];
*/
echo $title;
} else {
$bool = false;
}
echo '</td>';
$td++;
}
echo '</tr>';
}
echo '</table>';
}

XML - Nodes with the same name showing only one time in PHP

I have a little problem with decoding my xml with php:
I have an xml file with details about movies, what I need to do is to take the nodes torrents->torrent->quality and show the result in my php script. Right now I have a part of the code, it shows me only the first quality that it finds.
(For example: I have the movie "Poltergeist" that is in 720p and 1080p, but in my script it will only show the 720p and skip the 1080p)
Screenshot website:
Screenshot xml:
<link rel="stylesheet" type="text/css" href="css/style.css">
<?php
$xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml?limit=10");
$titolo = array();
$locandina = array();
$anno = array();
$durata = array();
$genere = array();
$qualita = array();
$i = 0;
foreach ($xml->data->movies->movie as $element) {
foreach ($element->children() as $key => $val) {
$chiave = $key;
$valore = $val;
if ($key == "title") {
$titolo[] = $val;
}
if ($key == "medium_cover_image") {
$locandina[] = $val;
}
if ($key == "year") {
$anno[] = $val;
}
if ($key == "runtime") {
$durata[] = $val;
}
if ($key == "genres") {
for($g = 0; $g < count($xml->data->movies->movie[$i]->genres->genre); $g++) {
$genere[$i][] = $xml->data->movies->movie[$i]->genres->genre[$g];
}
}
$qualdef = $xml->data->movies->movie->torrents->torrent;
foreach ($qualdef as $element) {
foreach ($element->children() as $key => $val) {
if ($key == "quality") {
$qualita[] = $val;
}
}
}
}
$i++;
}
for ($i = 0; $i < count($titolo); $i++) {
echo "<div class=totbox>";
if (isset($locandina[$i])) {
echo "<div class=box><img src=" . $locandina[$i] . "></div>";
}
echo "<div class=text><b>" . $titolo[$i] . "</b> - " . $anno[$i] . "</div>";
foreach ($genere[$i] as $genResult) {
echo "<div class=text><b>" . $genResult . "</b></div>";
}
echo "<div class=text><b> Qualita':" . $qualita[$i] . "</b></div>";
echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
echo "</div>";
}
?>
What am I doing wrong?
It seems like you are collecting data without any problem but when you are displaying you go by the index of $titolo so you don't go through every element of $qualita array.
So just try this:
echo "<div class=text><b> Qualita':" . implode(', ',$qualita) . "</b></div>";

Count duplicate values in PHP array

my problem is, i have this script
foreach ($arr1 as $k => $val) {
if (in_array($val, $arr2)) {
echo 'Obsazeno <br>';
} else {
echo $val . "<BR>";
}
}
Where $arr1 is generated array of days and $arr2 is array from mysql results. Actually it works fine. If one of days from $arr1 is in database, it will echo "Obsazeno" instead of day value.
But i need small upgrade. Script check if $val is in $arr2 and i need small counting contidion that will do this:
If $val is in $arr2 once - it will echo $val.
If $val is in $arr2 twice - it will echo $val.
But if $val is in $arr2 thrice - it will echo "Obsazeno".
I hope you understand my question.
I quest, it is possible by array_count_values. I try it by myself but no success.
You can achieve this with array_count_values.
$values = array_count_values($arr2);
foreach ($arr1 as $k => $val) {
if (array_key_exists($val, $values) && $values[$val] >= 3) {
echo 'Obsazeno <br>';
} else {
echo $val . '<br>';
}
}
try
$i=0;
foreach($arr1 as $k=>$val) {
if(in_array($val,$arr2)) {
$i++;
if($i == 3) {
echo 'Obsazeno <br>';
}
else {
echo $val."<BR>";
}
}
}
$data = array();
foreach($arr1 as $k=>$val)
{
if(!array_key_exists($val,$data)){
$data[$val] = 0;
}
if(in_array($val,$arr2))
{
$data[$val]++;
if($data[$val]==3){
echo 'Obsazeno <br>';
}else{
echo $val."<BR>";
}
}
else
{
echo $val."<BR>";
}
}

how to ignore first loop and continue from second in foreach?

I use foreach loop but it always gives an wierd result in the first but others are fine so i want to remove 1st loop and continue from 2nd...
My code is
foreach($doc->getElementsByTagName('a') as $a){
foreach($a->getElementsByTagName('img') as $img){
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}
$counter = 0;
foreach($doc->getElementsByTagName('a') as $a){
foreach($a->getElementsByTagName('img') as $img){
if ($counter++ == 0) continue;
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}
The simplest way I can think of in order to skip the first loop is by using a flag
ex:
$b = false;
foreach( ...) {
if(!$b) { //edited for accuracy
$b = true;
continue;
}
}
try something like this
foreach($doc->getElementsByTagName('a') as $a)
{
$count = 0;
foreach($a->getElementsByTagName('img') as $img)
{
if(count == 0)
{
$count++;
continue;
}
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}
$nm = 0;
foreach($doc->getElementsByTagName('a') as $a){
if($nm == 1){
foreach($a->getElementsByTagName('img') as $img){
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}
$nm=1;
}
If you don't wanna define an extra counter:
foreach($doc->getElementsByTagName('a') as $a){
foreach($a->getElementsByTagName('img') as $img){
if ( $a === reset( $doc->getElementsByTagName('a') ) && $img === reset( $a->getElementsByTagName('img') ) ) continue;
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}
I don't know which will perform better.
You could also try array slice in PHP:
foreach(array_slice($doc->getElementsByTagName('a'),1) as $a){
foreach(array_slice($a->getElementsByTagName('img'),1) as $img){
echo $a->getAttribute('href');
echo $img->src . '<br>';
}
}

Categories