dongxun7962 2016-05-11 03:23
浏览 28
已采纳

如果数组的指定子数组的值不相同,则添加新数组

First I have this array

$age = $request->age //laravel's input

$m_array = array(
   'jason' => array(
      'age' => '21', 
      'gender' => 'male'
     ),
   'mechelle' => array(
      'age' => '19', 
      'gender' => 'female'
     ),
   'Khent' => array(
      'age' => '25', 
      'gender' => 'male'
     )
  );

and then check if a specified array exist from the $m_array arrays

if(isset($m_array['jason'])):

endif

and then I want to check if each array that has a 'jason' value and its sub array named 'age' has the same value from the variable '$age' and if does not then add the same array but not the same sub array named 'age' value e.g.

$m_array['jason'] = array('age' => $age, 'gender' => 'male')

so the expected output if an array is added with the same name but not the same sub array named 'age' value will be

$m_array = array(
    'jason' => array(
       'age' => '21', 
       'gender' => male
      ),
    'jason' => array(
        'age' => '18', 
        'gender' => male
     )
  );

any ideas, help, clues, recommendations, suggestions to achieve that please?

  • 写回答

1条回答 默认 最新

  • doumowu7371 2016-05-11 03:41
    关注

    Just check the $m_array['jason'] exists or not, if not then the add a new array into the main array.

    Try adding 'jason' => array('age' => '21', 'gender' => 'male'), in the main array, you will get a message Already exist and if you run this you will be notified Array Inserted.

    As per your last requirement I also check for that array has the same sub array or not, if not then add the array into the main array.

    $age = '22';
    $gender = 'male';
    $m_array = array('jason' => array('age' => '21', 'gender' => 'male'),
                     'mechelle' => array('age' => '19', 'gender' => 'female'),
                     'Khent' => array('age' => '25', 'gender' => 'male')
                    );
    
    if(isset($m_array['jason']) && is_array($m_array['jason'])){
        if($m_array['jason']['age'] == $age && $m_array['jason']['gender'] == $gender)
            echo 'Already exist';
        else
            $m_array['jason'] = array('age' => $age, 'gender' => $gender);
    }else{
        $m_array['jason'] = array('age' => $age, 'gender' => $gender);
        echo 'Array Inserted';
    }
    
    print_r($m_array);
    

    Result:

    The value age overridden.

    Array
    (
        [jason] => Array
            (
                [age] => 22
                [gender] => male
            )
    
        [mechelle] => Array
            (
                [age] => 19
                [gender] => female
            )
    
        [Khent] => Array
            (
                [age] => 25
                [gender] => male
            )
    
    )
    

    Note: The same key of an array never added, its must be override, So you will loss data. Don't do that, This is what you want. But this is not possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计