Asked  4 Months ago    Answers:  5   Viewed   42 times

I have a multidimensional indexed array. Each element is an associative array with an id column which is unique between elements (its value never repeats within the array).

[indexed] =>Array
(
    [0] => Array
        (
            [id] => john
            [name] => John
            [age] => 29
        ),

    [1] => Array
        (
            [id] => peter
            [name] => Peter
            [age] => 30
        ),

    [2] => Array
        (
            [id] => harry
            [name] => Harry
            [age] => 19
        )
)

My goal is to convert this array into a multidimensional associative array, indexed by id values.

[indexed] =>Array
(
    [john] => Array
        (
            [id] => john
            [name] => John
            [age] => 29
        ),

    [peter] => Array
        (
            [id] => peter
            [name] => Peter
            [age] => 30
        ),

    [harry] => Array
        (
            [id] => harry
            [name] => Harry
            [age] => 19
        )
)

My best attempt so far is to loop over array elements and manually create the final array.

$associative = array();
foreach($indexed as $key=>$val) $associative[$val['id']] = $val;

I think it's not the most elegant solution. Is it possible to obtain the same result with built-in (more efficient) functions?

 Answers

34

The truth is php DOES offer a single, native function that allows you to replace the outer indexes with the values of a single column. The "magic" is in the 2nd parameter which tells php not to touch the subarray values when assigning the new keys.

Code: (Demo)

$indexed = [
    ['id' => 'john', 'name' => 'John', 'age' => 29],
    ['id' => 'peter', 'name' => 'Peter', 'age' => 30],
    ['id' => 'harry', 'name' => 'Harry', 'age' => 19],
];

var_export(array_column($indexed, null, 'id'));

Output:

array (
  'john' => 
  array (
    'id' => 'john',
    'name' => 'John',
    'age' => 29,
  ),
  'peter' => 
  array (
    'id' => 'peter',
    'name' => 'Peter',
    'age' => 30,
  ),
  'harry' => 
  array (
    'id' => 'harry',
    'name' => 'Harry',
    'age' => 19,
  ),
)
Friday, August 6, 2021
 
sohum
answered 4 Months ago
42
function set_val(array &$arr, $path,$val)
{
   $loc = &$arr;
   foreach(explode('.', $path) as $step)
   {
     $loc = &$loc[$step];
   }
   return $loc = $val;
}
Wednesday, March 31, 2021
 
laurent
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
10
foreach($array1 as &$arrayItem) {
$arrayItem['grade'] = $array2[$arrayItem['id']]
}

Here you will have array 1 merged as you wished

Saturday, May 29, 2021
 
Classified
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
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