SitePoint Sponsor

User Tag List

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

    Question How to convert matrix?

    I have an array like this:
    PHP Code:
    $start = array( 
       array(
    'a''b''c'), 
       array(
    'b''c'), 
       array(
    'a''c'
    ); 
    and I want convert to:
    PHP Code:
    $end = array( 
       array(
    'a' => 1'b' => 1'c' => 1), 
       array(
    'a' => 0'b' => 1'c' => 1), 
       array(
    'a' => 1'b' => 0'c' => 1
    ); 
    It means that each element in $start will be key in $end. If key appears in $start, value is 1 and 0 if not. What do all you think?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi there,

    Here's my solution.
    I'm still learning PHP (normally I work in Ruby), so I would be really grateful for any constructive criticism / pointers the rest of you could give me.

    PHP Code:
    <?php
    function convertArray($start){
      
    $end = array();
      
    $unique_keys = array();
      
      
    // Get a list of all possible keys
      
    for ($row 0$row count($start); $row++){
        for (
    $col 0$col count($start[$row]); $col++){
          
    array_push($unique_keys$start[$row][$col]);
        }
      }
      
    $unique_keys array_unique($unique_keys);
      
    sort($unique_keys);
      
      
    // Create new array using these keys
      
    for ($row 0$row count($start); $row++){
        for (
    $col 0$col count($start[$row]); $col++){
          
    $temp_array = array();
          foreach (
    $unique_keys as $key){
            
    $temp_array[$key] = in_array($key$start[$row])? '1' '0';
          }
        }
        
    array_push($end$temp_array);
      }
      return(
    $end);
    }


    $start = array(  
       array(
    'a''b''c'),  
       array(
    'b''c'),  
       array(
    'a''c')  
    );

    print_r(convertArray($start));
    This outputs:

    Code:
    Array ( [0] => Array ( [a] => 1 [b] => 1 [c] => 1 ) [1] => Array ( [a] => 0 [b] => 1 [c] => 1 ) [2] => Array ( [a] => 1 [b] => 0 [c] => 1 ) )

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Do you know for sure that $start (or $end) will always contain a fixed set of keys/values? Or a set of fixed keys/values you can divine/compute?

  4. #4
    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 Pullo View Post
    Hi there,

    Here's my solution.
    I'm still learning PHP (normally I work in Ruby), so I would be really grateful for any constructive criticism / pointers the rest of you could give me.
    Hi! I'm still learning PHP too. Your solution is good. Thank a lot!

    Quote Originally Posted by Cups View Post
    Do you know for sure that $start (or $end) will always contain a fixed set of keys/values? Or a set of fixed keys/values you can divine/compute?
    $start (or $end) is not contain a fixed set of keys and values, it's based on database. Each subarray is a transaction (or an order).

    And here is my solution:

    main.php
    PHP Code:
    for($i=0; $<count($database); $i++){
         
    $transaction[] = getTransaction($candidates$database);

    PHP Code:
    <?php
        
    function minus_sets($a$b){
            
            
    // number candidates in database
            
    $number_cans count($a);
            
    // number transactions database
            
    $number_trans count($b);
            
            for(
    $i=0$i<count($a); $i++){
                
    $same0;
                for(
    $j=0$j<count($b); $j++){
                    if(
    $a[$i]==$b[$j]) $same1;
                }
                
                if(!
    $same){
                    
    $tmp $tmp 1;
                    
    $c[$tmp-1] = $a[$i];
                }
            }
            
            return 
    $c;
        }
        
        
    //demo
        //$a = array(1, 2, 3, 4);
        //$b = array(2, 3, 4);
        //$minus = minus_set($a, $b);
        //print_r($minus);
        //result is Array ( [0] => 1 )
    ?>
    PHP Code:
    <?php

        
    include('minus_sets.php');
        
        function 
    getTransaction($candidates$database){
            
    // $candidates minus $database to take elements not in $database
            
    $tmp minus_sets($candidates$database);
            
            for(
    $i=0$i<count($candidates); $i++){
                if(!
    in_array($candidates[$i], $tmp)){
                    
    $transaction[$candidates[$i]] = 1;
                } else {
                    
    $transaction[$candidates[$i]] = 0;
                }
            }
            
            return 
    $transaction;
        }
        
        
        
    //$candidates = array('a', 'b', 'c', 'd', 'e');
        //$database = array('a', 'b', 'c');
        
        //$transaction = getTransaction($candidates, $database);
        //print_r($transaction);
    // result is Array ( [a] => 1 [b] => 1 [c] => 1 [d] => 0 [e] => 0 )
    ?>
    What do you think about my solution?

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try "in_array"

    there is full documentation at php.net

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Maybe I have not understood your problem exactly, and re-using your test case (thanks, I like that, very thoughtful).

    PHP Code:
    $a = array(1234); 
    $b = array(234); 

        
    //$minus = minus_set($a, $b); 
        //print_r($minus); 
        //result is Array ( [0] => 1 ) 

    $c array_diff($a$b);

    var_dump($c);

    // 0 => 1 

  7. #7
    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 localhost8080 View Post
    try "in_array"

    there is full documentation at php.net
    Thank for your opinion!

    Quote Originally Posted by Cups View Post
    Maybe I have not understood your problem exactly, and re-using your test case (thanks, I like that, very thoughtful).

    PHP Code:
    $a = array(1234); 
    $b = array(234); 

        
    //$minus = minus_set($a, $b); 
        //print_r($minus); 
        //result is Array ( [0] => 1 ) 

    $c array_diff($a$b);

    var_dump($c);

    // 0 => 1 
    My idea is like Pullo's idea but I don't use array_push function so it may be longer than him. Thank!

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    I have a feeling my version might be reducable, but meh...

    (Note: I changed the input variable to $in)
    PHP Code:
    $keys array_unique(array_merge_recursive($in))[0]; //Find all keys
    $values array_combine($keys,array_pad(array(),count($keys),0)); //Fill with 0's. Creating a 'row' for the matrix.
    $replace array_map(function($item) { return array_combine($item,array_pad(array(),count($item),1)); }, $in); //All my 1's.
    $out array_pad(array(),count($in),$values); //Matrix of 0's.
    $out array_replace_recursive($out,$replace); //Put the 1's in the matrix. 
    PHP belches array-to-string issues at me on my Replace line, but i'm not... entirely sure where i'm converting a string in there... anyway, it works.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Nice one StarLion. That's really neat!
    I hadn't heard of array_replace_recursive or array_merge_recursive, so I've just been reading the docs for those.
    Now my head is ready to explode


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
  •