Advertisement
mocfaisal

PHP Manipulate Data array as index to column

May 26th, 2022
1,202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $new_data = array();
  3.  
  4. $data = array(
  5.  
  6.     array(
  7.         'name' => 'kunci',
  8.         'value' => '1'
  9.     ),
  10.     array(
  11.         'name' => 'set1',
  12.         'value' => '1'
  13.     ),
  14.     array(
  15.         'name' => 'set2',
  16.         'value' => '2'
  17.     ),
  18. );
  19.  
  20. foreach ($data as $row) {
  21.     $new_data[] = $row;
  22. }
  23.  
  24.  
  25. $new_data = array_filter(arrayToColumn($new_data, 'name', 'value'));
  26.  
  27. print_r($new_data);
  28.  
  29. function arrayToColumn($arr = array(), $index = 'name', $value = 'value') {
  30.     //convert list value to column
  31.  
  32.     /*
  33.     @param $arr = Array()
  34.     @param index like name
  35.     @param value like value
  36.  
  37.      */
  38.  
  39.     return array_column($arr, $value, $index);
  40. }
  41.  
Advertisement
RAW Paste Data Copied
Advertisement