PHP Get values from array using a specific value

Issue

I need to get values from a multidimensional array using a specific value in PHP.

For example, given this array I would like to extract data based on index [1] equal to ‘MUFA-D’
then use the other values found in [3],[4] which would be ‘141’ and ‘Purity FM Synthetic’

Array
(
    [0] => Array
        (
            [0] => CHI
            [1] => MUFA-D
            [2] => 1
            [3] => 141
            [4] => Purity FM Synthetic
            [5] => 5
            [6] => Lubricants
        )

    [1] => Array
        (
            [0] => CHI
            [1] => BRD1-IS
            [2] => 1
            [3] => 146
            [4] => Food Grade Silicon
            [5] => 3
            [6] => Lubricants
        )

    [2] => Array
        (
            [0] => CHI
            [1] => BRD1-MC
            [2] => 1
            [3] => 145
            [4] => SAE 140 Oil
            [5] => 1
            [6] => Lubricants
        )


    [3] => Array
        (
            [0] => CHI
            [1] => JBC-BAK-B
            [2] => 1
            [3] => 141
            [4] => Purity FM Synthetic
            [5] => 5
            [6] => Lubricants
        )

)

Solution

A simple loop over the array, checking for the first occurance that matches and then returning the values will suffice for this

$in = [
    ['CHICAGO', 'MUFA-D', 1, 141, 'Purity FM Synthetic', 5, 'Lubricants'],
    ['CHICAGO', 'BRD1-IS', 1, 146, 'Food Grade Silicon', 3, 'Lubricants'],
    ['CHICAGO', 'BRD1-MC', 1, 145, 'SAE 140 Oil', 1, 'Lubricants'],
    ['CHICAGO', 'JBC-BAK-B', 1, 141, 'Purity FM Synthetic', 5, 'Lubricants']
];

function getStuff($in, $lookIn, $forValue, array $gets)
{
    foreach ($in as $arr){
        if ( $arr[$lookIn] == $forValue) {
            $res = [];
            foreach ($gets as $get){
                $res[] = $arr[$get];
            }
            return $res;
        }
    }
}

$result = [];
$result[] = getStuff($in, 1, 'MUFA-D', [3,4]);
print_r($result);

RESULT

Array
(
    [0] => Array
        (
            [0] => 141
            [1] => Purity FM Synthetic
        )

)

To return multiple finds, just move the positioning of the return

$in = [ 
    ['CHICAGO', 'MUFA-D', 1, 141, 'Purity FM Synthetic', 5, 'Lubricants'], 
    ['CHICAGO', 'MUFA-D', 1, 141, 'Test Part', 3, 'Oil'], 
    ['CHICAGO', 'BRD1-IS', 1, 146, 'Food Grade Silicon', 3, 'Lubricants'], 
    ['CHICAGO', 'BRD1-MC', 1, 145, 'SAE 140 Oil', 1, 'Lubricants'], 
    ['CHICAGO', 'JBC-BAK-B', 1, 141, 'Purity FM Synthetic', 5, 'Lubricants'] 
    ];

function getStuff($in, $lookIn, $forValue, array $gets)
{
    $res = [];
    foreach ($in as $arr){
        if ( $arr[$lookIn] == $forValue) {

            foreach ($gets as $get){
                $res[] = $arr[$get];
            }
        }
    }
    return $res;
}

$result = [];
$result = getStuff($in, 1, 'MUFA-D', [3,4]);
print_r($result);

RESULT

Array
(
    [0] => 141
    [1] => Purity FM Synthetic
    [2] => 141
    [3] => Test Part
)

Or this would make a more usable result array

function getStuff($in, $lookIn, $forValue, array $gets)
{
    $res = [];
    foreach ($in as $arr){
        if ( $arr[$lookIn] == $forValue) {
            $t = [];
            foreach ($gets as $get){
                $t[] = $arr[$get];
            }
            $res[] = $t;
        }
    }
    return $res;
}

$result = [];
$result = getStuff($in, 1, 'MUFA-D', [3,4]);
print_r($result);

RESULT

Array
(
    [0] => Array
        (
            [0] => 141
            [1] => Purity FM Synthetic
        )
    [1] => Array
        (
            [0] => 141
            [1] => Test Part
        )
)

Answered By – RiggsFolly

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published