0
$array = array(
  "id" => array("1","2","3"), 
  "text" => array("text 1","text 2", "text 3"), 
  "checked" => array("checked","","checked")
);

i have an array like this. i want to access value in foreach like this.

first loop => 1 | text 1 | checked or null

second loop => 2 | text 2 | checked or null

0

2 Answers 2

1
foreach($array['id'] as $key=>$value)
{

    echo $value . ' | ' . $array['text'][$key] . ' | ' . ($array['checked'][$key] == 'checked' ? 'checked' : 'null') . '<br />';

}

Result:

1 | text 1 | checked
2 | text 2 | null
3 | text 3 | checked
Sign up to request clarification or add additional context in comments.

Comments

1

This code can be usefull

$array = array(
  "id" => array("1","2","3"), 
  "text" => array("text 1","text 2", "text 3"), 
  "checked" => array("checked","","checked")
);

// find the max count of multidimensional arrar
$count = max( array_map( 'count',  $array ) );
for($i=0;$i<$count;$i++){
    foreach($array as $key=>$value){
        echo $array[$key][$i]."|";

    }
     echo "\n";
}

OutPut:

1|text 1|checked|
2|text 2||
3|text 3|checked|

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.