如何将数据附加到数组的每个子数组中


How to append data into each sub-Array of an array

我有一个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()方法来检索聊天计数。

您只需要将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;
}

希望它能有所帮助!

尝试以下代码:

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

您的阵列

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

代码段

$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
    )
[1] => Array
    (
        [post_id] => 8
        [post_title] => Title 8
    )
)

循环后输出

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

试试这个例子

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