1

Here's the array i have , and i wanted to modify it the way i needed.

   Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [ip] => 127.0.0.1
                        [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                        [timestamp] => 1402593151
                    )

            )

        [1] => Array
            (
                [0] => Array
                    (
                        [ip] => 127.0.0.1
                        [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                        [timestamp] => 1402593194
                    )

            )

        [2] => Array
            (
                [0] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593249
                )

            [1] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593250
                )

            [2] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593257
                )

            [3] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593282
                )

            [4] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593286
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593303
                )

            [1] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593308
                )

            [2] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593309
                )

            [3] => Array
                (
                    [ip] => 127.0.0.1
                    [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
                    [timestamp] => 1402593309
                )

        )

)

i need the above array parents to remove , and the child values saved, like this one :

Array
(
    [0] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593151
        )
    [1] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593194
        )   
   [2] => Array
       (
           [ip] => 127.0.0.1
           [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
           [timestamp] => 1402593249
       )     
    [3] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593303
        )

    [4] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593308
        )

    [5] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593309
        )

    [6] => Array
        (
            [ip] => 127.0.0.1
            [php_session] => d9r2qfeiheo0gfklcq7vbj9nq7
            [timestamp] => 1402593309
        )
)

after this , i want to modify the result array to remove any key that have the same ip and php_session value. any ideas out there ?

2
  • Just curious, but have you tried to solve this? Perhaps even a simple foreach loop? Commented Jun 12, 2014 at 17:28
  • @JonathanKuhn , yes , i did some research about it , i found tons of answers but non of the answers solved my issue. most helpful answer i got was the RecursiveIteratorIterator function , but still not doing exact what i wanted. Commented Jun 12, 2014 at 17:30

1 Answer 1

2

Just make 2 foreach loops like so:

<?php
$beginArray = array(
    array(
        array(
            'ip' => '127.0.0.1',
            'php_session' => 'abcde',
            'timestamp' => 'abce'
        )
    ),
    array(
        array(
            'ip' => '127.0.0.1',
            'php_session' => 'abcde',
            'timestamp' => 'abce'
        )
    ),
    array(
        array(
            'ip' => '127.0.0.1',
            'php_session' => 'abcde',
            'timestamp' => 'abce'
        )
    ),
);


foreach($beginArray as $array) {

    foreach($array as $arr) {
        $resultArray[] = $arr;
    }

}
var_dump($resultArray);
?>

Anyways: Take better names for the variables. :)

Sign up to request clarification or add additional context in comments.

5 Comments

Nice one , but what about matching arrays with its value ? something like remove array where ip == 127.0.0.1 and php_session == abcde ? and what do u mean by Take better names for the variables , do i set these bad or wrong or something?
No haha, I mean don't take my variable names '$array' '$resultArray' '$arr'.
You will probably have to work with array_search() then.
if you have any idea about it , any way to get it work, i did alots of research about it and still cant figure it out :D
Google array_search() and try it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.