0

I am trying to get data from an array on form of:

  Array ( [actual-1] => 2 [action-1] => blabla [actual-2] => 1 [action-2] => sss [actual-49] => 3 [action-58] => sasa )

 ID 1 Level 2 action blabla
 ID 2 level 1 action sss
 ID 49 level 3 action sasa

I tried the following code:

foreach(array_chunk($array,3,true) as $val){

   foreach($val as $k=>$v){
      if(strpos($k, "actual") !== false){
          $temp = explode("-",$k);
          $id = $temp[1];
          $actual = $v;
      }
      if(strpos($k, "action") !== false){
          $action = $v;
      }
   }
    echo "ID ".$id." Level ".$actual." action ".$action;
    echo "<br>";
}

But what I get instead was:

ID 2 Level 1 action blabla
ID 49 level 3 action sasa

2 Answers 2

1

You're chunking your array into groups of 3 elements instead of 2. Change the second argument in array_chunk to 2.

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

Comments

1
foreach(array_chunk($array,3,true) as $val){

needs to be

foreach(array_chunk($array,2,true) as $val){

1 Comment

Thank's a lot, I dont know why I put 3 there.

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.