2016-05-13 7 views
0

Ich habe ein PHP-Array-Format:Wie anhängen Daten in jedem Teil Array eines Arrays

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

Nun, ich möchte einen zusätzlichen Index mit dem Wert anzuhängen. Dazu verwende ich diesen 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); 
} 

Allerdings kann die ich nicht die Daten in der ursprünglichen $all_data schieben. Wie kann ich das erreichen?

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

Die Chat-Anzahl wird durch Aufruf der Methode count_data_by_condition() abgerufen.

+0

Haben Sie die chant_count hinzufügen oder wollen Sie zusammenführen möchten/überschreiben? – KiwiJuicer

+0

Können Sie bitte Ihr Array posten? –

+1

@BikashP. Ich habe bereits die Array-Struktur gepostet, Bruder. – Saswat

Antwort

1

Sie müssen nur die chat_count drücken, um Array zu korrigieren. Ich denke, Ihre "$ all_data" Variable ist jetzt ein Teil von $ data array mit Schlüssel 'data'.

Beispielcode:

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

Hoffe, es hilft!

1

Try unten 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); 
1

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

Ausgabe vor der Schleife

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

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

) 

Ausgabe nach Schleife

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

versuchen, dieses Beispiel

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

) 
Verwandte Themen