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

时间:2016-05-13 10:51:16

标签: php arrays

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

4 个答案:

答案 0 :(得分:1)

您只需将chat_count推送到正确的数组即可。我认为你的" $ all_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 :(得分: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);

答案 2 :(得分: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);

代码段

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

)

答案 3 :(得分: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
    )

  )