Viewed   71 times

Currently I have 2 array:

array(1, 2, 3, 4);
array(4, 5, 6, 7);

How can I check if there is at least one equal value in both of them? (The example above has 1 equal value => 4, so the function should return true).

 Answers

2

array_intersect()

returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved

$a = array(1, 2, 3, 4);
$b = array(4, 5, 6, 7);
$c = array_intersect($a, $b);
if (count($c) > 0) {
    var_dump($c);
    //there is at least one equal value
}

you get

array(1) {
  [3]=>
  int(4)
}
Saturday, August 6, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
2

You can use any:

any(x in a for x in b)

The nice thing about this generator expression is that any will return True as soon as the generator yields a True, i.e. there won't be redundant x in a lookups.

Edit:

You can improve the time complexity by making a set from a.

a_set = set(a)
any(x in a_set for x in b)

Regarding your new question:

[x == y for x,y in zip(a,b)]
Thursday, August 25, 2022
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
3

You can use the format-number() function to convert a number into a string in a given format. At least two decimal places will be appeared if you use this "#.00##########" format string.

If you have an xml file which can contains values like 54.2,54.23,54.234,54.234567:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
        <price>54.2</price>
        <price>54.23</price>
        <price>54.234</price>
        <price>54.234567</price>
</catalog>

You can convert the numbers with an xslt like this to get at least two decimal places

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <xsl:for-each select="/catalog/price">
             <xsl:value-of select='format-number(., "#.00##########")'/><br />
      </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Then the output:

54.20
54.23
54.234
54.234567
Tuesday, December 27, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :