0

I have a big problem with a array and I want to group by key (or I don't now :( )

I get the links from mySQL and I do the following:

$lien2 = $links2['text'];
$lien2 = stripslashes($lien2);
$lien2 = htmlspecialchars($lien2);
$lien2 = nl2br($lien2);
preg_match_all('#http://.*?\.?([a-zA-Z0-9\-]+)(\.[a-zA-Z0-9]+)/[a-zA-Z0-9\/\*\-\?\&\%\=\,\.\;\#\_]+#i', $lien2, $lien2_result, PREG_SET_ORDER);

This is the array $lien2_result:

    Array
(
    [0] => Array
        (
            [0] => links1
            [1] => A
        )

    [1] => Array
        (
            [0] => links2
            [1] => B
        )

)
Array
(
    [0] => Array
        (
            [0] => links3
            [1] => C
        )

)
Array
(
    [0] => Array
        (
            [0] => links4
            [1] => B
        )

)
Array
(
    [0] => Array
        (
            [0] => links5
            [1] => D
        )

)
Array
(
    [0] => Array
        (
            [0] => links6
            [1] => E
        )

)

and I want to get the following result:

A
links1
B
links2
links4
C
links3
D
links5
E
links6
4
  • I think that you have to view your problem from another approach. I think that you want to prepare this data to be rendered in some menu structure, so the array you want to get in your question from the source array is not suitable to do such task. Commented Dec 9, 2012 at 17:18
  • Write exactly the format of the array you want. Do you mean Array ( 'A'=>array('links1'), 'B'=> array('links2', 'links4'), 'C'=> .....) ? Commented Dec 9, 2012 at 17:21
  • are you getting $lien2_result in loop means your preg_match_all in loop? Commented Dec 9, 2012 at 17:25
  • if you need 2 different structures for the same data, analyse is probably broken somewhere; would be much easier to decide a final structure which will be used throught the whole project Commented Dec 9, 2012 at 17:30

1 Answer 1

2

I would adjust the query personally but if you are stuck with this resultset you could rewrite it with

foreach($lien2_result as $lien2){
    foreach($lien2 as $item){
        $arr[$item[1]][] = $item[0];
    }
}

where print_r($arr) would result something like:

$arr = Array('A' => Array('links1'), 'B' => Array('links2','links4')); //and so on..

And actual printing the way you asked:

foreach($arr as $name => $value){
    echo($name.'<br />');
    foreach($value as $item){
        echo($item.'<br />');
    }
}

EDIT

Here's an example http://sandbox.onlinephpfunctions.com/code/c0da893797cb2049e8346168b280a9f5b1fa145b

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

3 Comments

@GBD yes i have while ($links2 = $db->get_row()) {
@Sanne print_r($arr) i get this Array ( [t] => Array ( [0] => t [1] => t ) ) Array ( [t] => Array ( [0] => t [1] => t [2] => t ) )
Depends on your resultset, but see above I edited an example online link

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.