0

currently I'm facing a problem that I wasn't able to fix.

Example =>

We have this Array


 $output = Array ( 
    [0] => Array (
    
     [name] => projects/christopher-hgkf/agent/sessions/6b61ac0d/contexts/welcome 
    
    [parameters] => Array (
         [config.original] => Array ( ) 
         [basics_welcome.original] => 
         [password] => 1234 
         [email.original] => test@test.com 
         [language.original] => es 
         [password.original] => 1234 
         [basics_welcome] => 
         [email] => test@test.com 
         [language] => es 
        ) 
       ) 
    
    [1] => Array (
     [name] => projects/christopher-hgkf/agent/sessions/6b61ac0d/contexts/doctor 
    
    [parameters] => Array (
         [language] => es 
        ) 
      ) 
    )

There's a way to find the array with the biggest amount of parameters?

mickmackusa Helped my with to solve this question with the use of


    rsort($array);
    var_export($output[0]['parameters']);

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    Loop, `count()`, and keep track of the highest value and the array index where you found it. – Sammitch Oct 21 '22 at 00:57
  • Thanks, I tried with foreach but isnt working ehhhe. Ill try more times. – Mizar Contasti Oct 21 '22 at 01:03
  • Show your code if you need help with it – ADyson Oct 21 '22 at 01:14
  • @PaulT. sure, I just didnt know that was possible hehehe. Thanks, going on! – Mizar Contasti Oct 21 '22 at 01:27
  • 1
    How dynamic is this data? Might you have more than two subarrays (your sample only shows 2)? What happens if there is a tie for the largest count? Do you want an array of keys where the max count is found? If there can never be a tie (or you don't care about ties, then [`rsort()` can do the whole job](https://3v4l.org/oNIfk). Please clarify. – mickmackusa Oct 21 '22 at 01:47
  • @mickmackusa well, is dynamic cause there should be more than 2 sub arrays, but the structure is the same a subarray that has "parameters". Well this structure will have one subarray that contains all parameters and there will be other sub arrays that will contain few params that are not important. Nope I just want to get the index of the sub array that has the max amount of parameters or either that array. rsort? gonna search now thanks. Could you give me an example on how to implement it with the example code that I wrote? – Mizar Contasti Oct 21 '22 at 02:00
  • 1
    If you click my link in the comment, it will show you my actual demo. It is using the underlying "magic" of `rsort()` (which sorts DESCending) -- it sorts by size, then by data quality. – mickmackusa Oct 21 '22 at 02:08
  • @mickmackusa you got it!!! Currently your response solved the issue that I got, hmmm what sort does with the array? Hmmm I haven't seen that function and I'm like 4 years programing with PHP hahahaha lmao – Mizar Contasti Oct 21 '22 at 02:20
  • PHP's rsort (descending sort) uses QuickSort. You can find my new answer on the dupe target page. – mickmackusa Oct 21 '22 at 02:30
  • Key @mickmackusa I see that its working with an example but, when trying in the webhook doesn't work hmmm. ITs weird that Im putting the same object, then parse it to array an them using the sort, but for some reason its working on the example you give me but isn't on my server lol hahaha – Mizar Contasti Oct 21 '22 at 13:04
  • Show me your more realistic script/data in a 3v4l.org link. – mickmackusa Oct 21 '22 at 13:13
  • @mickmackusa Sure, I added the original request and put some comments ![Issue](https://3v4l.org/BRXUh) – Mizar Contasti Oct 21 '22 at 13:25
  • `rsort()` does not work on your updated data because sort compares `name` data before `parameters`. https://3v4l.org/plMhc -- as demonstrated on the dupe as well. – mickmackusa Oct 21 '22 at 13:52
  • @mickmackusa rsort worked for me, the solution you provided changed my array structure, so My webhook got broken :C. – Mizar Contasti Oct 27 '22 at 22:36
  • As I said, because `name` data gets sorted before `parameters` data, using `rsort()` is no longer the correct approach. See my earlier demo link that uses `array_multisort(array_map('count', array_column($input, 'parameters')), SORT_DESC, $input);` -- this approach is demonstrated on the dupe target in Arnaud's accepted answer. – mickmackusa Oct 27 '22 at 22:41
  • thanks for your help but, sometimes the "correct approach" doesn't fit the solution as happened to me. – Mizar Contasti Oct 27 '22 at 23:45

1 Answers1

1
$output = [
        [
            "parameters" => [
                "var1" => 1,
                "var2" => 2
            ]
        ],
        [
            "parameters" => [
                "var1" => 3,
                "var2" => 4,
                "var3" => 5
            ]
        ],
    ];
$amounts = array_map(function ($subArray) {
        return count($subArray['parameters']);
    }, $output);
$index = array_keys($amounts, max($amounts));
$r = $output[$index[0]];

use array_map to get the amount, and use max to get the biggest amount, use array_keys to get the indexes of the max amounts. and use $output[$index[0]] to get the result u want.

Cositanto
  • 682
  • 5
  • 15
  • could you give me an example with the example code that I used? Hmm I have some doubts about your explanation. Max returns the keys but, isnt about keys is about the value that the keys have inside. What do you think? – Mizar Contasti Oct 21 '22 at 01:39
  • just try my code,it's the example – Cositanto Oct 21 '22 at 01:41
  • Cositano, you are right. What I say is that I miss explained the restructure hehehe. Got I expected I was fine :C. To give more context, Im receiving a json, that Im turning into an array and in that array there's this part that contains output. Im going to add the specific example that Im taking care at the top :3 – Mizar Contasti Oct 21 '22 at 02:05