2

I got the following array:

"task" : {
    "author_id" : 150,
    "created_at" : somedate,
    "status_id" : 2,
    "assignee_id" : 100,
    "updated_at" : somedate_too

and I got 2 more associative arrays where I store names for IDs in the following way:

"100" => Mike,
"150" => Bob //etc..., the same for statuses

I need to check for the IDs in the first array and replace numbers with names for the corresponding arrays in the most effective way. I tried the following:

if(isset(task['status_id'])){$row = array_merge($row, [$status_ids[
  task['status_id']]]);}else{$row = array_merge($row, '');}

if(isset(task['author_id'])){row = array_merge($row, [$users[// note the different array here
  task['author_id']]]);}else{$row = array_merge($row, '');}

if(isset(task['assignee_id'])){$row = array_merge($row, [$users[
  task['assignee_id']]]);}else{$row = array_merge($row, '');}

In my resulting array ($row) I cannot miss the index and replace it with another value. If there is no value in the first array, I need to insert an empty string to get the following, for example:

['in progress', '', 'Mike']

if there is no author_id in the first array. I believe there should be a better way to do it with foreach loop, but I cant find out how because for different fields I get the data from different arrays. I dont think a separate if clause for every field is the most suitable here. Any help would be welcome. Thank You.

4
  • You should not store data as code. Most types of database provide a means for resolving this problem usually with less code and effort than doing it in php. Commented Sep 20, 2016 at 20:55
  • acrtually it is a result from a db query. I am parsing many such 'tasks' and trying to map the ids with the corresponding names. Also the problem is that some data in some 'tasks' is omitted - say there is no assignee_id or author_id, and I need to leave the value blank in the array Commented Sep 20, 2016 at 21:05
  • 1
    RESOLVE THE DATA IN THE DATABASE Commented Sep 21, 2016 at 9:14
  • what do you mean? could you please explain? Commented Sep 21, 2016 at 13:23

2 Answers 2

1

You could map your special keys to their array counterparts using references and use that mapping when populating $row, like this:

$users = [
    "100" => "Mike",
    "150" => "Bob",
];

$status_ids = [
    1 => "Foo",
    2 => "Bar",
];

// Define output format and array mapping
$mapping = [
    "author_id"   => &$users, // Mapped to $users array
    "created_at"  => null,    // Not mapped, keep $task value
    "status_id"   => &$status_ids,
    "assignee_id" => &$users,
    "updated_at"  => null,
];

$task = [
    "author_id"   => 150,
    "created_at"  => "Some date",
    "status_id"   => 2,
    // "assignee_id" => 99999, // Oops, missing key/value => empty string in $row
    "updated_at"  => "Some other date",
];

foreach ($mapping as $key => $mappedArray) {
    @$row[] = $mappedArray
        ? $mappedArray[$task[$key]] ?: ''
        : $task[$key];
}

print_r($row);

Output:

Array
(
    [0] => Bob
    [1] => Some date
    [2] => Bar
    [3] => 
    [4] => Some other date
)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Is it possible to do the same thing without the 3rd mapping array?
I'm not sure I quite follow. Which array do you mean?
The 3rd one, which you name $mapping. Btw, is result the same if one entry is omitted (say assignee_id is not 99999 but totally erased from the array - key and value)
$mapping is like the key to this solution as it connects "author_id" to the $users array etc. If you drop that variable, this solution won't work.
Regarding your second question; in that case, there will not be any (empty) value in $row for the assignee ID. If you want that functionality, you'll have to define the columns of the expected result (for example in the before-mentioned $mapping) and iterate over that array instead of $task. Without it there is no way for the program to tell what columns are expected in the output.
|
0

It should work (I didn't try it, but it should give you the general idea):

<?php

$fields = array("author_id", "assignee_id", "status_id");
$aliases = array("users", "users", "status_ids");

foreach ($task as $key=>&$value) {
    $alias = str_replace($fields, $aliases, ${$key});

    if (is_array(${$alias}) {
        $value = array_key_exists($value, ${$alias}) ? ${$alias}[$value] : "";
    }
}
unset($value);

And then you can fill up your $row as you planned, directly from the $task array.

1 Comment

Thank you for the idea. I`ll check it out and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.