0

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.

6
  • Do you want to add the chant_count or do you want to merge/overwrite? Commented May 13, 2016 at 10:53
  • Can you post your array please ? Commented May 13, 2016 at 10:53
  • 1
    @BikashP. I already posted the array structure, bro. Commented May 13, 2016 at 10:54
  • @KiwiJuicer, Want to add chat_count. Commented May 13, 2016 at 10:55
  • You just need to push the chat_count to correct array. Commented May 13, 2016 at 10:55

4 Answers 4

1

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 !

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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
    )

)

Comments

1

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
    )

  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.