1

I am using php. I have a multidimentional array, time sorted, created array_merge() from other arrays, created from multiple MySql databases. The aim is to create an activity log. It all works but I have multiple entries saying;

X uploaded image a
X uploaded image b
which is quite correct but wastefull of space.
Array
(
    [3] => Array
        (
            [time] => 1500218082
            [user] => James
            [desc] => James posted to forum a_hippo
        )
    [0] => Array
        (
            [time] => 1499294355
            [user] => Tom
            [desc] => Tom edited a wiki page a_tree
        )
    [1] => Array
        (
            [time] => 1498939657
            [user] => Bob
            [desc] => Bob uploaded image a_badger
        )
    [2] => Array
        (
            [time] => 1498939656
            [user] => Bob
            [desc] => Bob uploaded image a_rabbit
        )
)

What I would like to do is roll up key [1] and [2] so [desc] says bob uploaded image a_badger & a_rabbit. My research has uncovered "array_unique" but I dont want to remove completely. and How to detect duplicate values in PHP array?

Can anyone help? Thanks Chris

  • What will happen to the [time] value? Are you looking for a solution that rolls up the resulting array or are you looking to modify your existing code to do the roll-up? –  Jul 16 '17 at 19:14
  • The time value and username can be dropped, its just the [desc] that needs to be merged into its duplicate array. I am struggling as to how this is done logically. so array 1 is read then needs to be compared to array 2.... – chris davies Jul 16 '17 at 19:47

2 Answers2

0

Just use a buffer to store a new array.

$result = array();

// $data is your array
foreach ($data as $row) {
    if (isset($result[$row->user])) {
       $result[$row->user] .= ' and '. trim(str_replace($row->user, '', $row->desc));
    } else {
       $result[$row->user] = $row->desc;
    }
}

// then loop through result var
// each row is a sentence 
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • Thanks I am trying to understand this; $result = array(); $data = array( array("time"=>"12345", "user"=>"tom", "desc"=>"uploaded picture 8"), array("time"=>"15425", "user"=>"tom", "desc"=>"uploaded picture 9") ); echo '
    ';
    print_r($data); // before view
    echo '
    '; // $data is your array foreach ($data as $row) { if (isset($result[$row->user])) { $result[$row->user] .= ' and '. trim(str_replace($row->user, '', $row->desc)); } else { $result[$row->user] = $row->desc; } } echo '
    ';
    print_r($result); // after view
    echo '
    ';'
    – chris davies Jul 16 '17 at 19:37
0

here is something might help:

$dup = [];
$array =
[
    "3" => 
    [
        "time" => "1500218082",
        "user" => "James",
        "desc" => "James posted to forum a_hippo"
    ],
    "0" => 
    [
        "time" => "1499294355",
        "user" => "Tom",
        "desc" => "Tom edited a wiki page a_tree"
    ],
    "1" =>
    [
        "time" => "1498939657",
        "user" => "Bob",
        "desc" => "Bob uploaded image a_badger"
    ],
    "2" =>
    [
        "time" => "1498939656",
        "user" => "Bob",
        "desc" => "Bob uploaded image a_rabbit"
    ]
];

//you could use this with modification of the index
foreach($array as $r)
{
    if(!empty($dup[$r['user']]))
    {
        $descArr = explode($r['user'], $r['desc']);
        $dup[$r['user']]['desc'] .= ' & '. trim(end($descArr));
    }
    else
    {
        $dup[$r['user']] = $r;
    }
}

print_r($dup);

Edit: this supports any desc not only image uploads.

Desolator
  • 22,411
  • 20
  • 73
  • 96
  • My god sir a quick paste into a test script and I think you have solved it. I need to tweek it into my code. I was dreaming of possible solutions last night! Thats not a good sign. The only thing; the truncated array after has the user instead of an id. I can work with that, the [desc] format is exactly what I was looking for. A big thanks! Array ( [tom] => Array ( [time] => 12345 [user] => tom [desc] => uploaded picture 8 & uploaded picture 9 ) ) – chris davies Jul 17 '17 at 09:08
  • Oh I see thats what you mean by the modification of the index? I am learning, arnt we all.. – chris davies Jul 17 '17 at 09:15
  • print_r(array_values($dup)); :) – chris davies Jul 17 '17 at 09:28
  • @chrisdavies - you're welcome man ;) and it's normal to dream about a solution for a problem. that way you keep training your brain and start thinking out of the box. happy coding :P – Desolator Jul 18 '17 at 00:20