SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with arrays and referencing

    Hi.
    I have a problem when i need to change an array value and i dont know in advance exactly what value.
    Its for a filteringclass for a system that parses articles to an xml-feed.

    I've taken the part where im in trouble out, since it would be too much to include everything in the system.
    This is just a testcase really, so names on variables are not as in real life

    The code is commented so you can just follow it:

    PHP Code:
    // This is the key we want to change
    $path = array("info""data");

    // This is the original array
    $test = array
    (
        
    "id" => 1,
        
    "name" => "testing",
        
    "info" => array
        (
            
    "data" => "some data here yes"
        
    )
    );

    // Lets create some copies/references to work with
    $test2 $test;
    $test3 =& $test;

    foreach (
    $path as $node) {
        
    $test2 $test2[$node];
    }
    // $test2 now should contain "some data here yes"
    // So how do we change it, references naturaly falls to mind
    // So i rewrite the foreach a bit so it works like this:
    foreach ($path as $node) {
        
    $test2 $test2[$node];
        
    $test3 $test3[$node];
    }
    // Now i have $test2 just like it was, and $test3 should be a reference to $test['info']['data']
    // But wait, i have overwritten $test on the way, so now $test is useless, thus also any reference i have.
    // What i want is the following
    $test3 $test['info']['data'];

    // And nope, i cant hardcode it :/ 
    Any help would really be appreciated, im totaly stuck here.
    I am open to any solution there might be.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just skip all the loop stuff and get straight to the point:
    PHP Code:
     $test3 $test[$path[0]][$path[1]]; 
    Thank God I'm not too early!

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because i dont know how many dimensions the array will have.
    Like i said "i cant hardcode it".

  4. #4
    SitePoint Zealot annimal's Avatar
    Join Date
    May 2004
    Location
    london
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all
    Dose any one know how i can define a foreach loop as an array i cant figuer out the corect syntax
    PHP Code:
    $result = foreach ($_POST['Bar'] as $key=>$value

    echo 
    $value.'<br>';
     } 
    Thanks!!!

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is kind of hard to see what you are getting at, but perhaps:
    PHP Code:
    function recursive_foreach($target) {
        if (
    is_array($target)) {
            foreach(
    $target as $value) { recursive_foreach($value); }
        } else {
            echo 
    $target'<br>';
        }
    }

    recursive_foreach('foo');

    echo 
    '<hr>';

    recursive_foreach(array(array(array('foo'=>'bar')))); 
    ?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    SitePoint Zealot annimal's Avatar
    Join Date
    May 2004
    Location
    london
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah cool what im trying to do is turn the foreach loop
    This
    PHP Code:
    foreach ($_POST['CocktailBar'] as $key=>$value

    echo 
    $value.'<br>';
     } 

    and im trying to turn the hole of that array into $result
    Dose that make any more sence

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean something like this?

    PHP Code:
    foreach ($_POST['CocktailBar'] as $value) {
        
    $result .= $value '<br>';


  8. #8
    SitePoint Zealot annimal's Avatar
    Join Date
    May 2004
    Location
    london
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup thats exactly it man
    Give That Man Beer
    thanks verry much i was having so many problems becouse i am echoing $result in a html table and it was repeting the hole table after every value

    Thanks For your Help Dood

  9. #9
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he he he

    you could replace this code
    PHP Code:
    foreach ($_POST['CocktailBar'] as $value) {
         
    $result .= $value '<br>';
     } 
    with this
    PHP Code:
     $result implode('<br>'$_POST['CocktailBar']); 

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdev
    he he he

    you could replace this code
    PHP Code:
    foreach ($_POST['CocktailBar'] as $value) {
         
    $result .= $value '<br>';
     } 
    with this
    PHP Code:
     $result implode('<br>'$_POST['CocktailBar']); 

  11. #11
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by swdev
    PHP Code:
     $result implode('<br>'$_POST['CocktailBar']); 
    Actually this...
    PHP Code:
    $result implode('<br>'$_POST['CocktailBar']) . '<br>'


    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  12. #12
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahem:

    $result = implode('<br />', $_POST['CocktailBar']) . '<br />';



  13. #13
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see this thread got hijacked a bit.
    But i have still not found a solution to my problem.
    I've changed things abit now, most likely this cant be done with referencing so i've skipped that and just works on a copy instead and letting the function return a modified array that is correct.

    This function now returns a complete copy of the array given to it. What i need now is to figure out how to change the value i want to change.
    It feels like im codeblinded with this problem now, im sure its quite simple but i just cant see what is wrong.

    Some revision would be nice.

    (As you can see im building up a copy of the original array with the change-value replaced.)

    PHP Code:
    // Path is a path in the array. I think its quite self explaining what i want to change
    $path = array("info""data");
    // This is the original array
    $test = array
    (
        
    "id" => 1,
        
    "name" => "testing",
        
    "info" => array
        (
            
    "data" => "some data here yes"
        
    )
    );


    // This is how far i got with a function to deal with this

    function arrayValueReplace($path, &$arr$replace) {
            
    // Create a copy of the array to work on
            
    $array $arr;
            foreach (
    $array as $key => $ar) {
                    
    // This is where we are in the path
                    
    $current array_slice($path01);
                    
    // Some recursivness
                    
    if (is_array($ar[$current])) {
                            
    $build[$key] = arrayValueReplace($path$ar[$current], $replace);
                    }
                    else {
                            
    // This is were i need to figure out if we are on the right spot and to change the value with the replacevalue
                            
    if ($ar == $current[0] && count($path) == 1) {
                                    
    $build[$key] = $replace;
                            }
                            else {
                                    
    $build[$key] = $ar;
                            }
                    }
                    
    // Remove the portion of path we are done with
                    
    $path array_slice($path1);
            }
            
    // Return the copy we are building up
            
    return $build;
    }

    // In order to test the restults:
    echo "This is the original array: <pre>";
    print_r($test);
    echo 
    "</pre> This is the final array which should have the correct value changed: <pre>";
    print_r(arrayValueReplace($path$test"Nice new text"));
    echo 
    "</pre>"
    This returns two equal arrays. So i have succesfully rebuilt the array, but not changed the value.

  14. #14
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see again, meaning i figured the problem out.
    Like i suspected it was just codeblindness. I was doing some weird things in there :/

  15. #15
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you looked at array_walk_recursive?
    Sounds like it could make your life a lot simpler
    Garcia


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •