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);
asked Apr 14, 2014 at 10:43

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
answered Apr 14, 2014 at 10:51
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);
answered Apr 14, 2014 at 10:52

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
 );
?>
answered Apr 14, 2014 at 10:52

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);
answered Apr 14, 2014 at 10:57

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
)
mah
40.1k9 gold badges79 silver badges94 bronze badges
answered Apr 14, 2014 at 10:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.