0

I tried convert the array type from $data to $data2 but didn't have success.

In the $data array all values placed according to dates.

But in the $data2 values are placed according to the title.

I wonder if it's convertable array.

$data['Group1']['Date1']['Empty'] = 5;
$data['Group1']['Date1']['Reservated'] = 1;
$data['Group1']['Date1']['WillReturn'] = 3;
$data['Group1']['Date1']['Remains'] = 1;
$data['Group1']['Date2']['Empty'] = 2;
$data['Group1']['Date2']['Reservated'] = 2;
$data['Group1']['Date2']['WillReturn'] = 3;
$data['Group1']['Date2']['Remains'] = -3;

$data['Group2']['Date1']['Empty'] = 0;
$data['Group2']['Date1']['Reservated'] = 1;
$data['Group2']['Date1']['WillReturn'] = 3;
$data['Group2']['Date1']['Remains'] = -4;
$data['Group2']['Date2']['Empty'] = 10;
$data['Group2']['Date2']['Reservated'] = 1;
$data['Group2']['Date2']['WillReturn'] = 1;
$data['Group2']['Date2']['Remains'] = 8;

$data2 = [
    'Group1' => [
        [ 'Title' => 'Empty',
            'Date1' => 5,
            'Date2' => 2,

        ],
        [ 'Title' => 'Reservated',
            'Date1' => 1,
            'Date2' => 2,

        ],
        [ 'Title' => 'WillReturn',
            'Date1' => 3,
            'Date2' => 3,
        ],
        [ 'Title' => 'Remains',
            'Date1' => 1,
            'Date2' => -3,
        ],
        // etc...
    ],
    'Group2' => [
        [ 'Title' => 'Empty',
            'Date1' => 0,
            'Date2' => 10,

        ],
        [ 'Title' => 'Reservated',
            'Date1' => 1,
            'Date2' => 1,

        ],
        [ 'Title' => 'WillReturn',
            'Date1' => 3,
            'Date2' => 1,
        ],
        [ 'Title' => 'Remains',
            'Date1' => -4,
            'Date2' => -8,
        ],
        // etc...
    ],
    // etc...
];

Some try here:

$new = [];
    $grp = array_keys($datax);
    $i =0;
    foreach($datax as $key => $val)
    {
        $dates = array_keys($val);
        foreach($val as $k=>$v)
        {
            $cases = array_keys($v);
            $caseAndVals[$i]=$v;
            $i++;
        }
    }
    $z =0;
    $y = 0;
    foreach($grp as $g)
    {
        foreach($cases as $c)
        {
            $new[$g][$z]['Title']=$c;
            foreach($dates as $d)
            {
                $new[$g][$z][$d] = 'values which correspond to the date and title';
            }
            $z++;
        }

    }

I converted array according to group and cases but i didn't have success to place values corresponded dates...

4
  • @Barmar there is no real code really. I'm just thinking of it. If you search my topics you will see that i'm not here for free coding service. Commented Dec 21, 2016 at 19:56
  • You wrote "I tried ... but didn't have success.". Show what you tried. Commented Dec 21, 2016 at 19:59
  • Ah.. allright.. I'll paste here in a hour Commented Dec 21, 2016 at 19:59
  • @Barmar sorry for late. I placed here my try. Commented Dec 22, 2016 at 10:33

1 Answer 1

1

You're very close.

To get the values which correspond to the date and title, use $data[$g][$d][$c].

And you need to reset $z to 0 each time through the $grp loop, so you get correct indexes on each group array.

$new = array();
$grp = array_keys($data);

foreach($data as $key => $val)
{
    $dates = array_keys($val);
    foreach($val as $k=>$v)
    {
        $cases = array_keys($v);
        $caseAndVals[]=$v;
    }
}

foreach($grp as $g)
{
    $z = 0;
    foreach($cases as $c)
    {
        $new[$g][$z]['Title']=$c;
        foreach($dates as $d)
        {
            $new[$g][$z][$d] = $data[$g][$d][$c];
        }
        $z++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.