Fun With Arrays

How many ways can you show how to get the name of the products and quantity that are “in stock” meaning one or more.

$stock = [
    	 ['name' => 'Phone', 'qty' => 0],
    	 ['name' => 'Notebook', 'qty' => 1],
    	 ['name' => 'SDD Drive', 'qty' => 0],
     	 ['name' => 'HDD Drive', 'qty' => 3]
];
1 Like

Translated directly from JavaScript:

<?php
function in_stock($items) {
  return $items['qty'] > 0;
}

$stock = [
  ['name' => 'Phone', 'qty' => 0],
  ['name' => 'Notebook', 'qty' => 1],
  ['name' => 'SDD Drive', 'qty' => 0],
  ['name' => 'HDD Drive', 'qty' => 3]
];

print_r(array_filter($stock, 'in_stock'));

Output:

Array
(
    [1] => Array
        (
            [name] => Notebook
            [qty] => 1
        )

    [3] => Array
        (
            [name] => HDD Drive
            [qty] => 3
        )

)

I’ll be interested to see other answers.

1 Like

@James_Hibbard,
That’s pretty close to what I was going to post so I will throw something else out.

$stock = [
    ['name' => 'Phone', 'qty' => 0],
    ['name' => 'Notebook', 'qty' => 1],
    ['name' => 'SDD Drive', 'qty' => 0],
    ['name' => 'HDD Drive', 'qty' => 3]
];

foreach ($stock as $v) {
    if (($v['qty'])) {
        $a[$v['name']] = $v['qty'];
    }
}
print_r($a);

Output

Array
(
    [Notebook] => 1
    [HDD Drive] => 3
)

@James_Hibbard,

What I had in mind was an anonymous function. It has the exact output as your example.

$stock = [
    ['name' => 'Phone', 'qty' => 0],
    ['name' => 'Notebook', 'qty' => 1],
    ['name' => 'SDD Drive', 'qty' => 0],
    ['name' => 'HDD Drive', 'qty' => 3]
];

$inStock = array_filter($stock, function ($items) {return $items['qty'] > 0;});

print_r($inStock);

Interesting. I was unaware that you could pass in an anonymous function in PHP.

In JavaScript you could also do this:

stock = [
  {'name': 'Phone', 'qty':  0},
  {'name': 'Notebook', 'qty': 1},
  {'name': 'SDD Drive', 'qty': 0},
  {'name': 'HDD Drive', 'qty': 3},
];

const inStock =  stock.map(item => item.qty > 0 ? item : null);
console.log(inStock.filter(Boolean));

How might that translate to PHP?

array_filter only requires the first parameter.
If not handed a function, will discard all FALSE values in an array. NULL is false-equivalent when coerced to boolean.

Not with a Multidimensional array as in the example.

the array presented in James’ post #5 isnt a multidimensional array - it’s a single dimensional array of objects. There is a difference there. But, even in a multidimensional array, if you NULL’d elements of the top level array, array_filter would dump them if fed no callback.

I can’t actually see your OP due to ignored content, so I was just responding based off James’ post.

EDIT: I should make it more clear in my original post, but it’s too old to edit. The original post should effectively have read:

@James_Hibbard : array_filter only requires the first parameter.
If not handed a function, will discard all FALSE values in an array. NULL is false-equivalent when coerced to boolean.
Thus, PHP’s version of your code is:

$stock = [
  (object) ['name': 'Phone', 'qty':  0],
  (object) ['name': 'Notebook', 'qty': 1],
  (object) ['name': 'SDD Drive', 'qty': 0],
  (object) ['name': 'HDD Drive', 'qty': 3],
];

$inStock =  array_map(function($item) { return $item->qty > 0 ? $item : null; },$stock);
print_r(array_filter($inStock));
1 Like

@m_hutley,

Looks like you accidentally used the Javascript code for the Php example but I get it. Good job on showing another example. :+1:

Getting into OOP opens the door for a few more options. Interested to see what else people can come up with.

@m_hutley’s example with the Php Array

 $stock = [
    (object) ['name' => 'Phone', 'qty' => 0],
    (object) ['name' => 'Notebook', 'qty' => 1],
    (object) ['name' => 'SDD Drive', 'qty' => 0],
    (object) ['name' => 'HDD Drive', 'qty' => 3]
];
$inStock = array_map(function ($item) {return $item->qty > 0 ? $item : null;}, $stock);
print_r(array_filter($inStock));
1 Like

shockingly overboiled

<?php

$stock = [
    ['name' => 'Phone', 'qty' => 0],
    ['name' => 'Notebook', 'qty' => 1],
    ['name' => 'SDD Drive', 'qty' => 0],
    ['name' => 'HDD Drive', 'qty' => 3]
];

print_r(array_filter(array_combine(array_column($stock, 'name'), array_column($stock, 'qty'))));
2 Likes