Jump to content

[SOLVED] Explode array counting?


NerdConcepts

Recommended Posts

I am trying to count the number of pieces of an explode so that I can run a foreach command. My code goes out and grabs work order codes from the database. Then runs and explode command to separate those codes. But I have to count how many codes there are to be able to run a foreach. Any ideas? Here is my code.

 

example of wo_coding:
E)|$$|JH|KA

so the count would be 4.

 

the code I have so far:

$id = $_GET['view'];

$query = "SELECT wo_coding FROM work_orders WHERE ";
$result = mysql_query($result);
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$coding = $row['wo_coding'];
$excode = explode("|", $coding);

// counting of elements in $excode would go here.

//foreach stuff would be here.

 

some have counts up to 20/30. But almost all are different, so I cannot just put a static number in there are it would throw an error.

Link to comment
https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/
Share on other sites

Ummm.... why not just use a foreach loop?

 


$str = 'this|is|something|to|explode';
$e = explode("\t", $str);

foreach($e as $v) echo $v . '<br />';

$i = 0;
$c = count($e);

while($i < $c) echo $e[$i] . '<br />';

for($i = 0; $i < $c; $i++) echo $e[$i] . '<br /';

 

All of those would do the exact same thing....

I figured maybe an easier way, and it works.

 

$id = $_GET['view'];
$query = "SELECT wo_coding FROM work_orders WHERE wo_num='$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$coding = $row['wo_coding'];
$excode = explode("|", $coding);

$ctr = -1;
foreach ($excode as $x) {
$ctr++;
   echo $x . '<br />';
}

 

the echo in the foreach will be replaced but it still works great. Not sure why I thought I needed a count...but thanks a bunch anyways.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.