auicsc
auicsc

Reputation: 297

Extract data from an array correctly

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

Upvotes: 0

Views: 65

Answers (2)

pfyod
pfyod

Reputation: 717

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

needs to be

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

Upvotes: 1

keaton_fu
keaton_fu

Reputation: 442

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

Upvotes: 1

Related Questions