user3041764
user3041764

Reputation: 839

Get key in multidimension array

I need to create array with carriers names and ids binded to them. And then I want execute some like get_carrier_name_by_id(id) what will return carrier name.

$carriers = array (
    "DPD" => array(1,2,3),
    "DPD Pobranie" => array(4,5,6),
    "Poczta-Polska" => array(7,8,9),
    "Poczta-Polska pobranie" => array(10,11,12),
    "Paczkomat" => array(13,14,15),
    "Paczkomat pobranie" => array(16,17,18),
    "Osobisty" => array(19,20,21),
    "UPS" => array(22,23,24),
    "UPS pobranie" => array(25,26,27)
);

Is this properly way to keep data?

Upvotes: 1

Views: 83

Answers (3)

fusion3k
fusion3k

Reputation: 11689

You can easily obtain Carrier Name using array_filter(), in_array() and key():

$key = 4;
$carrier = key( array_filter( $carriers, function( $row ) use( $key ) { return in_array( $key, $row ); } ) );
echo $carrier;

will output:

DPD Pobranie

Basically, you have to filter $carriers array to matched $key. You can use array_filter with an anonymous function: array_filter pass each array element to the called function and if the function returns a value evaluable as True, the element is returned in the filtered array, otherwise it is discarded. In your case, we check if in element items there is the requested key.

Then, we use key() to return the key of currently pointed element of filtered array: the array is just created, so the pointer is at the start of array.

You can adapt this script to a function, if you want:

function CarrierByID( $carriers, $key )
{
    return key( ... );
}

Upvotes: 1

Chetan Ameta
Chetan Ameta

Reputation: 7896

have a look at array iterator.

look below solution using iterator:

function get_carrier_name_by_id($id)
{
    $carriers = array(
        "DPD" => array(1, 2, 3),
        "DPD Pobranie" => array(4, 5, 6),
        "Poczta-Polska" => array(7, 8, 9),
        "Poczta-Polska pobranie" => array(10, 11, 12),
        "Paczkomat" => array(13, 14, 15),
        "Paczkomat pobranie" => array(16, 17, 18),
        "Osobisty" => array(19, 20, 21),
        "UPS" => array(22, 23, 24),
        "UPS pobranie" => array(25, 26, 27)
    );

    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($carriers));

    foreach ($it as $key => $val) {
        $key = $it->getSubIterator($it->getDepth() - 1)->key();
        if($val == $id){
            return $key;
        }
    }
}

echo get_carrier_name_by_id(3); //Output: DPD
echo get_carrier_name_by_id(12); //Output: Poczta-Polska pobranie
echo get_carrier_name_by_id(24); //Output: UPS

Upvotes: 1

aslawin
aslawin

Reputation: 1981

Try this:

function get_carrier_name_by_id($id)
{
    $carriers = array (
        "DPD" => array(1,2,3),
        "DPD Pobranie" => array(4,5,6),
        "Poczta-Polska" => array(7,8,9),
        "Poczta-Polska pobranie" => array(10,11,12),
        "Paczkomat" => array(13,14,15),
        "Paczkomat pobranie" => array(16,17,18),
        "Osobisty" => array(19,20,21),
        "UPS" => array(22,23,24),
        "UPS pobranie" => array(25,26,27)
    );

    foreach($carriers as $name => $ids)
        if(in_array($id, $ids))
            return $name;

    return null;
}

echo '<pre>';
var_dump(get_carrier_name_by_id(122));
var_dump(get_carrier_name_by_id(12));

Outputs of var_dumps:

NULL
string(22) "Poczta-Polska pobranie"

Upvotes: 1

Related Questions