i have this array
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] => 3
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)
what i want to do is if the key post_id is repeated i just want to empty it and keep one so my final array will look like this
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] =>
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] =>
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] =>
[post_user_added_id] => 15
)
i have tried this code but it doesn't seem to work it deletes the whole array
foreach($arry as $k => $v)
{
foreach($arry as $key => $value)
{
if($k != $key && $v['post_id'] == $value['post_id'])
{
unset($arry [$k]);
}
}
}
print_r($arry);
You can perform foreach with ternary operator
$last = null;//this will keep the previous post_id
foreach($arr as &$v){
($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);
}
print_r($arr);
Working example :- https://3v4l.org/RiU9J
here try this.
$tempArr = [];
$resArr = [];
foreach($orignalArr as $key=>$obj){
if(in_array($obj['post_id'], $tempArr)){
$obj['post_id'] = '';
$resArr[] = $obj;
}else{
$tempArr[] = $obj['post_id'];
$resArr[] = $obj;
}
}
print_r($resArr);
try this :
// example code
$arrayResult = array();
$arry = [
1 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 2
],
2=>
[
'user_id' => 76,
'post_id' => 3,
'post_user_added_id' => 16
],
3 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 12
],
4 =>
[
'user_id' => 76,
'post_id' => 9,
'post_user_added_id' => 15
],
5 =>
[
'user_id' => 77,
'post_id' => 9,
'post_user_added_id' => 15
]];
$final = array();
foreach($arry as $a)
{
if (in_array($a['post_id'], $arrayResult)) {
$a['post_id'] = 0;
}else{
$arrayResult[] = $a['post_id'];
}
$final[] = $a;
}
var_dump($final);
Maybe if you use a stack for comparison?
edit: rewritten the code...
$stack=[];
foreach ($array as $index => $subarray){
if(in_array($subarray['post_id'], $stack)) {
$array[$index]['post_id'] = null;
}
else $stack[]=$subarray['post_id'];
}
print_r($array);
example: https://3v4l.org/FsPUG
Hope this helps!
use of your array value in instead of $arr and try to filter array like the following code :
$arr = array(
array(
'user_id'=>15,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
);
$postids = array();
foreach($arr as $key=>$val){
if(in_array($val['post_id'], $postids)){
$arr[$key]['post_id'] = '';
}else{
$postids[] = $val['post_id'];
}
}
echo "<pre>";print_r($arr);exit;
Output :
Array
(
[0] => Array
(
[user_id] => 15
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
[2] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[3] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[4] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
)
Related
I have the following array:
[0] => Array
(
[id] => 1
[uid] => 50
[sum1] => 1
[sum2] => 2
)
[1] => Array
(
[id] => 2
[uid] => 50
[sum1] => 2
[sum2] => 4
)
[2] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
As you can see, on some of those indexes, [uid] is the same. The length and data of the array is dynamic.
what i need to do is merge the indexes that have the same value for[uid] and sum the values for the other keys:
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
But for the life of me, i can't figure out how to do that.
Any help appreciated!
You can do it like this way,
<?php
$mainArray = [
0 => Array
(
'id' => 1,
'uid' => 50,
'sum1' => 1,
'sum2' => 2
),
1 => Array
(
'id' => 2,
'uid' => 50,
'sum1' => 2,
'sum2' => 4
),
2 => Array
(
'id' => 3,
'uid' => 51,
'sum1' => 3,
'sum2' => 5
)
];
$newArray=[];
foreach ($mainArray as $value) {
if(!array_key_exists($value['uid'], $newArray)){
// Store first time
$newArray[$value['uid']] = $value;
}else{
// Already exist, then sum and replace it
$newArray[$value['uid']]['id'] = $value['id'];
$newArray[$value['uid']]['sum1'] += $value['sum1'];// Sum with previous value
$newArray[$value['uid']]['sum2'] += $value['sum2'];// Sum with previous value
}
}
$newArray = array_values($newArray);// Reset indexes, Start the array index with zero
print_r($newArray);// To see the output
Output:
Array
(
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
)
I think you are correct. Actually, i solved the problem using something similar:
$sumArray = Array();
foreach ($array1 as $item){
if(isset($sumArray[$item['uid']])){
$sumArray[$item['idPartener']]['sum1'] += $item['sum1'];
$sumArray[$item['idPartener']]['sum2'] += $item['sum2'];
}else{
$sumArray[$item['uid']] = Array(
'id' => $item['id'],
'sum1' => $item['sum1'],
'sum2' => $item['sum2'] ,
);
}
}
Thanks all for your help!
I need to merge two arrays on the second level with different key names but the same amount of keys.
usera=102
userb=103
My desired output would be array 3.
I have tried a lots of variations to get this together.
My best attempt was something like the following but it destroyed my key names:
$results = array();
foreach($arr1 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name'] = $value;
}
}
foreach($arr2 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name2'] = $value;
}
}
Array 1 :
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
)
[1] => Array
(
[user] => usera
[duration_s] => 9
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
)
[3] => Array
(
[user] => userb
[duration_s] => 19
)
)
)
Array 2:
Array
(
[102] => Array
(
[0] => Array
(
[user] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => 102
[duration_s2] => 378
)
)
[103] => Array
(
[2] => Array
(
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[usernr] => 103
[duration_s2] => 110
)
)
)
Array 3:
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
[usernr] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => usera
[duration_s] => 9
[usernr] => 102
[duration_s2] => 378
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[user] => userb
[duration_s] => 19
[usernr => 103
[duration_s2] => 110
)
)
)
Try the following code using array_values() :
<?php
$array1 = [
'usera' => [
0=> ['user' => 'usera','duration_s' => 15],
1=> ['user' => 'usera','duration_s' => 9],
],
'userb' => [
2=> ['user' => 'usera','duration_s' => 15],
3=> ['user' => 'usera','duration_s' => 9],
],
];
$array2 = [
102 => [
0=> ['usernr' => 102,'duration_s2' => 54],
1=> ['usernr' => 102,'duration_s2' => 378]
],
103 => [
2=> ['usernr' => 103,'duration_s2' => 299],
3=> ['usernr' => 103,'duration_s2' => 110]
],
];
$array2 = array_values($array2);
$array1 = array_values($array1);
foreach($array1 as $index=>$ar1){
foreach ($ar1 as $index2=>$ar2){
$array1[$index][$index2] = array_merge($ar2,$array2[$index][$index2]);
}
}
print_r($array1);
To merge the array you can also do the following :
$index = 0;
foreach ($arr2 as $key => $value) { // changing the indexing of second array
$arr3[$index] = $value;
$index++;
}
$results = array();
$count = 0;
foreach ($arr1 as $index => $value) { // merging two array
$total = 0;
foreach ($value as $key => $val) {
$merge_value = array_merge($val,$arr3[$count][$total]);
$results[$index][$total] = $merge_value;
$total++;
}
$count++;
}
print_r($results);
In this result your indexing will not lost.
I have an php array format:
[0] => Array
(
[post_id] => 37
[post_title] => الأَبْجَدِيَّة العَرَبِيَّة
[post_image] =>
[post_status] => 1
)
[1] => Array
(
[post_id] => 36
[post_title] => TEST open for text
[post_image] => post_1463052793.jpeg
[post_status] => 1
)
[2] => Array
(
[post_id] => 35
[post_title] => Hey Sushovan
[post_image] => post_1463038438.jpg
[post_status] => 1
)
Now, I want to append an extra index with value. For this, I am using this code:
$all_data = $this->master_model->fetch_all_data_order_by($entity, $selectString, $entity.'_publish_date', 'DESC', $limit, $offset = $page);
$data['all_data']=$all_data;
foreach($all_data as $ad => $row)
{
$fetch = '*';
$table = 'chat';
$cond = $table."_to = 'A' AND post_id = '".$row['post_id']."' AND chat_view_status = 0";
$count = $this->master_model->count_data_by_condition($fetch,$table,$cond);
$pushArr = array('chat_count' => $count);
array_push($row,$pushArr);
}
However, the I can't push the data into the original $all_data.
How can I achieve this?
[0] => Array
(
[post_id] => 37
[post_title] => الأَبْجَدِيَّة العَرَبِيَّة
[post_image] =>
[post_status] => 1
[chant_count] => 2
)
The chat count is retrieved by calling count_data_by_condition() method.
You just need to push the chat_count to correct array. I think your "$all_data" variable is now a part of $data array with key 'data'.
Sample code:
$all_data = $this->master_model->fetch_all_data_order_by($entity, $selectString, $entity.'_publish_date', 'DESC', $limit, $offset = $page);
$data['all_data']=$all_data;
foreach($data['all_data'] as $ad => $row)
{
$fetch = '*';
$table = 'chat';
$cond = $table."_to = 'A' AND post_id = '".$row['post_id']."' AND chat_view_status = 0";
$count = $this->master_model->count_data_by_condition($fetch,$table,$cond);
$data['all_data'][$ad]['chat_count'] = $count;
}
Hope it helps !
Try below code:
$array = array(array("title" => "test", "desc" => "test2"), array("title" => "aaa", "desc" => "bbb"));
echo "before==>";
print_r($array);
foreach ($array as $key => $value) {
$array[$key]["chat_count"] = "123456";
}
echo "<br/>after==>";
print_r($array);
Your array
$arr = array(
0=> array('post_id'=> 7,'post_title'=> 'Title 7'),
1=> array('post_id'=> 8,'post_title'=> 'Title 8'),
);
echo '<pre>';
print_r($arr);
Code snippet
$i=0;
foreach($arr as $eacharr):
$eacharr['chant_count'] = 'Your chat count';
$arr[$i] = $eacharr;
$i++;
endforeach;
echo '<pre>';
print_r($arr);
Output before loop
Array
(
[0] => Array
(
[post_id] => 7
[post_title] => Title 7
)
[1] => Array
(
[post_id] => 8
[post_title] => Title 8
)
)
Output after loop
Array
(
[0] => Array
(
[post_id] => 7
[post_title] => Title 7
[chant_count] => Your chat count
)
[1] => Array
(
[post_id] => 8
[post_title] => Title 8
[chant_count] => Your chat count
)
)
try this example
<?php
$ss = array("0" => Array
(
"post_id" => '37',
"post_title" =>'ss',
"post_image" =>'dsd' ,
"post_status" => '1'
),
"1" => Array
(
"post_id" => '36',
"post_title" => 'TEST open for text',
"post_image" => 'post_1463052793.jpeg',
"post_status" => '1'
),
"2" => Array
(
"post_id" => '35',
"post_title" => 'Hey Sushovan',
"post_image" => 'post_1463038438.jpg',
"post_status" => '1'
)
);
print_r($ss);
$i=1;
foreach($ss as $key=>$row)
{
$ss[$key]['mm']=$i;
$i++;
}
echo "<pre>";
print_r($ss);
?>
OUTPUT
Array
(
[0] => Array
(
[post_id] => 37
[post_title] => ss
[post_image] => dsd
[post_status] => 1
[mm] => 1
)
[1] => Array
(
[post_id] => 36
[post_title] => TEST open for text
[post_image] => post_1463052793.jpeg
[post_status] => 1
[mm] => 2
)
[2] => Array
(
[post_id] => 35
[post_title] => Hey Sushovan
[post_image] => post_1463038438.jpg
[post_status] => 1
[mm] => 3
)
)
I have the following array:
Array
(
[0] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 2
)
[1] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 3
)
[2] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 4
)
[3] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 23
)
[4] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 24
)
)
I have two different hotelID keys. I would like to extract only one element (the first one) where the hotelID is unique in whole array. I am trying with following code:
$data['uniqueHotels'] = array_map('unserialize', array_unique(array_map('serialize', $hotels)));
but without any luck so far.
Anyone can give me a hint?
If looking for the first element:
<?php
$hotels = array(
array(
'id' => 1,
'hotelID' => 10
),
array(
'id' => 2,
'hotelID' => 10,
),
array(
'id' => 3,
'hotelID' => 20,
),
array(
'id' => 4,
'hotelID' => 20,
),
);
function getUniqueHotels($hotels) {
$uniqueHotels = array();
foreach($hotels as $hotel) {
$niddle = $hotel['hotelID'];
if(array_key_exists($niddle, $uniqueHotels)) continue;
$uniqueHotels[$niddle] = $hotel;
}
return $uniqueHotels;
}
$unique_hotels = getUniqueHotels($hotels);
print_r($unique_hotels);
results in:
Array
(
[10] => Array
(
[id] => 1
[hotelID] => 10
)
[20] => Array
(
[id] => 3
[hotelID] => 20
)
)
You could simply loop through the array and add them to a new array, indexed by hotelID. This way any duplicates will just overwrite the existing value and you end up with one entry per hotel:
$unique = array();
foreach ($hotels as $value)
{
$unique[$value['hotelID']] = $value;
}
$data['uniqueHotels'] = array_values($unique);
Here is a dynmaic solution:
function uniqueAssocArray($array, $uniqueKey){
$unique = array();
foreach ($array as $value){
$unique[$value[$uniqueKey]] = $value;
}
$data = array_values($unique);
return $data;
}
How to use:
uniqueAssocArray($yourArray, 'theKey');
along the lines of what you're trying,
array_unique(array_map(function($hotel) { return $hotel['hotelID']; }, $array))
How combine arrays in this way?
source:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000
)
[1] => Array
(
[id] => 3
[title] => book
[tval] => 1700
)
[3] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
result:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000,1700
)
[1] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
please help to solve this problem,
thanks!!!
sorry for bad english(
This should work:
$result = array();
foreach($array as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
This basically groups elements by id, concatenating tvals (separated by ,).
Simply building slightly on user576875's method:
$a = array ( 0 => array ( 'id' => 3,
'title' => 'book',
'tval' => 10000
),
1 => array ( 'id' => 3,
'title' => 'book',
'tval' => 1700
),
3 => array ( 'id' => 27,
'bcalias' => 'fruit',
'tval' => 3000
)
);
$result = array();
foreach ($a as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
$result = array_merge($result);
var_dump($result);
gives a result of:
array
0 =>
array
'id' => int 3
'title' => string 'book' (length=4)
'tval' => string '10000,1700' (length=10)
1 =>
array
'id' => int 27
'bcalias' => string 'fruit' (length=5)
'tval' => int 3000
The only real difference is the array_merge() to reset the keys