PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
aft3rm4th
Forum Newbie
Posts: 8 Joined: Sun Sep 26, 2004 5:00 pm
Location: Polokwane, South Africa
Post
by aft3rm4th » Sun Sep 26, 2004 5:15 pm
Hi Developers
I have the following array:
Code: Select all
numsї0]ї0] = "8 6 3 17";
numsї0]ї1] = "5 5 4 14";
numsї0]ї2] = "5 3 6 14";
I extracted them from an html file using preg_match_all(). What I would like to do is to split these numbers up and store them in a new two dimensional array as follows:
Code: Select all
new_numї0]ї0] = 8;
new_numї0]ї1] = 6;
new_numї0]ї2] = 3;
new_numї0]ї3] = 17;
new_numї1]ї0] = 5;
new_numї1]ї1] = 5;
new_numї1]ї2] = 4;
new_numї1]ї3] = 14;
new_numї2]ї0] = 5;
new_numї2]ї1] = 3;
new_numї2]ї2] = 6;
new_numї2]ї3] = 14;
Can I do this with a for loop? I just can't seem to figure this one out!
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Sep 26, 2004 5:53 pm
Code: Select all
<?php
$nums[0] = "8 6 3 17";
$nums[0] = explode(" ",$nums[0]);
print_r($nums[0]);
?>
will ouput
Code: Select all
Array ( ї0] => 8 ї1] => 6 ї2] => 3 ї3] => 17 )
aft3rm4th
Forum Newbie
Posts: 8 Joined: Sun Sep 26, 2004 5:00 pm
Location: Polokwane, South Africa
Post
by aft3rm4th » Mon Sep 27, 2004 3:44 am
Yes, that works fine with a single dimensional array, but how do I read the values from a two dimensional array, split them, and store them seperately in a new two dimensional array?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Sep 27, 2004 3:52 am
Code: Select all
$newarr[] = explode(' ',$oldarr[0]);
aft3rm4th
Forum Newbie
Posts: 8 Joined: Sun Sep 26, 2004 5:00 pm
Location: Polokwane, South Africa
Post
by aft3rm4th » Tue Sep 28, 2004 3:30 pm
Thanks for your help guys. Here's what I've done...
Code: Select all
<?php
$nums[0][0] = "8 6 3 17";
$nums[0][1] = "5 5 4 14";
$nums[0][2] = "5 3 6 14";
$numscount = count($nums[0]);
for ($i=0; $i<$numscount; $i++) {
$new_num[] = explode(" ", $nums[0][$i]);
}
$nnumcount = count($new_num[0]);
for ($i=0; $i<$numscount; $i++) {
for ($j=0; $j<$nnumcount; $j++) {
printf("\$nnumcount[".$i."][".$j."] = %d<br>", $new_num[$i][$j]);
}
}
?>
and the result...
Code: Select all
$nnumcountї0]ї0] = 8
$nnumcountї0]ї1] = 6
$nnumcountї0]ї2] = 3
$nnumcountї0]ї3] = 17
$nnumcountї1]ї0] = 5
$nnumcountї1]ї1] = 5
$nnumcountї1]ї2] = 4
$nnumcountї1]ї3] = 14
$nnumcountї2]ї0] = 5
$nnumcountї2]ї1] = 3
$nnumcountї2]ї2] = 6
$nnumcountї2]ї3] = 14
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 28, 2004 3:33 pm
looks like you figured it out, want me to mark the thread solved?
aft3rm4th
Forum Newbie
Posts: 8 Joined: Sun Sep 26, 2004 5:00 pm
Location: Polokwane, South Africa
Post
by aft3rm4th » Tue Sep 28, 2004 3:36 pm
Yes, that would be great.