0

I want to create a list where if its already in the array to add to the value +1.

Current Output

[1] => Array
    (
        [source] => 397
        [value] => 1
    )

[2] => Array
    (
        [source] => 397
        [value] => 1
    )

[3] => Array
    (
        [source] => 1314
        [value] => 1
    )

What I want to Achieve

[1] => Array
    (
        [source] => 397
        [value] => 2
    )

[2] => Array
    (
        [source] => 1314
        [value] => 1
    )

My current dulled down PHP

        foreach ($submissions as $timefix) {

              //Start countng
              $data = array(
                    'source' => $timefix['parent']['id'],
                    'value' => '1'
              );

              $dataJson[] = $data;

    }

            print_r($dataJson);

5 Answers 5

2

Simply use an associated array:

$dataJson = array();

foreach ($submissions as $timefix) {
    $id = $timefix['parent']['id'];

    if (!isset($dataJson[$id])) {
        $dataJson[$id] = array('source' => $id, 'value' => 1);
    } else {
        $dataJson[$id]['value']++;
    }
}

$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
Sign up to request clarification or add additional context in comments.

Comments

1

This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:

foreach ($submissions as $timefix) {
    $id = $timefix['parent']['id'];
    if (array_key_exists($id, $dataJson)) {
        $dataJson[$id]["value"]++;
    } else {
        $dataJson[$id] = [
            "source" => $id,
            "value" => 1
        ];
    }
}
print_r($dataJson);

Comments

0

You should simplify this for yourself. Something like:

<?
  $res = Array();
  foreach ($original as $item) {
    if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
    else $res[$item['source']] += $item['value'];
  }
?>

After this, you will have array $res which will be something like:

Array(
  [397] => 2,
  [1314] => 1
)

Then, if you really need the format specified, you can use something like:

<?
  $final = Array();
  foreach ($res as $source=>$value) $final[] = Array(
    'source' => $source,
    'value' => $value
  );
?>

Comments

0

This code will do the counting and produce a $new array as described in your example.

$data = array(
    array('source' => 397, 'value' => 1),
    array('source' => 397, 'value' => 1),
    array('source' => 1314, 'value' => 1),
);

$new = array();
foreach ($data as $item)
{
    $source = $item['source'];
    if (isset($new[$source]))
        $new[$source]['value'] += $item['value'];
    else
        $new[$source] = $item;
}
$new = array_values($new);

Comments

0

PHP has a function called array_count_values for that. May be you can use it

Example:

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

Output:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

Comments

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.