skip to Main Content

This is my array

array(5) {
  [0]=>
  string(2) "10"
  [1]=>
  string(2) "22"
  [2]=>
  string(2) "25"
  [3]=>
  string(2) "37"
  [4]=>
  string(2) "38"
}

How to get it to array like this

array(1) {
  [0]=>
  string(2) "10"
    }
array(2) {
  [0]=>
  string(2) "22"
    }
array(3) {
  [0]=>
  string(2) "25"
    }
array(4) {
  [0]=>
  string(2) "37"
    }
array(5) {
  [0]=>
  string(2) "38"
}

I’m using this code

$sellerIds = * My Array *

if(is_array($sellerIds)){
    foreach($sellerIds as $key => $value){
         $sellerIds = $value;
    }
}
// do something with $sellerIds

But when i var_dump it, it like this

string(2) "10"
string(2) "22"
...

Please Help Me ! Thanks All

2

Answers


  1. Using array_chunk function :-

    print_r(array_chunk($input_array,1,true)); //pass your Array
    

    array_chunk

    Login or Signup to reply.
  2. $array=array("10","22","25","37","38");
    
    $finalArrays=(array_chunk($array, 1,true));
    
    foreach($finalArrays as $value)
    
    {
    
      var_dump($value);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search