1

i was working on array using PHP lang. I got following output by rotating for loop. Now want to split this into two different array. Any one please guide me to split into two different array... Like into a[0] = 810 and b[0] = 44

as well as , i am trying to sort it without changing the values for example sorting max value a[0] = 840 & b[0] = 87

Array
(
    [0] => 810/44
    [1] => 0/280
    [2] => 73/279
    [3] => 0/278
    [4] => 0/277
    [5] => 0/276
    [6] => 840/87
    [7] => 0/275
    [8] => 0/274
    [9] => 0/273
    [10] => 6/271
    [11] => 0/272
    [12] => 0/269
    [13] => 3/267
    [14] => 0/268
    [15] => 3/259
    [16] => 124/108
    [17] => 82/239
    [18] => 221/238
    [19] => 30/160
    [20] => 3/161
    [21] => 65/168
    [22] => 73/169
    [23] => 0/170
    [24] => 0/248
    [25] => 192/226
    [26] => 209/222
    [27] => 80/223
    [28] => 273/182
    [29] => 79/183
    [30] => 73/184
    [31] => 73/185
    [32] => 6/186
    [33] => 73/187
    [34] => 0/188
    [35] => 48/270
    [36] => 138/190
    [37] => 370/191
    [38] => 152/192
    [39] => 12/200
    [40] => 200/210
    [41] => 0/213
    [42] => 21/212
    [43] => 124/214
    [44] => 0/215
    [45] => 0/216
    [46] => 133/217
    [47] => 6/260
    [48] => 73/285
    [49] => 73/284
    [50] => 0/283
    [51] => 73/286
    [52] => 73/287
    [53] => 0/288
    [54] => 73/289
    [55] => 0/290
    [56] => 76/291
    [57] => 76/292
    [58] => 0/293
    [59] => 0/294
    [60] => 0/295
    [61] => 0/296
    [62] => 0/297
    [63] => 3/298
    [64] => 0/299
    [65] => 0/300
    [66] => 3/301
)

3 Answers 3

5

How about:

$a = $b = array();

foreach ($array as $key => $value) {
    list($a[$key], $b[$key]) = explode('/', $value, 2);
}

EDIT: To handle the sorting afterwards, you can use this:

array_multisort($a, SORT_DESC, SORT_NUMERIC, $b);
Sign up to request clarification or add additional context in comments.

4 Comments

yes , i will use this only ... But before it divided into two array .. how we can sort it ?
Amazing it working fine !! Hey Please can give me your gmail please
@SandeepKamble all my contact info is available on my user profile.
I have invited you on gmail please add !
1

You could do it like this:

<?php 
$a = array();
$b = array();

$orig_array = array('810/44','0/280');

foreach($orig_array as $orig){
   $values = explode('/',$orig,2);
   $a[]=$values[0];
   $b[]=$values[1];
}
print_r($a);
print_r($b);
/*
Array
(
    [0] => 810
    [1] => 0
)
Array
(
    [0] => 44
    [1] => 280
)    
*/
?>

3 Comments

Thanks this will work but sorting will not working in this passion
@Sandeep Kamble how do you want it sorted
okay , 1st i need to sort it by max value. Without changing the values for example sorting max value a[0] = 840 & b[0] = 87
1
  foreach($array1 as $a){

 $a = (string)$a

 $pos = strpos($a, '/')

  $first[] = substr($a, 0, $pos);
 $second[] = substr($a, $pos);

 }

not tested . . .but u can try something on these lines

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.