1

I have a multidimensional array with same key value uid.I want to convert this into sub array by limiting foreach loop.you can see two of uid has value 100 and two of uid has value 5465

$userdb=Array
(
    0 => Array
    (
        "uid"=> '100',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    ),

1 => Array
    (
        "uid"=> '5465',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    ),

2 => Array
    (
            "uid"=> '100',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    ),
3 => Array
    (
        "uid"=> '5465',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    ),

);

I want to get array like this.

$userdb=Array
(

  0 => Array(
      0 => Array
      (
          "uid"=> '100',
          "name" => 'Sandra Shush',
          "url"=> 'urlof100'
      ),
      2 => Array
      (
          "uid"=> '100',
          "name" => 'Sandra Shush',
          "url"=> 'urlof100'
      )
    ),
  1=> Array(
    1 => Array
    (
        "uid"=> '5465',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    ),
    3 => Array
    (
        "uid"=> '5465',
        "name" => 'Sandra Shush',
        "url"=> 'urlof100'
    )
  )
);

Can i get the result without using foreach loop ?

1
  • Without any for/foreach is impossible, but you can create a smaller loop that does not loop all values. Commented Jul 25, 2018 at 6:42

2 Answers 2

2

Without looping is impossible but you can limit your looping to just the unique uid's.

// Get all uids to an flat array
$uid = array_column($userdb, "uid");

// Loop the unique uids and find all matching uids and place them in array
foreach(array_unique($uid) as $id){
    $new[] = array_intersect_key($userdb, array_intersect($uid, [$id]));
}
var_dump($new);

This returns as your expected result.

https://3v4l.org/nWFVP

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

1 Comment

@Thanks for solution.Looks Better
2
$userdb=[ ["uid"=> '100', "name" => 'Sandra Shush',"url"=> 'urlof100'],
["uid"=> '5465', "name" => 'Sandra Shush',"url"=> 'urlof100'],
["uid"=> '100', "name" => 'Sandra Shush',"url"=> 'urlof100'],
["uid"=> '5465', "name" => 'Sandra Shush',"url"=> 'urlof100']];

  foreach($userdb as $value) {
        $result[$value['uid']][] = $value;
  }

print_r(array_values($result));

Output:

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [uid] => 100
                    [name] => Sandra Shush
                    [url] => urlof100
                )

            [1] => Array
                (
                    [uid] => 100
                    [name] => Sandra Shush
                    [url] => urlof100
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [uid] => 5465
                    [name] => Sandra Shush
                    [url] => urlof100
                )

            [1] => Array
                (
                    [uid] => 5465
                    [name] => Sandra Shush
                    [url] => urlof100
                )

        )

)

3 Comments

I don't want to use foreach loop.Any other method
@ShaanSetia that is impossible!
You are outputting an associative array, OP wants an indexed array. You should use array_values prior to your print_r

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.