Asked  7 Months ago    Answers:  5   Viewed   41 times

I wrote this function to get a subset of an array. Does php have a built in function for this. I can't find one in the docs. Seems like a waste if I'm reinventing the wheel.

function array_subset($array, $keys) {
    $result = array();
    foreach($keys as $key){
        $result[$key] = $array[$key];
    return $result;



array_diff_key and array_intersect_key are probably what you want.

Wednesday, March 31, 2021
answered 7 Months ago
  1. Boolean is a function. It's the function you're calling indirectly through noisy. A bit confusing, I know, because it looks like the name of a type. But in JavaScript, those initially-capped things (Boolean, Number, String, and so on) are functions. When you call Boolean (without using new), it tries to convert the argument you gave it into a boolean primitive value and returns the result. (See §15.6.1 in the spec.)

  2. f is the name of the argument in the noisy function.

Functions in JavaScript are first-class objects. You can pass them into other functions as arguments just like any other object.

When you do


There are two things going on. First:

// (In effect, we're not really creating a variable...)
var x = noisy(Boolean);

That gives us a function that, when called, will call Boolean with the argument we give it while also doing those console.log statements. This is the function you see being created in noisy (return function(arg)...);

Then we call that function:


And that's when you see the console output. Since Boolean(0) is false, you see Boolean return that value.

Here's a much simpler example:

function foo(bar) {
function testing() {
    alert("testing got called");

There, I'm passing the function testing into foo. The argument name I'm using for that within foo is bar. The line bar(); calls the function.

Wednesday, June 2, 2021
answered 5 Months ago

Thanks everyone for pointing out that there is no such built-in mechanism in C.

I tried using what @Afonso Tsukamoto suggested but I realized I needed a solution for multi-dimensional array. So I ended up writing my own function. I will put it in here in case anyone else is looking for similar answer:

void GetSlicedMultiArray4Col(int A[][4], int mrow, int mcol, int B[1][4], int sliced_mrow)
    int row, col;
    sliced_mrow = sliced_mrow - 1; //cause in C, index starts from 0
    for(row=0; row < mrow; row++)
        for (col=0; col < mcol; col++)
            if (row==sliced_mrow) B[0][col]=A[row][col];

So A is my input (original array) and B is my output (the sliced array). I call the function like this:

GetSlicedMultiArray4Col(A, A_rows, A_cols, B, target_row);

For example:

int A[][4] = {{1,2,3,4},{1,1,1,1},{3,3,3,3}};
int A_rows = 3; 
int A_cols = 4; 
int B[1][4]; //my subset
int target_row = 1;

GetSlicedMultiArray4Col(A, A_rows, A_cols, B, target_row);

This will produce a result (multidimensional array B[1][4]) that in Matlab is equal to the result of A(target_row,1:4).

I am new to C so please correct me if I'm wrong or if this code can be made better... thanks again :)

Tuesday, August 3, 2021
answered 3 Months ago

This works:

function sub_array(array $haystack, array $needle)
    return array_intersect_key($haystack, array_flip($needle));

$myArray = array(
$keys = array(

$ret = sub_array($myArray, $keys);

Friday, August 13, 2021
answered 2 Months ago

For an easier and more reusable code pass to the method the object and the index, as follows:

function getAllBefore(obj, index){
  var sum=0;
  for(var i=0; i<index; i++){

  return sum;

And call it like this:

getAllBefore(obj, 5);
Tuesday, August 31, 2021
answered 2 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 :