Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific array value where another value is "1"?

Tags:

I have an array that looks like this:

    Id = "ADA001"
    Stock: 15

The array has about 1700 records that looks the same, how would I search the array for the ID 1 and return the stock?

Edit: I will need to access the stock of each one of these 17000 records

Edit: I have been getting some help from Daniel Centore, he told me to set an arrays primary key to the id of the item and that it is equal to the stock, but I can't get it to work.

I am currently getting the data from an MySQL database and I store it in an array, like so:

       $data[] = array();
    $getdisposabletest = mysqli_query($connect, "Select id, disposable FROM products");      

while ($z = mysqli_fetch_array($getdisposabletest)) {  
    $data = $z;
}

Now when I use Daniels code that looks like this:

    $myMap = [];
foreach($data as $item) {
    $myMap[$item['id']] = $item['disposable'];
}

It doesn't return anything when I try to echo my product with the ID "ADA001"

  echo $myMap["ADA001"];

Also when I do "count($mymap)" it says its 2 records big, when it should be muuuch larger than that?

Thanks for help

like image 944
user2750279 Avatar asked Apr 13 '16 14:04

user2750279


1 Answers

I would use array_filter. Return the result of a comparitor.

$results = array_filter($targetArray, function($el) {
    return $el === 1;
});
like image 194
DevDonkey Avatar answered Oct 03 '22 22:10

DevDonkey