0

I have the following array

array(
    'messages' => array(
        0 => array(
            'id' => '3',
            'subject' => 'what did you say last night?',
            'message' => 'your mother wai',
            'from' => '13',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 14:02:12',
        ) ,
        1 => array(
            'id' => '2',
            'subject' => 'this is new',
            'message' => 'hello guy',
            'from' => '11',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 13:59:56',
        ) ,
        2 => array(
            'id' => '1',
            'subject' => 'hello boy.',
            'message' => 'i love this too much . what do you think about making this happen for tomorrow? already then . good bye',
            'from' => '11',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 13:54:02',
        ) ,
    ) ,
)

All i want to do is to take the 'FROM' value of every key in the array(in this case it is - 13, 11 ,11) and then send them to the database to retrieve information like their name and avatar , in codeigniter.

UPDATE

I TRIED THE FOLLOWING AND IT GAVE ME 11,11,13, (with a comma at the back).

 $from  =  $messages['messages'];


      for ($col = 0; $col < 3; $col++) {
        echo $messages['messages'][$col]['from'].",";
      }   

Besides , i dont want to determine how many values it should echo. I want it to echo as long as it exists.

1
  • you put here var_dump() array. Please provide var_export() array, so that we can test Commented May 14, 2016 at 13:48

1 Answer 1

2

you may use this

$t = array(
    'messages' => array()
 )
 /// your array ......

$from = '';
foreach($t['messages'] as $k => $m){
    if($k == 0){
        $from = $m['from'];
    }else{
        $from .= ','.$m['from'];
    }
}
echo $from;

It will output as 13,11,11. But if you need array of those values then you may use following

$from = array();
foreach($t['messages'] as $k => $m){
        $from[] = $m['from'];

}
print_r($from);
Sign up to request clarification or add additional context in comments.

5 Comments

Vote up. Alternatively, you can offer $from[$k] to have binded concrete message key.
thanks but this ` $t = array( 'messages' => array() ); /// your array ...... $from = $messages['messages']; foreach($t['messages'] as $k => $m){ $from[] = $m['from']; } print_r($from); ` only prints an empty array - array();
the following one prints 13,13. $t = array( 'messages' => array() ); /// your array ...... $from = $messages['messages'][0]['from']; foreach($t['messages'] as $k => $m){ if($k == 0){ $from = $m['from']; }else{ $from .= ','.$m['from']; } } echo $from; print_r($from); . would be nice to combine the value into an array so i can send it to the database for processing please. thanks
you may use first solution and then explode the result to make array. $final_arr = explode(',',$from);
WORKED NOW. HAD TO DO A FEW TWEAKING!

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.