1

I am trying to get my head around arrays.

The arrays should look like this:

$questions[$a] => array( [0] => No, comment1
                         [1] => Yes, comment2
                         [2] => No, comment3 )

$answer[$a] => array( [0] => No
                      [1] => Yes
                      [3] => No )

$comment[$a] => array( [0] => comment1
                       [1] => comment2
                       [3] => comment3 )

=========================================================================

SECOND EDIT: Need to execute this in the loop to create a third array -

if($answer[$a] == "Yes") { $display[$a] = "style='display:none'"; 
} else { $display[$a] = "style='display:block'"; }

This is what i have: (28th for minitech)

while ($a > $count)
{
if($count > 11) {
foreach($questions as $q) {
    list($answer, $comments[]) = explode(',', $q);
    if($answer === "Yes") { 
    $display[$a] = "style='display:none'"; 
    } else { 
    $display[$a] = "style='display:block'"; 
    }

$answers[] = $answer;
    }
  }
$a++;
}
9
  • 1
    If they're actually strings separated by commas, that would be a problem. Try explode. Commented Apr 25, 2013 at 15:51
  • Sorry, i accepted a bit prematurely, i also need this code executed as part of the loop to create a third array... if($answer[$a] == "Yes") { $display[$a] = "style='display:none'"; } else { $display[$a] = "style='display:block'"; } Commented Apr 28, 2013 at 0:51
  • Okay, I added an answer. Commented Apr 28, 2013 at 1:43
  • you need to work on your premature acceptance... Commented Apr 28, 2013 at 13:55
  • @Raidenace: What does that even mean? Commented Apr 28, 2013 at 15:54

8 Answers 8

1

If they are actually strings, explode works:

$answers = array();
$comments = array();
$display = array();

foreach(array_slice($questions, 11) as $question) {
    list($answer, $comments[]) = explode(',', $question);
    $display[] = $answer === 'Yes' ? 'style="display: none"' : 'style="display: block"';
    $answers[] = $answer;
}

Here’s a demo!

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

17 Comments

ahh... ok gottcha, will give it a try. Thanks!
Just to clarify, how do i create the $display array with the above, i was counting $a like $display[$a] each iteration, do i define it as $display[] and just use $display for each iteration? Hope that makes sense!
@Edward: This replaces your inner loop, so it would still be $display[$a].
Getting parse error unexpected "[". I am running PHP 5.2.17, i see codepad is set to 5.5, would this affect the result??
Will talk to our server guy. :)
|
0

Change your while loop to this

while ...
{
$parts = explode(',', $questions[$a]);
$answer[$a][] = trim($parts[0]);
$comment[$a][] = trim($parts[1]);
}

In your original code you were overwriting the $answer[$a] and $comment[$a] each time, not appending to the end of an array

1 Comment

Modified slightly as I wasn't sure if you had done the split (also I missed a bracket in there )
0
$questions[$a] = array('Q1?' => 'A1', 'Q2?' => 'A2', 'Q3?' => 'A3');

foreach($questions[$a] as $key => $value)
{
    $comment[$a][] = $key;
    $answer[$a][] = $value;
}

Comments

0

This should work.

foreach ($questions[$a] as $key=>$value){
  $temp = explode(',',$value);
  $answer[$key] = $temp[0];
  $comment[$key] = $temp[1];
}

$key will have 0,1,2 respectively. $value will have the values for each $question[$a](No,Comment1 ....)

Comments

0

Can't think of a funky one-liner, but this should do it:

foreach ($questions as $a => $entries) {
    foreach ($entries as $k => $entry) {
        $parts = array_map('trim', explode(',', $entry));
        $answer[$a][$k] = $parts[0];
        $comment[$a][$k] = $parts[1];
    }
}

Comments

0
$questions = array( 0 => 'No,comment1',1 => 'Yes,comment2',2 => 'No,comment3' );

foreach($questions as $question)
{
    $parts = explode(",",$question);
    $answer[] = $parts[0];
    $comment[] = $parts[1]; 
}

echo "<pre>";
print_r($answer);
print_r($comment);                 

Comments

0

Here is the right answer

foreach($questions as $key => $question){
  foreach($question as $q => $data){
   $data= explode(',',$data);
   $comments[$key][$q] = $data[0];
   $answer[$key][$q] = $data[1];
  }
}

Comments

0

If the values in $questions are comma-separated strings you could use an array_walk function to populate your $answer and $comment arrays

$question = array(...); //array storing values as described
$answer = array();
$comment = array();

array_walk($question, function ($value, $key) use ($answer,$comment) {
    $value_array = explode(',', $value);
    $answer[$key] = $value_array[0];
    $comment[$key] = $value_array[1];
});

Note that this is shown using an anonymous function (closure) which requires PHP >= 5.3.0. If you had a lower version of PHP, you would need to declare a named function, and declare $answer and $comment as globals in the function. I think this is a hacky approach (using globals like this) so if I was using PHP < 5.3 I would probably just use a foreach loop like other answers to your question propose.

Functions like array_walk, array_filter and similar functions where callbacks are used are often great places to leverage the flexibility provided by anonymous functions.

1 Comment

5.2 :( thanks for the example tho, will bear it in mind if we upgrade.

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.