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
)
)
Related
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
)
)
I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You
Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();
This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST
I'm losing my hair on this one... I have an array structure that print_r's like this (I've hidden unnecessary fields):
[0] => Array
(
[id] => 14
[name] => Foo Directory
)
[1] => Array
(
[id] => 16
[name] => Bar Project
[parent] => Array
(
[id] => 14
[name] => Foo Directory
)
)
[2] => Array
(
[id] => 20
[name] => Baz Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
)
[3] => Array
(
[id] => 10
[name] => Qux Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
And I need it to be nested like this:
[0] => Array
(
[id] => 14
[name] => Foo Directory
[children] => Array
(
[id] => 16
[name] => Bar Project
[children] => Array
(
[id] => 20
[name] => Baz Project
)
(
[id] => 10
[name] => Qux Project
)
)
)
What I've tried so far
$projTree = array();
foreach ($projects as $project) {
if (isset($project['parent']))
array_push($projTree['children'], $project);
$projTree['id'] = $project['parent']['id'];
}
But that overwrites the previous inserted element. I also tried to walk it recursively, but couldn't figure out the correct callback for that, since it only operates on the leafs of the tree and I need to fully walk it.
How can I do this? Thanks!
Please try like below:-
<?php
$array = Array(
'0' => Array
(
'id' => 14,
'name' => 'Foo Directory'
),
'1' => Array
(
'id' => 16,
'name' => 'Bar Project',
'parent' => Array
(
'id' => 14,
'name' => 'Foo Directory'
)
),
'2' => Array
(
'id' => 20,
'name' => 'Baz Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
),
'3' => Array
(
'id' => 10,
'name' => 'Qux Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
)
);
$new_array = array();
$i = 0;
$j = 0;
foreach ($array as $key=>$val){
if(array_key_exists('parent',$val)){
foreach($new_array as $key1=>$val1){
if($val['parent']['name'] === $val1['name']){
$new_array[$key1]['children'][$i]['id'] = $val['id'];
$new_array[$key1]['children'][$i]['name'] = $val['name'];
}else{
foreach ($val1['children'] as $key3=>$val3){
if($val['parent']['name'] === $val3['name']){
$new_array[$key1]['children'][$key3]['children'][$j]['id'] = $val['id'];
$new_array[$key1]['children'][$key3]['children'][$j]['name'] = $val['name'];
}
$j++;
}
$i++;
}
}
}else{
$new_array[$key] = $val;
}
}
echo "<pre/>";print_r($new_array);
Output:- https://eval.in/419591
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