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

  • Home
  • SEARCH
  • 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 8447953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:18:05+00:00 2026-06-10T10:18:05+00:00

I’m trying to loop through a multidimensional array, and add a new sub array.

  • 0

I’m trying to loop through a multidimensional array, and add a new sub array. My code doesn’t return any errors, but it also doesn’t add the new item.

I have the following code:

foreach ($data['switches'] as $switch) {
    foreach ($switch['atags'] as $attributelist)  {
        $nohardwareAttribFound = false;

        foreach ($attributelist as $attribute) {
            $pos =  strpos(trim($attribute),'$attr_2_');

            if ($pos !==false)   {      

                //echo 'in the loop';
                //found it.  extract and exit loop
                $modelnumber = substr(trim($attribute),8);
                $hardwaremodel = array();
                $hardwaremodel['tag'] = 'hardware_model:'.$modelnumber;
                array_push($switch['atags'],$hardwaremodel);
                print_r($switch);
                //echo '<br>=====<br>';
                $nohardwareAttribFound = true;
            }   

        }//end foreach ($attributelist
    }// end foreach ($switch['atags']

    if ($nohardwareAttribFound==false) {
        $hardwaremodel['tag'] = 'Unknown';
        array_push($switch['atags'],$hardwaremodel);
    }//end if


}// end foreach ($data['switches']

I would like the data to look like:

[atags] => Array ( 
  [0] => Array ( [tag] => $id_365 ) 
  [1] => Array ( [tag] => $typeid_8 ) 
  [2] => Array ( [tag] => $any_object ) 
  [3] => Array ( [tag] => $casd ) 
  [4] => Array ( [tag] => $unmounted ) 
  [5] => Array ( [tag] => $no_asset_tag ) 
  [6] => Array ( [tag] => $attr_2_1086 ) 
  [7] => Array ( [tag] => $untagged ) 
  [8] => Array ( [tag] => hardware_model:1086 ) ) ) 

where the last array – element [8], represents a new subarray that I’ve added. The print_r() statement looks correct, but when I loop through the results that are passed to my view, i can see that in fact, a new tag array has not been added.

Do i need to some sort of a replace instead of the array_push()?

If it’s not a good idea to modify an array while looping through it, could I simply check to see if an item exists.
how would i check if the [‘atags’] array for each switch contains a [tag] with a value that looks like “$attr_2_NNNN” where N is a number? For example, check out element 6 in the sample array above. The challenge is that it’s not always element 6, and you’re not always guaranteed that a tag will have the attr_2 value.
I know there is an in_array() function … i will try something like:

if (in_array(array('$attr_2_'), $switches['atags']))

I have a bug with the logic around the $nohardwareAttribFound variable, which i’m going to fix.
Thanks

  • 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-06-10T10:18:06+00:00Added an answer on June 10, 2026 at 10:18 am

    First of all: it is not advised to change an array or collection while you are iterating over it.

    If you really want to do it like this, then you should take $switch by reference instead of by value. Otherwise you can change $switch to whatever you want, it will not be reflected in the $data['switches'] array.

    To take $switch by reference just add the &:

    foreach ($data['switches'] as &$switch) { 
    }
    

    Check out foreach in PHP manual.

    EDIT After studying your code, I think this is what you are looking for:

    foreach ($data['switches'] as &$switch) { 
        $hardwaremodel = array();
        $hardwaremodel['tag'] = NULL; // initialize to NULL so we can check at the end of the loop if we have found a hardwaremodel or not (so we don't need that bool)
    
        foreach ($switch['atags'] as $attributelist) { 
            foreach ($attributelist as $attribute) { 
                $pos = strpos(trim($attribute), '$attr_2_'); 
                if ($pos !== false) {
                    $modelnumber = substr(trim($attribute), 8);
                    $hardwaremodel['tag'] = 'hardware_model:' . $modelnumber;
                    break;
                }
            }
            if ($hardwaremodel['tag'] !== NULL)
                break; // exit the loop because we already found a tag
        }
    
        if ($hardwaremodel['tag'] === NULL)
            $hardwaremodel['tag'] = 'hardware_model:unknown';
    
        // Note that this is a safe place to modify the $switch array
        // as we are not currently iterating it
        array_push($switch['atags'], $hardwaremodel); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.