如何在PHP中细分数组多维

时间:2019-06-16 07:07:22

标签: php arrays

我在下面详细描述数组编号时遇到问题

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

如果最终结果如下,该怎么办

 var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

字符串array (size=3) 0 => array (size=2) 'um' => float 1000 'kali' => string '2' (length=1) 1 => array (size=2) 'um' => float 2000 'kali' => string '5' (length=2) 2 => array (size=2) 'um' => float 5000 'kali' => string '1' (length=1) 的循环次数与以前的数组相同

例如: array (size=7) 0 => array (size=2) 'um' => float 1000 'kali' => string '1' (length=1) 1 => array (size=2) 'um' => float 1000 'kali' => string '2' (length=1) 2 => array (size=2) 'um' => float 2000 'kali' => string '3' (length=1) 3 => array (size=2) 'um' => float 2000 'kali' => string '4' (length=1) 4 => array (size=2) 'um' => float 2000 'kali' => string '5' (length=1) 5 => array (size=2) 'um' => float 2000 'kali' => string '6' (length=1) 6 => array (size=2) 'um' => float 2000 'kali' => string '7' (length=1) 7 => array (size=2) 'um' => float 5000 'kali' => string '8' (length=1) 循环2次 ['kali']循环5次 等。

请启发本案

2 个答案:

答案 0 :(得分:1)

您可以使用//code to take input of strings in an array of pointers #include <stdio.h> #include <strings.h> int main() { //suppose the array of pointers is of 10 elements char *strings[10],string[50],*p; int length; //proper method to take inputs: for(i=0;i<10;i++) { scanf(" %49[^\n]",string); length = strlen(string); p = (char *)malloc(length+1); strcpy(p,string);//why use strcpy here instead of p = string strings[i] = p; //why use this long way instead of writing directly strcpy(strings[i],string) by first defining malloc for strings[i] } return 0; }

array_walk

DEMO

答案 1 :(得分:1)

简单的foreach循环可以这样做:

$cnt = 1;
foreach($arr as $e) {
    for($i = 0; $i < $e["kali"]; $i++)
        $res[] = array('um' => $e['um'], 'kali' => $cnt++);
}

实时示例:3v4l