2016-05-13 68 views
0

的每个子阵列我有一个PHP数组格式:如何将数据追加到一个数组

[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 
     ) 

现在,我想与附加价值的额外指标。对于这一点,我使用这个代码:

$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); 
} 

但是,我不能将数据推入原$all_data。 我该如何做到这一点?

[0] => Array 
      (
       [post_id] => 37 
       [post_title] => الأَبْجَدِيَّة العَرَبِيَّة 
       [post_image] => 
       [post_status] => 1 
       [chant_count] => 2 
      ) 

通过调用count_data_by_condition()方法检索聊天计数。

+0

你要添加的chant_count还是要合并/改写? – KiwiJuicer

+0

你可以发布你的数组吗? –

+1

@BikashP。我已经发布了数组结构,bro。 – Saswat

回答

1

你只需要将chat_count推到正确的数组。我认为你的“$ all_data”变量现在是包含关键'data'的$ data数组的一部分。

示例代码:

$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; 
} 

希望它能帮助!

1

试试下面的代码:

$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); 
1

你的阵列

$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); 

代码片断循环

Array 
(
[0] => Array 
    (
     [post_id] => 7 
     [post_title] => Title 7 
    ) 

[1] => Array 
    (
     [post_id] => 8 
     [post_title] => Title 8 
    ) 

) 
012之前

$i=0; 
foreach($arr as $eacharr): 
$eacharr['chant_count'] = 'Your chat count'; 
$arr[$i] = $eacharr; 
$i++; 
endforeach; 

echo '<pre>'; 
print_r($arr); 

输出循环之后

输出

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 
    ) 

) 
1

尝试这个例子

<?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); 
    ?> 

输出

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 
    ) 

)