0

Im a new bie to PHP..

I'm able to push the elements into an Array using array_push. It gives the output like this..

array(0) { } 
array(1) { [0]=> string(28) "For all your PC requirements" } 
array(2) { [0]=> string(28) "For all your PC requirements" [1]=> string(10) "Welcome to" }

My code is

if($msgIdFound == 1){
$parts = explode('msgid ', $line_of_text);
$fp = fopen("/home/bigc/Documents/msgids.csv","w"); 

    array_push($msgIds,$m[1]);                  

$counter++;
var_dump($msgIds);
fputcsv($fp, $msgIds);
fclose($fp);
}   

But, what I want the array looks like this.

array(array('For all your PC requirements'), array('Welcome to'))

Any help would be greatly appreciated.

1
  • You are pushing a string to it. How to you want to be an array? Push an array intead. Commented Mar 12, 2014 at 11:41

3 Answers 3

3

You can put

msgIds[] = array($m[1]);

instead of array_push(...)

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

1 Comment

This is the better answer. no reason to use array_push here. msgIds[] runs faster.
0

replace

array_push($msgIds,$m[1]);

with

if (!empty($m[1])) { // avoid empty stuff...
    array_push($msgIds, array($m[1]));
}

this should result in:

array(2)(
    [0] => 
    array(1){
        [0] => 
        string(28) "For all your PC requirements"
    }
    [0] => 
    array(1){
        [0] => 
        string(10) "Welcome to"
    }
)

Comments

0

You are pushing a string when you should push an array:

if($msgIdFound == 1){
  $parts = explode('msgid ', $line_of_text);
  $fp = fopen("/home/bigc/Documents/msgids.csv","w"); 

  array_push($msgIds, array($m[1])); // you need to push an array             

  $counter++;
  var_dump($msgIds);
  fputcsv($fp, $msgIds);
  fclose($fp);
}

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.