PHP数组读取

The file originally was like this way:

$strings

redredredredgreengreengreengreengreenblackblackblackblackblackwhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitegreenredorangeorangeorangeblackblackblackyellowyellowyellowyellow

foreach ($inarray as $k=>$v)

So I converted to array

Array ( [0] => red[red] => 17 
        [1] => orange[orange] => 3 
        [2] => green[green] => 46 
        [3] => yellow[yellow] => 5 
        [4] => black[black] => 21 
        [5] => white[white] => 10 
      )

HoW do I read the Array just like

red is 17
orange is 3
green is 46
yellow is 5
black is 21
white is 10

currently I am using foreach($thisarray as $key => $value), but it reads the way is not I am expecting. many thanks

foreach ($array as $k=>$v) {
   $color = preg_replace('/\[.*\]/', '', $k);
   echo $color . ' is ' . $v;
}

There's probably some typo in the array representation you have up there. Anyway, to read from a string, you could use sscanf(..) (I would hope that you are checking for the validity of the string and of course the return value of sscanf(..)):

<?php
    $output = array();
    $s = "red is 17";
    $arr = sscanf($s, "%s is %d");
    $output[$arr[0]] = $arr[1];
    print_r($output);
?>

Output:

Array
(
    [red] => 17
)

You could easily extend this to reading from several strings inside an array or something.

HTH

<?php
$arr=array(0 => 'red[red] => 17',
        1=> 'orange[orange] => 3' ,
        2 => 'green[green] => 4' ,
        3 => 'yellow[yellow] => 5' ,
        4 => 'black[black] => 21' ,
        5 => 'white[white] => 10');


foreach ($arr as $k=>$v) {

    $str=preg_match('[[a-z \s]+]',$v,$match);
    $num=preg_match('/[0-9]+$/',$v,$matchnum);
    echo $match[0].' is '.$matchnum[0]."<br>";
}
?>

Not sure if your looking for something simpler. but I think you want this:

<?php
  $colour = array()
    $color['red'] = 17;
    $color['orange'] = 3; 
    //etc


foreach ($colour as $name=>$value) {

   echo "<br> $name is $value";
}
?>

output

red is 17
orange is 3