1

I need to group array by its sub array value.

just came through this link.

Group array by subarray values

But here sub array value having keys. In my case we don't have key. Only indexed keys. Below is my array

Array
(
    [0] => Array
        (
            [0] => 14. Remarketing Bengaluru
            [1] => RM Web
            [2] => Ludhiana
            **[3] => Display Only**
            [4] => INR
            [5] => 6
        )

    [1] => Array
        (
            [0] => 12. Other Search ROTN
            [1] => 01. Eye glass Shapes
            [2] => Tiruchirappalli
            **[3] => Search Only**
            [4] => INR
            [5] => 144
        )

    [2] => Array
        (
            [0] => 10. Brand ROTN
            [1] => 03. Brand + Keywords
            [2] => Erode
            **[3] => Search Only**
            [4] => INR
            [5] => 24
        )

    [3] => Array
        (
            [0] => 11. Competitors ROTN
            [1] => 02. Titan Eye Plus
            [2] => Thoothukudi
            **[3] => Search Only**
            [4] => INR
            [5] => 1
        )

    [4] => Array
        (
            [0] => 14. Remarketing Bengaluru
            [1] => RM Web
            [2] => Chandigarh
            **[3] => Display Only**
            [4] => INR
            [5] => 6
        )
)

Expected:

Needs to group above array with starred value. Eg. Need to group array with array indexed key 3.

Grouped array should be like Display Only, Search Only

6
  • 1
    Can you post your source code Commented Feb 15, 2020 at 14:07
  • Not much in source code. Just fetching array from csv file. $csvFile = file('new-report.csv'); $csvRecords = []; foreach ($csvFile as $key => $line) { if($key > 2) { $csvRecords[] = str_getcsv($line, "\t"); } } $csvRecords - this is the array i have added in my question. Thanks. Commented Feb 15, 2020 at 14:10
  • 2
    Have you tried using one of the solutions from the SO post you link to? A numerical index of an array is the key when it is like that. Commented Feb 15, 2020 at 14:21
  • Yes i tried, but it not working for my case. Commented Feb 15, 2020 at 14:27
  • It works exactly as GetSet described in the comment. You'd better post your array as PHP source code. You can do this with the var_export () function. Commented Feb 15, 2020 at 17:22

1 Answer 1

2

This is the solution from Group array by subarray values. I just replaced 'id' with 3.

$old_arr = array( 
  array( "14. Remarketing", "RM", "Lu", "Display Only", "INR", "6" ), 
  array( "12. Other", "Shapes", "Ti", "Search Only", "INR", "144" ), 
  array( "10. Brand", "Brand", "E", "Search Only", "INR", "24" ), 
  array( "11. Competitors", "Titan", "Th", "Search Only", "INR", "1"), 
);

foreach ($old_arr as $key => $item) {
   $arr[$item[3]][$key] = $item;
}

echo '<pre>';
var_export($arr);

Result:

array (
  'Display Only' => 
  array (
    0 => 
    array (
      0 => '14. Remarketing',
      1 => 'RM',
      2 => 'Lu',
      3 => 'Display Only',
      4 => 'INR',
      5 => '6',
    ),
  ),
  'Search Only' => 
  array (
    1 => 
    array (
      0 => '12. Other',
      1 => 'Shapes',
      2 => 'Ti',
      3 => 'Search Only',
      4 => 'INR',
      5 => '144',
    ),
    2 => 
    array (
      0 => '10. Brand',
      1 => 'Brand',
      2 => 'E',
      3 => 'Search Only',
      4 => 'INR',
      5 => '24',
    ),
    3 => 
    array (
      0 => '11. Competitors',
      1 => 'Titan',
      2 => 'Th',
      3 => 'Search Only',
      4 => 'INR',
      5 => '1',
    ),
  ),
) 
Sign up to request clarification or add additional context in comments.

Comments

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.