Home>

When the following sequence exists

$testarr [] = array (
            'namae' =>'Tanaka',
            'favorite' =>'tuna'
        );
        $testarr [] = array (
            'namae' =>'Tanaka',
            'favorite' =>'Hamachi'
        );
        $testarr [] = array (
            'namae' =>'Tanaka',
            'favorite' =>'how much'
        );
        $testarr [] = array (
            'namae' =>'Tanaka',
            'favorite' =>'salmon'
        );
        $testarr [] = array (
            'namae' =>'Tanaka',
            'favorite' =>'sardines'
        );
        $testarr [] = array (
            'namae' =>'Matsuda',
            'favorite' =>'shishamo'
        );
        $testarr [] = array (
            'namae' =>'Matsuda',
            'favorite' =>'mackerel'
        );
        $testarr [] = array (
            'namae' =>'Matsuda',
            'favorite' =>'Thailand'
        );
        $testarr [] = array (
            'namae' =>'Matsuda',
            'favorite' =>'Buri'
        );
        $testarr [] = array (
            'namae' =>'Matsuda',
            'favorite' =>'Kalei'
        );
I created a new array as one array with the same

name, but I am not sure how to do it.
I want to create a new array as shown below.

$kanseilist = array (
  0 =>
  array (
    'name' =>'Tanaka'
    array (
      0 =>'tuna',
      1 =>'Hamachi',
      2 =>'how much',
      3 =>'Salmon',
      4 =>'sardines',
    ),
  1 =>
  array (
    'name' =>'Matsuda'
    array (
      0 =>'shishamo',
      1 =>'mackerel',
      2 =>'Thailand',
      3 =>'Buri',
      4 =>'Kalei',
  ),
);

I tried the following, but I was struggling because I couldn't distribute them well.

$kanseilist = array ();
            for ($j = 0;$j<count ($testarr);$j ++) {
                if ($testarr [$i] ['name'] == $testarr [$j] ['name']) {
                    $kanseilist [] = array (
                        'name' =>$testarr [$j] ['name'],
                        'shohi' =>$testarr [$j] ['favorite']
                    );
                }
            }
        }
php
  • Answer # 1

    'Tanaka',
        'favorite' =>'tuna'
    );
    $testarr [] = array (
        'namae' =>'Tanaka',
        'favorite' =>'Hamachi'
    );
    $testarr [] = array (
        'namae' =>'Tanaka',
        'favorite' =>'how much'
    );
    $testarr [] = array (
        'namae' =>'Tanaka',
        'favorite' =>'salmon'
    );
    $testarr [] = array (
        'namae' =>'Tanaka',
        'favorite' =>'sardines'
    );
    $testarr [] = array (
        'namae' =>'Matsuda',
        'favorite' =>'shishamo'
    );
    $testarr [] = array (
        'namae' =>'Matsuda',
        'favorite' =>'mackerel'
    );
    $testarr [] = array (
        'namae' =>'Matsuda',
        'favorite' =>'Thailand'
    );
    $testarr [] = array (
        'namae' =>'Matsuda',
        'favorite' =>'Buri'
    );
    $temp = [];
    $arr = [];
    foreach ($testarr as $value) {
        $temp [$value ['namae']] [] = $value ['favorite'];
    }
    foreach ($temp as $name =>$favorites) {
        $arr [] = [
          'name' =>$name,
          $favorites
        ];
    }
    var_dump ($arr);