skip to Main Content

I have an array with multiple array, and in each array one of the fields is another array, see below. I need to find the value for "First name", in the array that has an ‘item_id’ of value 177. How can I do that?

array(1) { 
[0]=> array(4) 
{ 
["product_id"]=> int(62) 
["item_id"]=> int(177) 
["quantity"]=> int(1) 
["options"]=> array(5) { 
[0]=> array(4) { 
["field_id"]=> string(13) "655259ef26f01" 
["label"]=> string(11) "First name:" 
["value"]=> string(4) "test" ["type"]=> string(4) "text" } 
[1]=> array(4) { ["field_id"]=> string(13) "65525a47553c3" 
["label"]=> string(10) "Last name:" 
["value"]=> string(4) "test" ["type"]=> string(4) "text" } 
[2]=> array(4) { 
["field_id"]=> string(13) "65525a658669b" 
["label"]=> string(15) "E-mail address:" 
["value"]=> string(17) "[email protected]" 
["type"]=> string(5) "email" } 
[3]=> array(4) { ["field_id"]=> string(13) "65525a964be34" 
["label"]=> string(27) "Language for questionnaire:" 
["value"]=> string(6) "German" 
["type"]=> string(6) "select" }
} } 

[1]=> array(4) { 
["product_id"]=> int(62) 
["item_id"]=> int(182) 
["quantity"]=> int(1) 
["options"]=> array(7) { 
[0]=> array(4) { 
["field_id"]=> string(13) "655259ef26f01" 
["label"]=> string(11) "First name:" 
["value"]=> string(4) "test" 
["type"]=> string(4) "text" 
} 
[1]=> array(4) { 
["field_id"]=> string(13) "65525a47553c3" 
["label"]=> string(10) "Last name:" 
["value"]=> string(4) "test" 
["type"]=> string(4) "text" 
} 
[2]=> array(4) { 
["field_id"]=> string(13) "65525a658669b" 
["label"]=> string(15) "E-mail address:" 
["value"]=> string(17) "[email protected]" 
["type"]=> string(5) "email" 
} 
[3]=> array(4) { 
["field_id"]=> string(13) "65525a7bb053f" 
["label"]=> string(46) "Send copy of the report to this email address:" 
["value"]=> string(17) "[email protected]" 
["type"]=> string(5) "email" 
} 
[4]=> array(4) { 
["field_id"]=> string(13) "65525a964be34" 
["label"]=> string(27) "Language for questionnaire:" 
["value"]=> string(7) "Chinese" ["type"]=> string(6) "select" }
} } }

I have tried several things, one of them this:

$fname = $customs['item_id'][$itemID]['options']['field_id']['655259ef26f01']['value'];

2

Answers


  1. You can use a function that searches through the $custom array, then if the item_id matches what is passed, look through the options sub-array and then pluck out the value when field_id matches:

    <?php
    
    /*
    
    Question Author: Pete
    Question Answerer: Jacob Mulquin
    Question: Find value in multidimensional array based on key
    URL: https://stackoverflow.com/questions/77506541/find-value-in-multidimensional-array-based-on-key
    Tags: , php, multidimensional-array, find
    
    */
    
    $customs = [
        [
            'product_id' => 61,
            'item_id' => 177,
            'quantity' => 1,
            'options' => [
                [
                    'field_id' => '655259ef26f01',
                    'label' => 'First name:',
                    'value' => 'test firstname1',
                    'type' => 'text'
                ],
                [
                    'field_id' => '65525a47553c3',
                    'label' => 'Last name:',
                    'value' => 'test lastname1',
                    'type' => 'text'
                ]
            ]
        ],
        [
            'product_id' => 60,
            'item_id' => 177,
            'quantity' => 1,
            'options' => [
                [
                    'field_id' => '655259ef26f01',
                    'label' => 'First name:',
                    'value' => 'test firstname1 again',
                    'type' => 'text'
                ],
                [
                    'field_id' => '65525a47553c3',
                    'label' => 'Last name:',
                    'value' => 'test lastname1',
                    'type' => 'text'
                ]
            ]
        ],
        [
            'product_id' => 62,
            'item_id' => 182,
            'quantity' => 1,
            'options' => [
                [
                    'field_id' => '655259ef26f01',
                    'label' => 'First name:',
                    'value' => 'test firstname2',
                    'type' => 'text'
                ],
                [
                    'field_id' => '65525a47553c3',
                    'label' => 'Last name:',
                    'value' => 'test lastname2',
                    'type' => 'text'
                ]
            ]
        ]
    ];
    
    function search_item_array($search, $item_id, $field_id)
    {
        $found = [];
        foreach ($search as $record) {
            if ($record['item_id'] !== $item_id)
                continue;
    
            foreach ($record['options'] as $option) {
                if ($option['field_id'] === $field_id)
                    $found[] = $option['value'];
            }
        }
        return $found;
    }
    
    $output = search_item_array($customs, 177, '655259ef26f01');
    var_dump($output);
    

    Yields:

    array(2) {
      [0]=>
      string(15) "test firstname1"
      [1]=>
      string(21) "test firstname1 again"
    }
    
    Login or Signup to reply.
  2. If you just want one field, the using foreach makes sense. If you are going to fetch several field values it can help to convert your fields array into an indexed array with the field_id as the index. This can easily be done using array_column()

    So something like

    foreach ($customs as $product) {
        if ($product['item_id'] == 177) {
            // Index the options by the field_id column.
            $product['options'] = array_column($product['options'], null, 'field_id');
    
            echo $product['options']['655259ef26f01']['value'] . PHP_EOL; // first name
            echo $product['options']['65525a47553c3']['value'] . PHP_EOL; // last_name
        }
    }
    

    If you use this data on a regular basis, it may be useful to create the original data with some useful index (perhaps having product_id at the top level and field_id for the options) to help in fetching the values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search