-3

if i have an array like this

$arr = [
    [ 'id' => 2, 'name' => 'John', 'class' => 4, 'score' => 90],
    [ 'id' => 5, 'name' => 'Smith', 'class' => 5, 'score' => 30],
    [ 'id' => 7, 'name' => 'Sam', 'class' => 4, 'score' => 70],
    [ 'id' => 9, 'name' => 'Robot', 'class' => 6, 'score' => 100],
];

and i want to pass it as object BUT i want that object to be designed runtime like so

/**
 @method SomeFunction
 @param array the array to be converted
 @param string the name of each property line inside the object
 @param array the format for each line in the property
**/
$myobject = SomeFunction( $arr, 'id', ['Student Class'=>'%class%', 'Student Name'=>'%name%']);
$mysecondobject = SomeFunction( $arr, 'name', ['student Id'=>'%id%', 'Student Score'=>'%score%']

// $myobject : stdClass {
//    2 => stdClass { 'Student Class' = 4, 'Student Name' = 'John' },
//    5 => stdClass { 'Student Class' = 5, 'Student Name' = 'Smith' },
//    7 => stdClass { 'Student Class' = 4, 'Student Name' = 'Sam' },
//    9 => stdClass { 'Student Class' = 6, 'Student Name' = 'Robot' },
// }

// $mysecondobject : stdClass {
//    'John'  => stdClass { 'student Id' = 2, 'Student Score' = 90 },
//    'Smith' => stdClass { 'student Id' = 5, 'Student Score' = 30 },
//    'Sam'   => stdClass { 'student Id' = 7, 'Student Score' = 70 },
//    'Robot' => stdClass { 'student Id' = 9, 'Student Score' = 100 },
// }

the point is the object will follow the format required not a hard-coded design

I've looked at some functions that do db hydration in hope to get some clues but didn't find anything resembling what i want to do

VirusEcks
  • 596
  • 1
  • 10
  • 14
  • Basically you want to map an object value to another object value? What is your final goal? Because you probably have the wrong solution in mind? – Niels Apr 13 '18 at 17:31
  • maybe: `json_decode(json_encode($arr));` ? – Saeed M. Apr 13 '18 at 17:34
  • @Niels i want to use the same model for all the project data so when i pass the object i don't have like 100 converter functions the do same thing with different output – VirusEcks Apr 13 '18 at 17:40
  • @smoqadam already tried .. you have to actually add the data inside the json first .. but what i want is to put the pattern that will encode the data .. sort of like a sprintf but for object and arrays – VirusEcks Apr 13 '18 at 17:42

2 Answers2

0

So you need something like this? Hope this gets your in the right direction. Now you can create a function of it that passes a Modelname and a datarow. To generate any model from data.

$className = "myClassName"; // Generic, pass a model name
$data = [
    [ 'id' => 2, 'name' => 'John', 'class' => 4, 'score' => 90],
    [ 'id' => 5, 'name' => 'Smith', 'class' => 5, 'score' => 30],
    [ 'id' => 7, 'name' => 'Sam', 'class' => 4, 'score' => 70],
    [ 'id' => 9, 'name' => 'Robot', 'class' => 6, 'score' => 100],
];

$output = [];

foreach($data as $row){
    $obj = new $className();
    foreach($row as $key => $value){
        $obj->{$key} = $value;
        // Or if you want to use setters
        // $obj->{"set" . ucfirst($key)}($value);
    }
    array_push($output, $obj);
}

var_dump($output);

Note: this code is not tested

Niels
  • 48,601
  • 4
  • 62
  • 81
  • i'm sorry but you missed the point .. the point is you can change the return vars inside the object "the object will follow the format required not a hard-coded design" – VirusEcks Apr 15 '18 at 13:25
0

after lots of looking around i couldn't find anything like i asked so i made one myself

Github - Objectron

it has limited functionality and maybe not well tested and no unit tests yet but it did the job right

you can pass it an array or object and then tell it to return object with the design you want for ex.

'%id% = %name%'
'Student Name = %student_name%' 

i'll try to update and add more to it as much as i could

VirusEcks
  • 596
  • 1
  • 10
  • 14