Asked  7 Months ago    Answers:  5   Viewed   24 times

I have an array that looks like this. This is a 2 dimensional array.

$MainArray = Array
(
    [0] => Array
        (
            [Job_Name] => WXYZ
            [Quantity] => 1000
            [Machine_Name] => Machine1
            [Start_Date] => 2014-07-30 00:00:00
            [Completion_Date] => 2014-08-02 00:00:00
            [Labor] => 4
        )
    [1] => Array
        (
            [Job_Name] => ABCD
            [Quantity] => 1500
            [Machine_Name] => Machine2
            [Start_Date] => 2014-08-08 00:00:00
            [Completion_Date] => 2014-08-14 00:00:00
            [Labor] => 2
        )
    [2] => Array
        (
            [Job_Name] => BCDA
            [Quantity] => 1200
            [Machine_Name] => Machine1
            [Start_Date] => 2014-08-02 00:00:00
            [Completion_Date] => 2014-08-07 00:00:00
            [Labor] => 1
        )
 )

I want to use this information to create a new 3 dimensional array that looks like this.

$ConvertedArray = Array
(
    [Machine1] => Array
        (
            [0] => Array
               (
                  [Job_Name] => WXYZ
                  [Quantity] => 1000
                  [Start_Date] => 2014-07-30 00:00:00
                  [Completion_Date] => 2014-08-02 00:00:00
                  [Labor] => 4
               )
            [1] => Array
               (
                  [Job_Name] => BCDA
                  [Quantity] => 1200
                  [Start_Date] => 2014-08-02 00:00:00
                  [Completion_Date] => 2014-08-07 00:00:00
                  [Labor] => 1
               )
         )
      [Machine2] => Array
        (
            [0] => Array
               (
                  [Job_Name] => ABCD
                  [Quantity] => 1500
                  [Machine_Name] => Machine2
                  [Start_Date] => 2014-08-08 00:00:00
                  [Completion_Date] => 2014-08-14 00:00:00
                  [Labor] => 2
               )
         )   
)

Please any help on this would be appreciated. I am stuck with something and need to figure out how to create the new array using this original array. So basically I am grouping all the jobs from each machine together and the keys for those jobs depend on how they are in the original array. So if the original array has a job with the key 2 and no other job has a higher key on that machine, then it will become key 0 for that job and create a new key with that machine name.

I really appreciate your help on this.

 Answers

82
foreach ($MainArray as $value) {
    $name = $value['Machine_Name'];
    unset($value['Machine_Name']);
    $ConvertedArray[$name][] = $value;
}
Tuesday, June 1, 2021
 
Akarun
answered 7 Months ago
29

I would gather the data and construct a tree first, and then print the tree out. Some sample code:

foreach ($nodes as $item) {
    // gather the data in an assoc array indexed by id
    if ( !isset($data_by_id[ $item['top_id'] ])) {
        $data_by_id[ $item['top_id'] ] = array( 'name' => $item['standard'] );
    }
    if ( !isset($data_by_id[ $item['parent_id'] ])) {
        $data_by_id[ $item['parent_id'] ] = array( 'name' => $item['indicator'] );
    }
    if ( !isset($data_by_id[ $item['child_id'] ])) {
        $data_by_id[ $item['child_id'] ] = array( 
            'name' => $item['element'],
            'contents' => array(
                $item['Connections'],
                $item['Effective Practice'],
                $item['Proficient'],
                $item['Suggested Artifacts'])
        );
    }
    // construct the tree - I've made a simple three tier array
    $tree[ $item['top_id'] ][ $item['parent_id'] ][ $item['child_id'] ]++;
}

// go through the tree and print the info
// this is a recursive function that can be used on arbitrarily deep trees
function print_tree( $data, $arr ){
    echo "<ul>n";
    // the tree is an associative array where $key is the ID of the node,
    // and $value is either an array of child nodes or an integer.
    foreach ($arr as $key => $value) {
        echo "<li>" . $data[$key]['name'] . "</li>n";
        if (isset($data[$key]['contents']) && is_array($data[$key]['contents'])) {
            echo '<ul>';
            foreach ($data[$key]['contents'] as $leaf) {
                echo '<li>' . $leaf . "</li>n";
            }
            echo "</ul>n";
        }
        // if $value is an array, it is a set of child nodes--i.e. another tree.
        // we can pass that tree to our print_tree function.
        if (is_array($value)) {
            print_tree($data, $value);
        }
    }
    echo "</ul>n";
}

print_tree($data_by_id, $tree);

You'll need to add in error checking, zapping of strange characters, removal of excess whitespace, etc.

Wednesday, March 31, 2021
 
Akarun
answered 9 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 7 Months ago
96

Because the arrays are multidimensional you have to extract the ids like this:

$ids1 = array();
foreach($array1 as $elem1)
    $ids1[] = $elem1['id'];

$ids2 = array();
foreach($array2 as $elem2)
    $ids2[] = $elem2['id'];

$one_not_two = array_diff($ids1,$ids2);

For your specific question, check out array_diff() with multidimensional arrays

Saturday, May 29, 2021
 
etsous
answered 7 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share