SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I can't unset element in array

    I have a code as below and I can't remove element for each loop. Do you know why?

    PHP Code:
    <?php
        $data 
    = array(
            array(
    'A''C'),
            array(
    'B''C''E'),
            array(
    'A''B''C''E'),
            array(
    'B''E')
        );
        
        
    $fi = array( 'C' => 3'B' => 3'E' => 3'A' => 2);
        
    $tmp = array();
        
    $mfi = array();
            
            for(
    $row=0$row<count($data); $row++){
                
    $tmp $fi;
                
    // each loop, remove element not in $data[$row]
                // and put $tmp into $mfi
                
    foreach($fi as $key=>&$value){
                    if(!
    in_array($tmp[$key], $data[$row])) unset($tmp[$key]);
                }
                
    array_push($mfi$tmp);
            }
            
            
    print_r($mfi);
    ?>

  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code is a bit confusing, but it looks like your array $fi has the letters as keys and numbers as values. So when you check if the value is in $data, since the arrays in $data have no numbers, it will never remove anything.

    You could always use $key, instead of $tmp[$key].
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  3. #3
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana View Post
    Your code is a bit confusing, but it looks like your array $fi has the letters as keys and numbers as values. So when you check if the value is in $data, since the arrays in $data have no numbers, it will never remove anything.

    You could always use $key, instead of $tmp[$key].
    So, how can I rewrite it?

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greedyman View Post
    So, how can I rewrite it?
    I told you in my post. I believe yu should be able to use $key instead of $tmp[$key] to check if it is in the array.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  5. #5
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana View Post
    I told you in my post. I believe yu should be able to use $key instead of $tmp[$key] to check if it is in the array.
    Oh! So sorry. Thank a lot!


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
  •