Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php实现复杂数组任务_Php - Fatal编程技术网

用php实现复杂数组任务

用php实现复杂数组任务,php,Php,标题 您好,这是一个php任务,我想写一个代码,使输出像 已经有了一个解决方案:D 但是我想要更多:D:D:D <?php $data = array( array( 'name'=>'Mark', 'job'=>'engineer', 'age'=>25, 'hobbies' => array('drawing','swimming','reading'

标题 您好,这是一个php任务,我想写一个代码,使输出像 已经有了一个解决方案:D 但是我想要更多:D:D:D

<?php
$data = array(
        array(
            'name'=>'Mark',
            'job'=>'engineer',
            'age'=>25,
            'hobbies' => array('drawing','swimming','reading'),
            'skills' => array('coding','fasting learning','teaching')
        ),
        array(
            'name'=>'Joe',
            'job'=>'designer',
            'age'=>19,
            'skills'=>array('fast learning')
        ) ,
        array(
            'name'=>'sara',
            'age'=>25,
            'city'=>'NY'
        ),

        array(
            'name'=>'sam',
            'job'=>'accountant',
            'age'=>25,
            'city'=>'london'
        ),
        array(
            'name'=>'Esraa',
            'job'=>'Designer',
            'age'=>23,
            'city'=>'cairo',
            'hobbies' => array('writing','reading'),
            'skills' => array('coding','teaching')
        ),
    );
    /** out put should be like this ==>
     *
     * there is [number] of users from [city]
     * ------------------------------------------
     * name : sara
     * age  : 25
     * city : Ny
     * ------------------------------------------
     * name : sam
     * age  : 25
     * city : london
     * job  : accountant
     * ------------------------------------------
     * name : Esraa
     * age  : 23
     * city : cairo
     * job  : Designer
     * skills:
     * -coding
     * -teaching
     * hobbies:
     * -writing
     * -reading
     * ----------------------------------------------
     * invalid data
     * ------------
     *
     *
     */
؟>
foreach($data as $key => $value){
    if($value["city"] != ""){
        if($count[$value["city"]] == ""){ $count[$value["city"]] = 1; }
        else{ $count[$value["city"]]++; }
    }
}

foreach($count as $key => $value){
    echo "There are ".$value." users from ".$key."<br>";
}

echo "<br><br>";

foreach($data as $key => $value){
  foreach($value as $key => $value){
    if(($value != "")&&($key != "hobbies")&&($key != "skills")){
        echo $key." : ".$value."<br>";
    }
    if(($key == "hobbies")||($key == "skills")){
        echo $key.":<br>";
        foreach($value as $kk => $vv){
            if($vv != ""){
                echo "-".$vv."<br>";
            }
        }
    }
  }
  echo "<hr>";
}