Comparing 2d array with 1D array iteration - php

I have 2 arrays translations(2-dimensional),and line(1-dimensional). translations holds arrays, when ever the index for line and translation[][i] matches i want to print that line bold.otherwise print next line as it is. i have tried it with this code.
$translations[0]=array("Volvo", "BMW", "Toyota");
$translations[1]=array("ferrari", "mustang", "bently");
$lines=array("mustang","BMW");
for($i=0;$i<count($translations);$i++){
for($j=0;$j<count($translations[$i]);$j++){
foreach ($lines as $key =>$line){
if($d==$translation[$i][$j]) {
echo "<b>" .$translation[$i][$j] . "</b><br>" ;
}
else{
echo $translation[$i][$j]."<br>";
}
}
}
}
the problem here is that it prints translation subarrays element 3 times. i know the problem is in the way i am iterating the arrays, how should i fix this problem? help will be appreciated.

Try this, Hope this will help you out. Instead of looping over $lines array, you can just check with in_array that whether an element is present or not.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$translations[0] = array("Volvo", "BMW", "Toyota");
$translations[1] = array("ferrari", "mustang", "bently");
$lines = array("mustang", "BMW");
for ($i = 0; $i < count($translations); $i++)
{
for ($j = 0; $j < count($translations[$i]); $j++)
{
if (in_array($translations[$i][$j], $lines))
{
echo "<b>".$translations[$i][$j] ."</b>". PHP_EOL;
}
else
{
echo $translations[$i][$j] .PHP_EOL;
}
}
}

Related

prevent output of data if array not found

I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?
-[UPDATE-
Sorry i had previously inserted the wrong code.. here is the proper code i need checked.
$oid = array('id-ce-subjectAltName');
$count = count($cert['tbsCertificate']);
for($i = 0; $i < $count; $i++) {
if(array_key_exists('extensions', $cert['tbsCertificate']) &&
in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n",'<b>[SANs]</b>',"\n","\n";
}
}
I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.
Notice: Undefined index: extensions in
C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142
AFTER UPDATE:
How is the structure of the array?
You count the number of items in $cert['tbsCertificate'],
but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].
So maybe you try to do something like this?:
$oid = array('id-ce-subjectAltName');
if (array_key_exists('extensions', $cert['tbsCertificate']) &&
is_array($cert['tbsCertificate']['extensions'])
) {
$count = count($cert['tbsCertificate']['extensions']);
for ($i = 0; $i < $count; $i++) {
if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n", '<b>[SANs]</b>', "\n", "\n";
}
}
}
okay, seems to work by adding an isset() at the start of the code:
if(isset($cert['tbsCertificate']['extensions'])) {
thanks guys!

Implementing strlen() in a loop

I've got a string here with names of students (leerlingen) and im trying to follow the exercise here.
The code shows the length of the full string.
Next up would be use a loop to check who has the longest name, but how to implement strlen() in a loop?
// change the string into an array using the explode() function
$sleerlingen = "Kevin,Maarten,Thomas,Mahamad,Dennis,Kim,Joey,Teun,Sven,Tony";
$namen = explode(" ", $sleerlingen);
echo $namen[0];
echo "<br><br>";
//determin the longest name by using a loop
// ask length
$arraylength = strlen($sleerlingen);
sleerlingen = $i;
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
echo $arraylength;
?>
You used bad separator in your explode function, in string there is no space.
This should work (I didn't try it). In foreach loop you check current length with the longest one and if the current is longer, just save it as longest.
<?php
$sleerlingen = "Kevin,Maarten,Thomas,Mahamad,Dennis,Kim,Joey,Teun,Sven,Tony";
$names = explode(',', $sleerlingen);
$longest;
$longest_length = 0;
foreach ($names as $item) {
if (strlen($item) > $longest_length) {
$longest_length = strlen($item);
$longest = $item;
}
}
echo 'Longest name: ' . $longest . ', ' . $longest_length .' chars.';
?>
You can create a custom sort function to sort the array based on the strings length. Then you can easily take the first key in the array.
<?php
$sleerlingen = "Kevin,Maarten,Thomas,Mahamad,Dennis,Kim,Joey,Teun,Sven,Tony";
$namen = explode(",", $sleerlingen); // changed the space to comma, otherwise it won't create an array of the string.
function sortByLength($a,$b){
return strlen($b)-strlen($a);
}
usort($namen,'sortByLength');
echo $namen[0];
?>

how to add data dynamically to an array

I want to add two element in an array. The first one is the key and the second is the value. But I want to add it dynamically. I want to do it like the following code:
$arr="";
for( $i=0;$i<20;$i++ ) {
$arr[$i]=arr($i=>$i+1);
array_push($arr[$i]);
}
print_r($arr);
But of course it don't work. Could anyone tell me how to do it ?
Maybe you are trying to do this:
$arr = array(); // use array() instead of empty string
for( $i=0; $i<20; $i++ ) {
$arr[$i]= $i + 1;
}
print_r($arr);
$arr must be an array not string try this
$arr= array();
instead of
$arr="";
Not sure what you mean by all this, but you didn't really define the arrays correctly.
$arr = array();
for($i=0;$i<20;$i++) {
$arr[$i] = $i + 1;
array_push($arr[$i]);
}
print_r($arr);
Like answered above, you must use the array() function.
Try this way
$arr = array();
for($i=0;$i<20;$i++) {
$arr[$i] = $i+1;
}
print_r($arr);
This is tested and working
<?php
$stack = array("");
for($i=0;$i<20;$i++) {
array_push($stack, $i);
}
print_r($stack);
?>
this code will allow you to do what you request, unless I understood your requirement wrong?
Let me know if this is any help :)
If you are trying to create a numbered list, then use this instead:
<?php
$stack = array("0");
for($i=1;$i<20;$i++) {
array_push($stack, $i);
}
print_r($stack);
?>
Checkout the php manual: http://uk3.php.net/array_push
Josh.

How to put random number in array and showing this array in php

Recently I made a program to create 4 random numbers I want to put these numbers in an array but I did not echo the array's numbers :
my code is:
<?php
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[i] = rand_num_generator();
}
echo $number[2];
?>
Here i am not able to access array using their index values.
You missed the $ sign in front of the i inside $number[i] which must be used before a variable
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[$i] = rand_num_generator();
echo $number[$i].'<br>';
}
//print_r($number);to see the whole array
You only echo once: at echo $number[i];, $i is 4, hence you only display the last random number.
You could loop on your array to echo each.
Put your echo into loop. And you have some mistakes. Use that:
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[$i] = rand_num_generator();
echo $number[$i].'<br>';
}
To put something in an array I recommend to use array_push
<?php
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
array_push($numbers, rand_num_generator());
}
print_r($numbers); //Or use 'echo $numbers[0] . " " . $numbers[1]' etc etc
?>

Whats wrong with this array sorting

Here is my code :
<?php
$a=array(10,8,6,5);
$b=count($a);
for($i=0;$i<($b-1);$i++)
{
for($j=1;$j<($b);$j++)
{
if($a[$j]<$a[$i])
{
$temp = $a[$j];
$a[$j]=$a[$i];
$a[$i]=$temp;
}
}
}
I just want to know what's wrong in the above code ? because if i take 3 array values it works fine but for 4 its not working....can someone do the modification for the same code,and also please briefly explain why is it not working any issues with looping?I am not looking for different code.
You have mistakenly modified the bubble sort algorithm. Use standard one.
<?php
$a=array(10,8,6,5);
$b=count($a);
for($i=0;$i<($b);$i++) //Changes over here
{
for($j=0;$j<($b);$j++) //Changes over here
{
if($a[$j]>$a[$i]) //Changes over here
{
$temp = $a[$j];
$a[$j]=$a[$i];
$a[$i]=$temp;
}
}
}
Why are you manually sorting when you have sort?
$a = array(10,8,6,5);
sort($a);
var_dump($a);
Similarly, why are you using temporary variables when you have list?
list($a[$i],$a[$j]) = array($a[$j],$a[$i]);
try below code may be that will help.
<?php
function pr($array = array())
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
$a = array(10,8,6,5);
$b = count($a);
for($i=0;$i <= ($b-1);$i++)
{
for($j=0; $j < ($b);$j++)
{
if($a[$j] < $a[$i])
{
$temp = $a[$j];
$a[$j]=$a[$i];
$a[$i]=$temp;
}
}
}
pr($temp);
pr($a);
?>
<?php
$a=array(8,6,5);
$b=count($a);
for($i=0;$i<($b);$i++)
{
for($j=0;$j<($b);$j++)
{
if($a[$j]<$a[$i])
{
$temp = $a[$j];
$a[$j]=$a[$i];
$a[$i]=$temp;
}
}
}
The inner loop doesn't need to be executed n times (where n=number of elements to be sorted). Every time the outer loop executes, one more element at the end (for ascending order) is in the correct position. So, inner loop should not check those elements.
<?php
$a=array(10,8,6,5);
$b=count($a);
for($i=0;$i<($b);$i++){
for($j=0;$j<($b-$i);$j++){ // this change will save time
if($a[$j]>$a[$i]){
$temp = $a[$j];
$a[$j]=$a[$i];
$a[$i]=$temp;
}
}
}
?>

Categories