Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:36:01+00:00 2026-05-13T19:36:01+00:00

Is there any way in php wherein I can get n level keys for

  • 0

Is there any way in php wherein I can get n level keys for multi-dimensional array in php ?

Here is my associative array and as output I want an array which would contain all the values for keys object_id as you can there from the structure itself there are many nested levels for object_id and so how can I get all the values for keys object_id ?

   array
  'cart' => 
    array
      12061 => 
        array
          'object_id' => string '12061' (length=5)
          'discriminator' => string 'SimpleProductOffering' (length=21)
          'spec' => 
            array
              100012061 => 
                array
                  'object_id' => string '100012061' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      'MAIN_CPE' => 
                        array
                          'object_id' => string '1000000000015' (length=13)
                          'is_configurable' => string '0' (length=1)
                          'trait_value' => 
                            array
                              10001 => 
                                array
                                  'object_id' => string '10001' (length=5)
                                  'collateral' => 
                                    array
                                      empty
                          'collateral' => 
                            array
                              empty
          'offer_type' => null
          'price' => 
            array
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      0 => string '12876' (length=5)
                      1 => string '12894' (length=5)
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              empty
          'family' => 
            array
              0 => string 'TV Subscription' (length=24)
          'relationship' => 
            array
              'CHILDREN' => 
                array
                  'object_id' => string '1206102000' (length=10)
                  'min' => string '0' (length=1)
                  'max' => string '1000000' (length=7)
                  'related_offer' => 
                    array
                      0 => string '10410' (length=5)
                      1 => string '10411' (length=5)
              'REQUIREMENTS' => 
                array
                  'object_id' => string '1206104000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '11950' (length=5)
                      1 => string '11990' (length=5)
              'EXCLUSIONS' => 
                array
                  'object_id' => string '1206101000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12062' (length=5)
                      1 => string '12063' (length=5)
              'ALTERNATIVES' => 
                array
                  'object_id' => string '1206105000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12263' (length=5)
              'BUNDLE_ITEMS' => 
                array
                  'object_id' => string '1206106000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12062' (length=5)
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
              'cancellation_period' => string 'ALWAYS' (length=6)
              'notice_period' => int 3
              'rollover_period' => int 2
              'right_of_wd_period' => int 1
          'collateral' => 
            array
              empty
      10017 => 
        array
          'object_id' => string '10017' (length=5)
          'spec' => 
            array
              100010017 => 
                array
                  'object_id' => string '100010017' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      empty
          'offer_type' => null
          'price' => 
            array
              300306 => 
                array
                  'object_id' => string '300306' (length=6)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12894 => 
                array
                  'object_id' => string '12894' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      0 => string '12876' (length=5)
                      1 => string '12894' (length=5)
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              0 => null
          'family' => 
            array
              empty
          'relationship' => 
            array
              'EXCLUSIONS' => 
                array
                  'object_id' => string '1001701000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '11893' (length=5)
                      14 => string '12305' (length=5)
                      15 => string '12306' (length=5)
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'MONTHLY' (length=7)
                  1 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
                  1 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'EMAIL' (length=5)
                  1 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
              'cancellation_period' => string 'ALWAYS' (length=6)
              'notice_period' => int 3
              'rollover_period' => int 2
              'right_of_wd_period' => int 1
          'collateral' => 
            array
              empty
  0 => 
    array
      11990 => 
        array
          'object_id' => string '11990' (length=5)
          'discriminator' => string 'SimpleProductOffering' (length=21)
          'spec' => 
            array
              100011990 => 
                array
                  'object_id' => string '100011990' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      empty
          'offer_type' => null
          'price' => 
            array
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      10017 => 
                        array
                          'object_id' => string '10017' (length=5)
                          'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                          'price_alteration' => 
                            array
                              empty
                          'collateral' => 
                            array
                              empty
                      12894 => 
                        array
                          'object_id' => string '12894' (length=5)
                          'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                          'price_alteration' => 
                            array
                              empty
                          'collateral' => 
                            array
                              empty
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              empty
          'family' => 
            array
              0 => string 'CATV' (length=4)
          'relationship' => 
            array
              empty
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
          'collateral' => 
            array
              empty

Output should be an array which would contain all the values for keys = object_id. Kindly advise ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:36:01+00:00Added an answer on May 13, 2026 at 7:36 pm

    Dereleased’s solution will be faster I guess (as it uses internal loops), mine can also deal with array values for object_id keys. Your tradeoff 😉


    function find_all($needle, array $haystack, array &$result = null) {
        // This is to initialize the result array and is only needed for
        // the first call of this function
        if(is_null($result)) {
            $result = array();
        }
        foreach($haystack as $key => $value) {
            // Check whether the key is the value we are looking for. If the value
            // is not an array, add it to the result array.
            if($key === $needle && !is_array($value)) {
                $result[] = $value;
            }
            if(is_array($value)) {
                // If the current value is an array, we perform the same
                // operation with this 'subarray'.
                find_all($needle, $value, $result);
            }
        }
        // This is only needed in the first function call to retrieve the results
        return $result;
    }
    

    As you can see, the result array is given to every call of the function as reference (denoted by the &). This way, every recursive call of this function has access to the same array and can just add a find.

    You can do:

    $values = find_all('object_id', $array);
    

    It gives me for your array:

    Array
    (
        [0] => 12061
        [1] => 100012061
        [2] => 1000000000015
        [3] => 10001
        [4] => 12862
        [5] => 12876
        [6] => 1206102000
        [7] => 1206104000
        [8] => 1206101000
        [9] => 1206105000
        [10] => 1206106000
        [11] => 10017
        [12] => 100010017
        [13] => 300306
        [14] => 12894
        [15] => 12862
        [16] => 12876
        [17] => 1001701000
        [18] => 11990
        [19] => 100011990
        [20] => 12862
        [21] => 12876
        [22] => 10017
        [23] => 12894
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can return a date using PHP that is the
Is there any way in which a PHP powered web app can store data
Is there any way to have PHP automatically call a function, before a script
Is there any way to force PHP to blow up (error, whatever) if I
Is there any way, from a PHP script called from mod_php with apache, to
Is there any way to include php file using require and calling a php
Is there any way to use PHP code to query a DNSBL (block list)
Is there any way to remove the EXIF data from a JPG using PHP?
Is there any way to open a new window or new tab using PHP
Is there any way to display video(snapshot) as as a image in PHP?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.