-1

Hi I have one multidimensional array, I retrieved the data using mysql PDO and formatted it this way

array('id'=>$row['empname'],'data'=>array(array($row['salestat'],$row['salecount'])))

Array
(
[0] => Array
    (           
        [id] => 'employee1'
        [data] => Array
             (
              0 => 'Sold'
              1 => 1
             )
    )

[1] => Array
    (            
        [id] => 'employee2'
        [data] => Array
             (
              0 => 'On Process'
              1 => 4
             )
    )

[2] => Array
    (            
        [id] => 'employee2'
        [data] => Array
             (
              0 => 'Sold'
              1 => 2
             )
    )
)

I am trying to make my output like this, trying to format this so I can use it for drilldown series in highcharts, thank you

Array
(
[0] => Array
    (           
        [id] => 'employee1'
        [data] => Array
             (
              0 => 'Sold'
              1 => 1
             )
    )

[1] => Array
    (            
        [id] => 'employee2'
        [data] => Array
           [0]
             (
              0 => 'On Process'
              1 => 4
             )
           [1]
             (
              0 => 'Sold'
              1 => 2
             )
    )
)
Clint Lo
  • 45
  • 6
  • What about using a loop? And you cant have same index at same level. – Sougata Bose Aug 07 '15 at 06:17
  • 2
    please first learn about array you can't give same key more then one time – Dipesh Shihora Aug 07 '15 at 06:17
  • Cant give same key in array – Santosh Jagtap Aug 07 '15 at 06:18
  • Array can't have same key if it have then it'll get the last value of that array [how-does-php-index-associative-arrays](http://stackoverflow.com/questions/30636179/how-does-php-index-associative-arrays/30636430#30636430) – Narendrasingh Sisodia Aug 07 '15 at 06:19
  • is the edit more appropriate? The only reason i specified a key for the inner arrays is because i thought that those keys could be used to identify for combining them into one – Clint Lo Aug 07 '15 at 06:28
  • thank you uchiha for the link it was really helpful. I know my knowledge is still lacking but i would like to know if it is possible to just get the output, i read other questions in this site and looked for other resources but I cannot find the answer to what i seek – Clint Lo Aug 07 '15 at 06:37

2 Answers2

1

Firstly, you can not use a field inside an array twice with the same key like salescount or salestat. But you can do something like this.

function wrapSameId( $employeeArray ) {

    //create the new array.
    $newEmployeeArray = array();

    //loop the old array.
    foreach ($employeeArray as $key => $value) {
        $exists = 0;
        $i=0;

        $id = $value['id'];
        $data = $value['data'];

        //see if you can find the current id inside the newly created array if you do, only push the new data in.
        foreach ($newEmployeeArray as $key2 => $value2) {
            if( $id == $value2['id'] ) { $newEmployeeArray[$key2]['data'][] = $data;$exists = 1;var_dump($value2['data']); }
            $i++;
        }

        //if we didnt find the id inside the newly created array, we push the id and the new data inside it now.
        if( $exists == 0 ){
            $newEmployeeArray[$i]['id'] = $id;
            $newEmployeeArray[$i]['data'][] = $data; 
        }
    }   

    return $newEmployeeArray;

}

This function should return a new Array but if in the old array there were multiple arrays with the same id, in this one we should have:

Array
(
    [0] => Array
        (
            [id] => employee1
            [data] => Array
                (
                    [0] => Array
                        (
                            [salescount] => 4
                            [salesstat] => Sold
                        )

                )

        )

    [1] => Array
        (
            [id] => employee2
            [data] => Array
                (
                    [0] => Array
                        (
                            [salescount] => 2
                            [salesstat] => In Progress
                        )

                    [1] => Array
                        (
                            [salescount] => 5
                            [salesstat] => Sold
                        )

                )

        )

)

You can use it like this:

$employees = wrapSameId( $PDOArray );

where $PDOArray is your initial Array. Hope this is what you were looking for.

  • thank you sir i edited my post since it caused confusion more than helping, but I tried your code and it worked like a charm my highcharts drilldown works now very much appreciate your answer and help :) – Clint Lo Aug 07 '15 at 07:25
  • @ClintLo Glad I could help, keep up the coding. – Denis Makula Aug 07 '15 at 07:29
0

Why not index your output array by id?

Loop over your initial array and add the salecount, salestat pair to an employee's data array.

$outputArray = array();

//Loop over each entry pulled from you database...
foreach ($employeeArray as $employeeInfo)
{
    //If no index for the current employee exists, create an empty array
    //The only reason I'm including this is because I'm not sure how array_merge handles nulls -- just to be safe
    if (!$outputArray[$employeeInfo['id']])
    {
        $outputArray[$employeeInfo['id']] = array();
    }

    //Merge the existing array for that employee with the new data for the same employee
    $outputArray[$employeeInfo['id']] = array_merge($outputArray[$employeeInfo['id']], $employeeInfo['data']);
}

print_r($outputArray);

It's easier to visualize the output...

Array
(
[employee1] => Array
            (           
                 0 => 1
                 1 => 'Sold'
            )

[employee2] => Array
            (            
                 0 => 4
                 1 => 'On Process'
                 2 => 2
                 3 => 'Sold'
            )
)

If you're not satisfied with your output being indexed by id, loop over it again and format it as you want.

$otherOutputArray = array();
foreach ($outputArray as $key => $value)
{
    $otherOutputArray[] = array('id' => $key, 'data' => $value);
}

That gives you exactly what you want.

Evan
  • 89
  • 5
  • sir thank you for giving a way to output to what i posted earlier, it works as expected i sincerely apologize since i edited my post late. But hopefully i could use what you taught me today in the near future. I appreciate your time and effort :) – Clint Lo Aug 07 '15 at 07:48