Asked  8 Months ago    Answers:  5   Viewed   36 times

I'm comparing strings with comparison operators.

I needs some short of explanations for the below two comparisons and their result.

if('ai' > 'i')
{
    echo 'Yes';
}
else
{
    echo 'No';
}

output: No

Why do these output this way?

if('ia' > 'i')
{
    echo 'Yes';
}
else
{
    echo 'No';
}

Output: Yes

Again, why?

Maybe I forgot some basics, but I really need some explanation of these comparison examples to understand this output.

 Answers

28

PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order.

  • In the first example, ai comes before i in alphabetical order so the test of > (greater than) is false - earlier in the order is considered 'less than' rather than 'greater than'.

  • In the second example, ia comes after i alphabetical order so the test of > (greater than) is true - later in the order being considered 'greater than'.

Wednesday, March 31, 2021
 
cyber_truite
answered 8 Months ago
80

Maybe the following is a solution:

// loop through array1
foreach($array1 as $line) {
    // check if the word is found
    $word_found = false;

    // explode on every word
    $words = explode(" ", $line);

    // loop through every word
    foreach($words as $word) {
        if(in_array($word, $array2)) {
            $word_found = true;
            break;
        }
    }

    // if the word is found do something
    if($word_found) {
        echo "There is a match found.";
    } else {
        echo "No match found."
    }
}

Should give you the result you want. I'm absolute sure there is a more efficient way to do this.. but thats for you 2 find out i quess.. good luck

Friday, May 28, 2021
 
daiscog
answered 5 Months ago
71

Try this, for example:

$a1 = [ 1 => 'one', 2 => 'two', 3 => 'three',5=>'fayve', 6=>'six'];
$a2 = [ 2 => 'two', 5=>'five'];

print_r( array_diff($a1, $a2) );
$keys = array_keys($a1);
foreach ($keys as $k)
{
    if (!isset($a2[$k])) $a2[$k] = '0';
}
print_r($a2);

Output:

Array
(
    [1] => one
    [3] => three
    [5] => fayve
    [6] => six
)
Array
(
    [2] => two
    [5] => five
    [1] => 0
    [3] => 0
    [6] => 0
)
Saturday, May 29, 2021
 
daiscog
answered 5 Months ago
65

You can't really convert strings to types without a mapping. In fact, you really don't want those strings in the first place except for the actual string passed into addEventListener() as the type parameter. It seems like what you actually want is something like namespaces or modules to organize your types.

For example, using namespaces we can get something similar to your EVENTS object, except that instead of referring to just string values, it refers to string values and types:

namespace EVENTS {
  export namespace button {
    export const myButtonPressed = "myButtonPressed";
    export namespace detailType {
      export interface ButtonDetailType {
        id: string;
      }
    }
  }
  export namespace slider {
    export const mySliderChanged = "mySliderChanged";
    export namespace detailTypes {
      export interface SliderChangingDetailType {
        id: string;
        value: number;
      }
      export interface SliderChangedDetailType {
        id: string;
        oldValue: number;
        newValue: number;
      }
    }
  }
}

This should give you the same autocompletes you saw before, plus ones for the types:

buttonEl.addEventListener(EVENTS.button.myButtonPressed, ((event: CustomEvent) => {
  const detail = event.detail as EVENTS.button.detailType.ButtonDetailType;
}) as EventListener);

sliderEl.addEventListener(EVENTS.slider.mySliderChanged, ((event: CustomEvent) => {
  const detail = event.detail as EVENTS.slider.detailTypes.SliderChangedDetailType;
}) as EventListener)

It's up to you if you want to change the level of nesting and naming of things since the namespace makes some things redundant (maybe you want EVENTS.slider.details.SliderChanged instead of EVENTS.slider.detailTypes.SliderChangedDetailType), but the main idea here is the general approach of using namespaces or modules.

Hope that helps; good luck!

Tuesday, August 3, 2021
 
Corne
answered 3 Months ago
37

You can use explode and array_intersect maybe?

Demo here & here

<?php

  function common($str1,$str2,$case_sensitive = false)
  {
    $ary1 = explode(' ',$str1);
    $ary2 = explode(' ',$str2);

    if ($case_sensitive)
    {
      $ary1 = array_map('strtolower',$ary1);
      $ary2 = array_map('strtolower',$ary2);
    }

    return implode(' ',array_intersect($ary1,$ary2));
  }

  echo common('Product Name - Blue','Blue Green Pink Black Orange');

Returns "Blue";

EDIT Updated it to include a case-insensitive version if you'd like it.

Saturday, August 7, 2021
 
Boris
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share