Need to extract data from arrays, please help.

Discussion in 'PHP' started by Sixsigma84, Aug 29, 2010.

  1. #1
    Hi,

    I'm trying to extract data from some arrays, but they contain arrays within arrays and it's a little complex to extract the numbers I need without having to analize each independed nested array.

    I thought taht maybe someone here was able to extract the information I'm needing, which is all the numbers contained.

    Example array:

    
    Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) [5] => Array ( ) [6] => Array ( ) [7] => Array ( ) [8] => Array ( ) [9] => Array ( ) [10] => Array ( [0] => 520723481 [1] => 699206543 [2] => 1340452035 ) [11] => Array ( ) [12] => Array ( [0] => 808740283 ) [13] => Array ( ) [14] => Array ( ) [15] => Array ( ) [16] => Array ( ) [17] => Array ( ) [18] => Array ( [0] => 520723481 [1] => 757042890 [2] => 699206543 ) [19] => Array ( ) [20] => Array ( ) [21] => Array ( ) [22] => Array ( [0] => 814159884 ) [23] => Array ( ) [24] => Array ( ) [25] => Array ( [0] => 1340452035 ) [26] => Array ( [0] => 594112108 ) [27] => Array ( ) )
    Code (markup):
    (serialized array, if someone needs to test)
    a:28:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}i:3;a:0:{}i:4;a:0:{}i:5;a:0:{}i:6;a:0:{}i:7;a:0:{}i:8;a:0:{}i:9;a:0:{}i:10;a:3:{i:0;i:520723481;i:1;i:699206543;i:2;i:1340452035;}i:11;a:0:{}i:12;a:1:{i:0;i:808740283;}i:13;a:0:{}i:14;a:0:{}i:15;a:0:{}i:16;a:0:{}i:17;a:0:{}i:18;a:3:{i:0;i:520723481;i:1;i:757042890;i:2;i:699206543;}i:19;a:0:{}i:20;a:0:{}i:21;a:0:{}i:22;a:1:{i:0;i:814159884;}i:23;a:0:{}i:24;a:0:{}i:25;a:1:{i:0;i:1340452035;}i:26;a:1:{i:0;i:594112108;}i:27;a:0:{}}
    Code (markup):
    Thanks!!
     
    Sixsigma84, Aug 29, 2010 IP
  2. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    i believe that the only way to traverse along an array is using the indeces. if you know the index, it can be traversed.
    but yeah, in your case, a check can be placed. e.g.

    
    <?php 
    function get_item( $ar, $i ) {
    
        if ( is_array( $i ) )
            if ( count($i) > 1 && is_array( $ar[ $i[0] ] ) )
                return get_item( $ar[ $i[0] ], array_shift($i) );
            else 
                return get_item( $ar, $i[0] );
    
        if ( !isset( $ar[$i] ) ) return;
    
        if ( func_num_args() == 2 )
            return $ar[$i];
    
        return get_item( $ar[$i], array_slice( func_get_args(), 2 ) );
    }
    ?>
    
    PHP:
    this function is not tested and is designed to be used in 2 ways:
    
    $ar = my_unserialize( "a:28:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}i:3;a:0:{}i:4;a:0:{}i:5;a:0:{}i:6;a:0:{}i:7;a:0:{}i:8;a:0:{}i:9;a:0:{}i:10;a:3:{i:0;i:520723481;i:1;i:699206543;i:2;i:1340452035;}i:11;a:0:{}i:12;a:1:{i:0;i:808740283;}i:13;a:0:{}i:14;a:0:{}i:15;a:0:{}i:16;a:0:{}i:17;a:0:{}i:18;a:3:{i:0;i:520723481;i:1;i:757042890;i:2;i:699206543;}i:19;a:0:{}i:20;a:0:{}i:21;a:0:{}i:22;a:1:{i:0;i:814159884;}i:23;a:0:{}i:24;a:0:{}i:25;a:1:{i:0;i:1340452035;}i:26;a:1:{i:0;i:594112108;}i:27;a:0:{}}" );
    
    echo get_item( $ar, 10, 2 ) . PHP_EOL;
    echo get_item( $ar, array(10, 2) ) . PHP_EOL;
    
    PHP:
     
    bvraghav, Aug 29, 2010 IP
  3. xcool101Man

    xcool101Man Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is there any built in method in PHP that you can tell if this is an object of array or not?
     
    xcool101Man, Aug 30, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    lukeg32, Aug 30, 2010 IP
  5. Sixsigma84

    Sixsigma84 Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the help.
    I'm needing though of a method to extract the number, without knowing the index or in which specific array they are, since most of the times the information contained within those arrays (the real output) is different and contains more data than the example attached, so It's impossible for me to know exactly which index will contain a valid number. The idea of course would be to avoid looping between each nested array to see if they have content, since it would be very time consuming.

    Regards,
    PS: I'm willing to pay 10 USD for a reliable solution. I would like the solution to be free, but of course, every person here has their own coding problems to attend first IMHO.
     
    Sixsigma84, Aug 30, 2010 IP
  6. Sixsigma84

    Sixsigma84 Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, I've got a temporary solution.
    Serializing the array and then extract (preg_match) the numbers, which should be 7 digits or more. It works faster than looping and checking every single array for content I think.

    If a better solution is available, I'll be very grateful.

    Thanks,
     
    Sixsigma84, Aug 30, 2010 IP