Asked  2 Years ago    Answers:  5   Viewed   277 times

Is there any php function such as in_array for associative arrays you get by the mysql function "mysql_fetch assoc" ?

For example, if I have an $array that looks like this:

array(0=>(array(ID=>1, name=>"Smith"), 1=>(array(ID=>2, name=>"John"))

Can I do something like in_array(key,value,array)?

Or in my case, if I am looking for the ID value of "1", in_array("ID",1,$array).

This is my solution, comment on it if you think it's the right way:

function in_assoc_array($key,$value,$array)
{
    if (empty($array))
        return false;
    else
    {
        foreach($array as $a)
        {
            if ($a[$key] == $value)
                return true;
        }
        return false;
    }
}

 Answers

1

Try this..... You can use this function for any depth of the associated array. Just contraint to this function is that the key value would not be repeat any where in array.

<?php 
function is_in_array($array, $key, $key_value){
      $within_array = 'no';
      foreach( $array as $k=>$v ){
        if( is_array($v) ){
            $within_array = is_in_array($v, $key, $key_value);
            if( $within_array == 'yes' ){
                break;
            }
        } else {
                if( $v == $key_value && $k == $key ){
                        $within_array = 'yes';
                        break;
                }
        }
      }
      return $within_array;
}
$test = array(
                0=> array('ID'=>1, 'name'=>"Smith"), 
                1=> array('ID'=>2, 'name'=>"John")
        );
print_r(is_in_array($test, 'name', 'Smith'));
?>
Thursday, November 10, 2022
 
chiwda
 
3

Since you have plain integers, you can simply join them with commas:

$sql = "SELECT * FROM table WHERE comp_id IN (" . implode(',', $arr) . ")";

If working with with strings, particularly untrusted input:

$sql = "SELECT * FROM table WHERE comp_id IN ('" 
     . implode("','", array_map('mysql_real_escape_string', $arr)) 
     . "')";

Note this does not cope with values such as NULL (will be saved as empty string), and will add quotes blindly around numeric values, which does not work if using strict mysql mode.

mysql_real_escape_string is the function from the original mysql driver extension, if using a more recent driver like mysqli, use mysqli_real_escape_string instead.

However, if you just want to work with untrusted numbers, you can use intval or floatval to sanitise the input:

$sql = "SELECT * FROM table WHERE comp_id IN (" . implode(",", array_map('intval', $arr)) . ")";
Friday, November 25, 2022
4

The simplest and likely most efficient way to do this is by iterating the array that holds the sort order and creating a new, sorted array:

$sorted = array();

foreach ($tempArray as $order) {
  if (isset($myArray[$order])) {
    $sorted[$order] = $myArray[$order];
  }
}

print_r($sorted);

This works because associative arrays implicitly have an order of the order in which elements were added to the array.

See it working


EDIT

Any solution involving a sorting function will likely be much less efficient than this. This is because in order to do it you will need to use a function that takes a callback - this already has an implied overhead of the function call.

The sorting functions also work by comparing items, meaning that the complexity any of those solutions will be greater than that of this solution (the complexity of this is simply O(n)). Also, in order to derive the return value for the sorting function you would need to inspect the target array, finding the position of each of the keys being compared, for each comparison, adding even more complexity.

Friday, August 19, 2022
 
karl
 
2

Can't you just do:

$resulting_array = $array2 + $array1;

?

Sunday, November 20, 2022
 
1

If you are using composer to manage your project, you can just change the composer.json file:

"autoload": {
    "psr-4": {
        "": "src/",
        "": "vendor/phpoffice/phpexcel/Classes/"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

Then add

use PHPExcel;
use PHPExcel_IOFactory;

to your controller file, and you can use the PHPExcel like this:

$objPHPExcel = new PHPExcel();

Hope it helps.

Sunday, August 14, 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 :
 

Browse Other Code Languages